-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathboard_test.go
111 lines (87 loc) · 2.15 KB
/
board_test.go
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
package bugs
import (
"testing"
"github.com/stretchr/testify/assert"
)
func countMines(b *board) int {
count := 0
for y := 0; y < b.height; y++ {
for x := 0; x < b.width; x++ {
item := b.bugs[y][x]
if item.bug {
count++
}
}
}
return count
}
func TestBoard(t *testing.T) {
b := newBoard(10, 10)
assert.Equal(t, 10, b.width)
b.load(0)
assert.Equal(t, 10, len(b.bugs))
assert.Equal(t, 10, len(b.bugs[0]))
assert.Equal(t, 0, b.bugs[2][2].near)
assert.Equal(t, false, b.bugs[2][2].shown)
}
func TestBoard_load(t *testing.T) {
b := newBoard(10, 10)
b.load(4)
assert.Equal(t, 4, countMines(b))
}
func TestBoard_setMine(t *testing.T) {
b := newBoard(3, 3)
b.load(0)
b.setMine(1, 1)
assert.True(t, b.bugs[1][1].bug)
assert.Equal(t, 1, b.bugs[0][0].near)
assert.Equal(t, 1, b.bugs[0][1].near)
assert.Equal(t, 1, b.bugs[0][2].near)
assert.Equal(t, 1, b.bugs[1][0].near)
assert.Equal(t, 1, b.bugs[1][2].near)
assert.Equal(t, 1, b.bugs[2][0].near)
assert.Equal(t, 1, b.bugs[2][1].near)
assert.Equal(t, 1, b.bugs[2][2].near)
}
func TestBoard_setMines(t *testing.T) {
b := newBoard(3, 4)
b.load(0)
b.setMine(1, 1)
b.setMine(2, 1)
assert.Equal(t, 2, countMines(b))
assert.True(t, b.bugs[1][1].bug)
assert.True(t, b.bugs[1][2].bug)
assert.Equal(t, 1, b.bugs[0][0].near)
assert.Equal(t, 2, b.bugs[0][1].near)
assert.Equal(t, 2, b.bugs[0][2].near)
assert.Equal(t, 1, b.bugs[0][3].near)
assert.Equal(t, 1, b.bugs[1][0].near)
assert.Equal(t, 1, b.bugs[1][3].near)
assert.Equal(t, 1, b.bugs[2][0].near)
assert.Equal(t, 2, b.bugs[2][1].near)
assert.Equal(t, 2, b.bugs[2][2].near)
assert.Equal(t, 1, b.bugs[2][3].near)
}
func TestBoard_remaining(t *testing.T) {
b := newBoard(3, 3)
b.load(1)
assert.Equal(t, 1, b.remaining())
b.flag(0, 0)
assert.Equal(t, 0, b.remaining())
}
func TestBoard_reveal(t *testing.T) {
b := newBoard(3, 3)
b.load(0)
b.setMine(1, 1)
b.reveal(2, 1)
assert.True(t, b.bugs[1][2].shown)
}
func TestBoard_revealCascade(t *testing.T) {
b := newBoard(3, 4)
b.load(0)
b.setMine(1, 1)
b.reveal(3, 1)
assert.True(t, b.bugs[0][3].shown)
assert.True(t, b.bugs[1][3].shown)
assert.True(t, b.bugs[2][3].shown)
}