aboutsummaryrefslogtreecommitdiff
path: root/go/game/range_test.go
blob: 959e5e8da24486a7f053715441cbeaf98fb6669d (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
package game

import (
	"slices"
	"strings"
	"testing"
)

func assertPosSliceEqual(is, exp []Position, t *testing.T) {
	if len(is) != len(exp) {
		t.Fatal("Unexpected number of calculated positions:", is, exp)
	}

	for _, p := range exp {
		if !slices.Contains(is, p) {
			t.Fatalf("Position %v not in calculated positions", p)
		}
	}
}

func TestPositionsInRange(t *testing.T) {
	origin := Position{0, 0}
	positionsInRange1 := []Position{{0, 1}, {1, 0}, {1, 1}}
	calculatedPositions := PositionsInRange(origin, 1, false)
	assertPosSliceEqual(calculatedPositions, positionsInRange1, t)

	origin = Position{1, 0}
	positionsInRange1 = []Position{{0, 0}, {0, 1}, {1, 1}, {2, 0}, {2, 1}}
	calculatedPositions = PositionsInRange(origin, 1, false)
	assertPosSliceEqual(calculatedPositions, positionsInRange1, t)

	origin = Position{0, 2}
	positionsInRange1 = []Position{{0, 1}, {1, 1}, {1, 2}, {0, 3}, {1, 3}}
	calculatedPositions = PositionsInRange(origin, 1, false)
	assertPosSliceEqual(calculatedPositions, positionsInRange1, t)

	origin = Position{2, 2}
	positionsInRange2 := []Position{
		{0, 1},
		{0, 2},
		{0, 3},
		{1, 0},
		{1, 1},
		{1, 2},
		{1, 3},
		{1, 4},
		{2, 0},
		{2, 1},
		{2, 3},
		{2, 4},
		{3, 0},
		{3, 1},
		{3, 2},
		{3, 3},
		{3, 4},
		{4, 1},
		{4, 2},
		{4, 3},
	}
	calculatedPositions = PositionsInRange(origin, 2, false)
	assertPosSliceEqual(calculatedPositions, positionsInRange2, t)
}

func TestPositionNotInRange(t *testing.T) {
	o := Position{5, 0}
	p := Position{6, 3}
	if IsPositionInRange(o, p, 2) {
		t.Fatalf("Invalid range inclusion %v not in %v", o, PositionsInRange(o, 2, false))
	}
}

func TestDistanceBetweenPos(t *testing.T) {
	o := Position{0, 0}
	p := Position{2, 0}
	d := DistanceBetweenPositions(o, p)
	d2 := DistanceBetweenPositions(p, o)
	if d != 2 {
		t.Fatal("Incorrect distance", d)
	}
	if d != d2 {
		t.Fatal("Distance direction discrepance", d, d2)
	}
}

func TestDistanceBetweenPerms(t *testing.T) {
	mapDef := `map: |+1
 H T
symbols:
  T: tower
  H: house
  S: street
`
	s := NewLocalState()
	r := strings.NewReader(mapDef)
	m, _ := readMap(r)
	s.SetMap(m)

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

	p1 := s.addNewUnit(NewCard("base/fighter"), Position{0, 0}, o)
	p2 := s.addNewUnit(NewCard("base/fighter"), Position{2, 0}, p)

	d := DistanceBetweenPermanents(p1, p2)
	d2 := DistanceBetweenPermanents(p2, p1)
	if d != 2 {
		t.Fatal("Incorrect distance", d)
	}
	if d2 != 2 {
		t.Fatal("Incorrect distance", d2)
	}
}