Conversation
|
FINALLY THE BUILD PASSED |
| func troll(nick, msg string) string { | ||
| const TROLL_USAGE = "Usage: !troll <nick>" | ||
|
|
||
| msg = strings.Trim(msg, "!troll ") |
There was a problem hiding this comment.
why not strings.Split(msg, " ") and then check length to get the target for the troll launcher?
| const TROLL_USAGE = "Usage: !troll <nick>" | ||
|
|
||
| msg = strings.Trim(msg, "!troll ") | ||
| if len(msg) > 1 || len(msg) < 1 { |
There was a problem hiding this comment.
this is equivalent to len(msg) != 1
| return TROLL_USAGE | ||
| } | ||
|
|
||
| target := string(msg) |
There was a problem hiding this comment.
Recommend against casting this here. If you want to treat a slice of length 1 as a string, just make it a string when you create it initially.
| return "Wha?! The trolls missed! That, like, never happens!" | ||
| } | ||
|
|
||
| return nick + " fires " + numTrolls + " at " + target + ", dealing " + dmg + " points of " + dmgType + " damage!" |
There was a problem hiding this comment.
So many concatenations here. Why not use a format string to make this simpler to read?
| } | ||
|
|
||
| func launchTrolls() (numTrolls, dmg, dmgType string) { | ||
| damage_type := [13]string{"bludgeoning", "piercing", "slashing", "cold", "fire", "acid", "poison", |
There was a problem hiding this comment.
Make this a slice, not an array of fixed length.
| damage_type := [13]string{"bludgeoning", "piercing", "slashing", "cold", "fire", "acid", "poison", | |
| damage_type := []string{"bludgeoning", "piercing", "slashing", "cold", "fire", "acid", "poison", |
| trolls := rand.Intn(10) | ||
| if trolls == 0 { | ||
| return string(trolls), "", "" | ||
| } | ||
|
|
||
| dmg = trollDamage(trolls) | ||
|
|
||
| return string(trolls), dmg, damage_type[rand.Intn(12)] | ||
| } |
There was a problem hiding this comment.
What about adding a zero case to trollDamage? Doing so would allow you to do something like
| trolls := rand.Intn(10) | |
| if trolls == 0 { | |
| return string(trolls), "", "" | |
| } | |
| dmg = trollDamage(trolls) | |
| return string(trolls), dmg, damage_type[rand.Intn(12)] | |
| } | |
| return string(trolls), trollDamage(rand.Intn[10]), damage_type[rand.Intn(12)] | |
| } |
|
|
||
| dmg = trollDamage(trolls) | ||
|
|
||
| return string(trolls), dmg, damage_type[rand.Intn(12)] |
There was a problem hiding this comment.
Treating a []string with len 1 like a string. Fix the type at initialization so you can use it properly.
| func trollDamage(trolls int) string { | ||
| i := 0 | ||
| trollDmg := 0 | ||
| for i < trolls { | ||
| trollDmg += rand.Intn(20) | ||
| } | ||
|
|
||
| if trollDmg == 0 { | ||
| return "miss" | ||
| } | ||
|
|
||
| return string(trollDmg) | ||
| } |
There was a problem hiding this comment.
Let's make this function a little less surprising. We expect we are dealing with integers, so let's stay dealing with integers and only cast if we need to
| func trollDamage(trolls int) string { | |
| i := 0 | |
| trollDmg := 0 | |
| for i < trolls { | |
| trollDmg += rand.Intn(20) | |
| } | |
| if trollDmg == 0 { | |
| return "miss" | |
| } | |
| return string(trollDmg) | |
| } | |
| func trollDamage(trolls int) int { | |
| dmg := 0 | |
| for i, _ range trolls { | |
| dmg += rand.Intn(20) | |
| } | |
| return dmg | |
| } |
No description provided.