-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
173 lines (156 loc) · 3.95 KB
/
test.py
File metadata and controls
173 lines (156 loc) · 3.95 KB
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
171
172
173
import pytest
from game import *
import numpy as np
def test_corners():
shape = np.array(
[
[0, 1, 0],
[1, 1, 1],
[0, 1, 0],
]
)
piece = PieceRotation(shape)
assert piece.corners == [
{(0, 1), (1, 0)}, # (-1, -1) corners
{(0, 1), (1, 2)}, # (-1, 1) corners
{(1, 0), (2, 1)}, # (1, -1) corners
{(1, 2), (2, 1)}, # (1, 1) corners
]
shape = np.array(
[
[0, 1, 0],
[1, 1, 0],
[0, 1, 0],
]
)
piece = PieceRotation(shape)
assert piece.corners == [
{(0, 1), (1, 0)}, # (-1, -1) corners
{(0, 1)}, # (-1, 1) corners
{(1, 0), (2, 1)}, # (1, -1) corners
{(2, 1)}, # (1, 1) corners
]
shape = np.array(
[
[1, 1],
[1, 1],
]
)
piece = PieceRotation(shape)
assert piece.corners == [
{(0, 0)}, # (-1, -1) corners
{(0, 1)}, # (-1, 1) corners
{(1, 0)}, # (1, -1) corners
{(1, 1)}, # (1, 1) corners
]
def test_piece_rots():
shape = np.array(
[
[0, 1, 0],
[1, 1, 1],
[0, 1, 0],
]
) # No rotations or reflections
assert len(Piece(shape).transforms) == 1
shape = np.array(
[
[0, 1, 0],
[1, 1, 0],
[0, 1, 0],
]
) # 4 total rotations
assert len(Piece(shape).transforms) == 4
shape = np.array(
[
[0, 0, 1],
[1, 1, 1],
[0, 0, 0],
]
) # All 8 reflections and rotations
assert len(Piece(shape).transforms) == 8
def test_piece_placements():
initial_board = GameState()
shape = np.array(
[
[0, 1, 0],
[1, 1, 1],
[0, 1, 0],
]
)
piece = Piece(shape)
positions = initial_board.get_positions(0, piece)
assert all(len(i) == 0 for i in positions)
shape = np.array(
[
[1, 1],
[1, 0],
]
)
piece = Piece(shape)
positions = initial_board.get_positions(0, piece)
assert sum(len(i) for i in positions) == 3 # 3 possible placements
new_board = GameState()
new_board.board = 2 << 4 # Place a piece by player 1 at (1, 0)
shape = np.array(
[
[1, 1],
[1, 0],
]
)
piece = Piece(shape)
positions = new_board.get_positions(0, piece)
assert sum(len(i) for i in positions) == 1 # 1 possible placement
# find the position
for i, trans in enumerate(piece.transforms):
if len(positions[i]) == 0:
continue
else:
assert positions[i][0] == (0, 0)
# Place this piece
new_board.place(0, trans, positions[i][0])
assert (
new_board.debug_str(False)
== "01"
+ 18 * "-"
+ "\n"
+ "00"
+ 18 * "-"
+ "\n"
+ ("-" * 20 + "\n") * 18
)
def test_left_right_bitmasks():
state = GameState()
state.board = state.left_bitmask
assert state.debug_str(False) == ("-" + "X" * 19 + "\n") * 20
state.board = state.right_bitmask
assert state.debug_str(False) == ("X" * 19 + "-" + "\n") * 20
def test_neighbor_bitmasks():
state = GameState()
state.board = state.neighbor_bitmasks[0]
print(state.debug_str(False))
assert state.debug_str(False) == (
"-0-"
+ "-" * 17
+ "\n"
+ "0X0"
+ "-" * 17
+ "\n"
+ "-0-"
+ "-" * 17
+ "\n"
+ ("-" * 20 + "\n") * 17
)
state.board = state.neighbor_bitmasks[3]
print(state.debug_str(False))
assert state.debug_str(False) == (
"-3-"
+ "-" * 17
+ "\n"
+ "3X3"
+ "-" * 17
+ "\n"
+ "-3-"
+ "-" * 17
+ "\n"
+ ("-" * 20 + "\n") * 17
)