aboutsummaryrefslogtreecommitdiff
path: root/go/game/unit_test.go
blob: 26ef37006afc92048c5a2511e79ebcd56d78a26b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package game

import (
	"strings"
	"testing"
)

func TestCrew(t *testing.T) {
	mapDef := `map: |1-
 HST
 HSF
 TSW
symbols:
  T: tower
  H: house
  F: farm
  S: street
  W: deep water
`
	s := NewLocalState()
	r := strings.NewReader(mapDef)
	m, _ := readMap(r)
	s.SetMap(m)

	p := s.AddNewPlayer("player", NewDeck())

	f := NewUnit(NewCard("nautics/fisher"), s.Map().TileAt(Position{2, 2}), p)
	s.addPermanent(f)

	a := NewUnit(NewCard("base/archer"), s.Map().TileAt(Position{1, 1}), p)
	s.addPermanent(a)

	sword := newEquipmentFromPath("base/sword", s.Map().TileAt(Position{0, 1}), p)
	s.addPermanent(sword)

	movePermanent(a, f.Tile())
	if len(f.Pile()) != 1 {
		t.Fatalf("fisher's pile size is not 1")
	}

	if f.Pile()[0] != a {
		t.Fatalf("fisher's pile does not contain the archer")
	}

	if a.Tile() != nil {
		t.Fatalf("piled archer has still a tile %v", a.Tile())
	}

	for r, d := range f.Attack.attacks {
		if d != a.Attack.attacks[r] {
			t.Fatalf("fisher's attack %d is not equal to the archer's attack %d at range %d",
				d, a.Attack.attacks[r], r)
		}
	}

	if INVALID_ATTACK(f.Attack) {
		t.Fatalf("fisher has an invalid attack")
	}

	if a.Attackable() {
		t.Fatalf("Piled archer is attackable")
	}

	if len(a.AttackableTiles()) != 0 {
		t.Fatalf("Piled archer has attackable tiles")
	}

	movePermanent(a, s.Map().TileAt(Position{1, 1}))
	if len(f.Pile()) != 0 {
		t.Fatalf("fisher's pile is not empty")
	}

	if !INVALID_ATTACK(f.Attack) {
		t.Fatalf("fisher has still a valid atack")
	}

	addPermanentToPile(a, sword)
	if a.Attack.attacks[0] != 2 {
		t.Fatalf("archer's mele attack is not 2")
	}
}

func TestAttackablePerms(t *testing.T) {
	mapDef := `map: |1-
 HST
 HSF
 TSW
symbols:
  T: tower
  H: house
  F: farm
  S: street
  W: deep water
`
	s := NewLocalState()
	r := strings.NewReader(mapDef)
	m, _ := readMap(r)
	s.SetMap(m)

	p := s.AddNewPlayer("player", NewDeck())
	o := s.AddNewPlayer("opponent", NewDeck())

	a := s.addNewUnit(NewCard("base/archer"), Position{2, 0}, p)
	units := []*Unit{}
	for x := range 3 {
		for y := range 3 {
			var u *Unit
			pos := Position{x, y}
			if x == 2 && y == 0 {
				continue
			} else if x == 2 && y == 2 {
				u = s.addNewUnit(NewCard("nautics/fisher"), pos, o)
			} else {
				u = s.addNewUnit(NewCard("misc/farmer"), pos, o)
			}
			units = append(units, u)
		}
	}

	aTiles := a.AttackableTiles()
	is := len(aTiles)
	if is != 6 {
		t.Fatal("6 attackable tiles expected not:", is)
	}

	aPerms := a.AttackableEnemyPermanents()
	is = len(aPerms)
	if is != 6 {
		t.Fatal("6 attackable permanents expected not:", is)
	}
}

func TestKnightArmor(t *testing.T) {
	a := NewUnit(NewCard("base/archer"), NewMockTile(), NewMockPlayer())
	k := NewUnit(NewCard("base/knight"), NewMockTile(), NewMockPlayer())
	DealDamage(a, k, a.Attack.DamageInRange(2))
	if k.Damage() != 0 {
		t.Fatal("knight received damage from attack 1")
	}
}