diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2025-07-05 22:04:17 -0400 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-07-06 11:49:26 -0400 |
| commit | ea91500ee1fa75aabf15af085cb98a98b3fb4b3e (patch) | |
| tree | ee70b6e8e265d10c39a995d7d175c9d468495e25 /go/game | |
| parent | 8cc7981fc197f863cb342fa8cbaa9828b2233ac6 (diff) | |
| download | muhqs-game-ea91500ee1fa75aabf15af085cb98a98b3fb4b3e.tar.gz muhqs-game-ea91500ee1fa75aabf15af085cb98a98b3fb4b3e.zip | |
add attack unit tests and documentation
Diffstat (limited to 'go/game')
| -rw-r--r-- | go/game/attack.go | 10 | ||||
| -rw-r--r-- | go/game/attack_test.go | 52 |
2 files changed, 60 insertions, 2 deletions
diff --git a/go/game/attack.go b/go/game/attack.go index 3896e142..9be465f2 100644 --- a/go/game/attack.go +++ b/go/game/attack.go @@ -7,18 +7,23 @@ import ( "strings" ) +// Attack contains all damage values for each attackable range. +// The damage value for range 1 is stored at index 0, the value for range 2 at index 1 and so on. type Attack struct { attacks []int } +// INVALID_ATTACK reports if there are no attackable ranges. func INVALID_ATTACK(a Attack) bool { return len(a.attacks) == 0 } +// MaxRange returns the biggest attackable range. func (a Attack) MaxRange() int { return len(a.attacks) } +// DamageInRange returns the damage in a certain range. func (a Attack) DamageInRange(r int) int { if r > a.MaxRange() { return 0 @@ -40,7 +45,7 @@ func (a Attack) String() string { return fmt.Sprintf("%d range %d", a.DamageInRange(r), r) } -func parseAttack(attack interface{}) Attack { +func parseAttack(attack any) Attack { if damage, ok := attack.(int); ok { return Attack{[]int{damage}} } @@ -48,6 +53,7 @@ func parseAttack(attack interface{}) Attack { attackStr := attack.(string) tokens := strings.Split(attackStr, " ") a, err := strconv.Atoi(tokens[0]) + // TODO: think about panicking here if err != nil { log.Panicf("Attack %s has no valid Damage value\n", attackStr) } @@ -61,7 +67,7 @@ func parseAttack(attack interface{}) Attack { } attacks := make([]int, 0, r) - for i := 0; i < r; i++ { + for range r { attacks = append(attacks, a) } diff --git a/go/game/attack_test.go b/go/game/attack_test.go new file mode 100644 index 00000000..6327b8eb --- /dev/null +++ b/go/game/attack_test.go @@ -0,0 +1,52 @@ +package game + +import ( + "testing" +) + +func TestParseIntAttack(t *testing.T) { + a := parseAttack(1) + if a.MaxRange() != 1 { + t.Fatal("unexpected max range (1):", a.MaxRange()) + } + if a.DamageInRange(1) != 1 { + t.Fatal("unexpected damage in range 1 (1):", a.DamageInRange(1)) + } +} + +func TestParseStringAttack(t *testing.T) { + a := parseAttack("1") + if a.MaxRange() != 1 { + t.Fatal("unexpected max range (1):", a.MaxRange()) + } + if a.DamageInRange(1) != 1 { + t.Fatal("unexpected damage in range 1 (1):", a.DamageInRange(1)) + } + if a.DamageInRange(2) != 0 { + t.Fatal("unexpected damage in range 2 (0):", a.DamageInRange(2)) + } +} + +func TestParseRangedAttack(t *testing.T) { + a := parseAttack("1 range 2") + if a.MaxRange() != 2 { + t.Fatal("unexpected max range (2):", a.MaxRange()) + } + if a.DamageInRange(1) != 1 { + t.Fatal("unexpected damage in range 1 (1):", a.DamageInRange(1)) + } + if a.DamageInRange(2) != 1 { + t.Fatal("unexpected damage in range 2 (1):", a.DamageInRange(2)) + } + if a.DamageInRange(3) != 0 { + t.Fatal("unexpected damage in range 3 (0):", a.DamageInRange(3)) + } +} + +func TestAttackString(t *testing.T) { + aStr := "2 range 3" + a := parseAttack(aStr) + if a.String() != aStr { + t.Fatal("unexpected attack string:", a.String()) + } +} |
