aboutsummaryrefslogtreecommitdiff
path: root/go/game/ai_test.go
blob: 08cef2cb7078c657b3cf30b2e5377b6f01cfd33b (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package game

import (
	"strings"
	"testing"
)

func TestGenerateGraph(t *testing.T) {
	mapDef := `map: |1-
 HST
 HSF
 TST
symbols:
  T: tower
  H: house
  F: farm
  S: street
`
	r := strings.NewReader(mapDef)
	m, _ := readMap(r)
	a := NewUnit(NewCard("base/archer"), m.TileAt(Position{0, 0}), nil)
	f := NewUnit(NewCard("misc/farmer"), m.TileAt(Position{2, 2}), nil)

	graph := m.generateMapGraphFor(a)
	p, err := findPathTo(graph, a, Position{1, 1})
	if err != nil {
		t.Fatal(err)
	}

	if p.Distance != 3 {
		t.Fatalf("Wrong distance %d", p.Distance)
	}

	paths := findPathsToUnits(graph, m, a, []*Unit{f})
	if len(paths) == 0 || paths[0] == nil {
		t.Fatal("No path found")
	}

	if paths[0].Distance != 3 {
		t.Fatalf("Wrong distance %d", p.Distance)
	}
}

func TestAngrySquidDiagonal(t *testing.T) {
	mapDef := `map: |1-
 WWWWWWW
 W     W
 WW   WW
 WWWWWWW
 WWWWWWW
 W     W
symbols:
  W: deep water
`
	r := strings.NewReader(mapDef)
	m, err := readMap(r)
	if m == nil || err != nil {
		t.Fatal("failed to parse map", m, err)
	}
	s := NewUnit(NewCard("kraken/angry_squid"), m.TileAt(Position{5, 0}), nil)
	f := NewUnit(NewCard("misc/farmer"), m.TileAt(Position{6, 5}), nil)

	graph := m.generateMapGraphFor(s)

	paths := findPathsToUnits(graph, m, s, []*Unit{f})
	if len(paths) == 0 || paths[0] == nil {
		t.Fatal("No path found")
	}
}

func TestClowFish(t *testing.T) {
	mapDef := `map: |1-
 WWWWW
 WWWWW
 WWWWW
 WWWWW
 WWWWW
symbols:
  W: deep water
`
	r := strings.NewReader(mapDef)
	m, _ := readMap(r)
	s := NewLocalState()
	s.SetMap(m)
	p1 := s.AddNewPlayer("kraken", NewDeck())
	p2 := s.AddNewPlayer("player", NewDeck())
	c := s.addNewUnit(NewCard("kraken/clownfish"), Position{2, 2}, p1)
	s.addNewUnit(NewCard("kraken/sailfish"), Position{1, 1}, p2)
	s.addNewUnit(NewCard("kraken/sailfish"), Position{3, 2}, p2)

	ai := NewUnitAI(s, c)
	if ai == nil {
		t.Fatal("No AI available for Bauernfeind")
	}

	ai.promptAction()
	a := <-ai.actions
	if a == nil {
		t.Fatal("No action reported")
	}

	var ma *MoveAction
	var ok bool
	if ma, ok = a.(*MoveAction); !ok {
		t.Fatal("AI did not move flee from enemies")
	}

	if tile, ok := ma.targets.Cur().sel[0].(*Tile); ok {
		if tile != m.TileAt(Position{1, 4}) {
			t.Fatalf("Unexpected clownfish move: expected tile(1,4), got: %s", tile)
		}
	} else {
		t.Fatal("Target is not a Tile")
	}
	ma.resolve(s)

	ai = NewUnitAI(s, c)
	ai.promptAction()
	a = <-ai.actions
	if a == nil {
		t.Fatal("No action reported")
	}
	if _, ok := a.(*FullAction); !ok {
		t.Fatal("Unthreatend AI did not use a full action")
	}
}

func TestBauernFeind(t *testing.T) {
	mapDef := `map: |1-
 WWW 
 W   
 WWW 
    
symbols:
  W: wall
`
	r := strings.NewReader(mapDef)
	m, _ := readMap(r)
	s := NewLocalState()
	s.SetMap(m)
	p1 := s.AddNewPlayer("tyrant", NewDeck())
	p2 := s.AddNewPlayer("player", NewDeck())
	b := s.addNewUnit(NewCard("tyrant/bauernfeind"), Position{1, 1}, p1)
	s.addNewUnit(NewCard("misc/farmer"), Position{1, 3}, p2)

	bAi := NewUnitAI(s, b)
	if bAi == nil {
		t.Fatal("No AI available for Bauernfeind")
	}

	bAi.promptAction()
	a := <-bAi.actions
	if a == nil {
		t.Fatal("No action reported")
	}

	var ma *MoveAction
	var ok bool
	if ma, ok = a.(*MoveAction); !ok {
		t.Fatal("AI not moving towards target")
	}

	if tile, ok := ma.targets.Cur().sel[0].(*Tile); ok {
		if tile != m.TileAt(Position{3, 2}) {
			t.Fatalf("Unexpected Bauernfeind move: expected tile(3,2), got: %s", tile)
		}
	} else {
		t.Fatal("Target is not a Tile")
	}
}