diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2025-07-05 22:04:17 -0400 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-08-20 15:57:24 +0200 |
| commit | a1ee56f631fb55161944a7c61b8153642098e09e (patch) | |
| tree | 1c02918152deadba270f5730c14f21a4c6d2d9a7 | |
| parent | 0da955d5fcd7db8bf85a55733257622682bf620e (diff) | |
| download | muhqs-game-a1ee56f631fb55161944a7c61b8153642098e09e.tar.gz muhqs-game-a1ee56f631fb55161944a7c61b8153642098e09e.zip | |
add attack unit tests and documentation
| -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()) + } +} |
