aboutsummaryrefslogtreecommitdiff
path: root/go/game/attack.go
diff options
context:
space:
mode:
Diffstat (limited to 'go/game/attack.go')
-rw-r--r--go/game/attack.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/go/game/attack.go b/go/game/attack.go
new file mode 100644
index 00000000..97881d48
--- /dev/null
+++ b/go/game/attack.go
@@ -0,0 +1,50 @@
+package game
+
+import (
+ "fmt"
+ "log"
+ "strconv"
+ "strings"
+)
+
+type Attack struct {
+ Damage int
+ Range int
+}
+
+func INVALID_ATTACK() Attack {
+ return Attack{-1, -1}
+}
+
+func (a Attack) String() string {
+ if a == INVALID_ATTACK() {
+ return "invalid attack"
+ }
+
+ if a.Range == 1 {
+ return fmt.Sprintf("%d", a.Damage)
+ }
+ return fmt.Sprintf("%d range %d", a.Damage, a.Range)
+}
+
+func parseAttack(attack interface{}) Attack {
+ if damage, ok := attack.(int); ok {
+ return Attack{damage, 1}
+ }
+
+ attackStr := attack.(string)
+ tokens := strings.Split(attackStr, " ")
+ a, err := strconv.Atoi(tokens[0])
+ if err != nil {
+ log.Fatalf("Attack %s has no valid Damage value\n", attackStr)
+ }
+
+ r := 1
+ if len(tokens) == 3 {
+ r, err = strconv.Atoi(tokens[2])
+ if err != nil {
+ log.Fatalf("Attack %s has no valid Range value\n", attackStr)
+ }
+ }
+ return Attack{a, r}
+}