-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_board.py
executable file
·193 lines (157 loc) · 5.9 KB
/
test_board.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# -*- coding: utf-8 -*-
import unittest
import logging
from board import Board
from piece import Piece
#logging.basicConfig(level=logging.DEBUG)
class DummyPiece(Piece):
def __init__(self, height, width, position=None, chargeable=True, transformable=False):
desc = {'name': 'DummyPiece',
'height': str(height),
'width': str(width)
}
super(DummyPiece, self).__init__(desc)
self.chargeable = chargeable
self.transformable = transformable
self.position = position
self._multichargeable = False
def chargingRegion(self):
if self.chargeable:
return (2, self.size[1])
else:
return (0, 0)
def transformingRegion(self):
if self.transformable:
return (1, 2)
else:
return (0, 0)
def canTransform(self, other):
return (self.size == (1,1) and other.size == (1,1))
def canCharge(self, other):
return self.chargeable
def charge(self):
if self.size == (1, 1):
dumb = DummyPiece(3, 1, position=self.position)
dumb.slidePriority = 1
if self.size == (2, 1):
dumb = DummyPiece(2, 1, position=self.position)
dumb.slidePriorit = 2
if self.size == (2, 2):
dumb = DummyPiece(2, 2, position=self.position)
dumb.slidePriority = 3
return dumb
def transform(self):
ret = DummyPiece(self.size[0], self.size[1], position = self.position, transformable=False)
ret.slidePriority = 1000
ret.chargeable = False
return ret
class TestBoard(unittest.TestCase):
#@unittest.skip("")
def testTransform(self):
b = Board(6, 8)
pieces = [DummyPiece(1, 1, transformable = True) for c in range(4)]
for c in range(4):
b.addPiece(pieces[c], c)
transformedPieces = []
def transformHandler(p): transformedPieces.append(p)
b.wallMade.addHandler(transformHandler)
b.normalize()
self.assertEqual(len(transformedPieces[0][0]), 4)
#-- piece update test.
#@unittest.skip("")
def testPieceUpdate(self):
b = Board(3, 3)
piece1 = DummyPiece(1, 1)
piece2 = DummyPiece(1, 1)
piece3 = DummyPiece(1, 1)
piece3.slidePriority = 5
updates = [None]
def updateHandler(p): updates[0] = p
b.pieceUpdated.addHandler(updateHandler)
b.addPiece(piece1, 0)
b.normalize()
self.assertEqual(updates[0], set([piece1]))
b.addPiece(piece2, 1)
b.normalize()
self.assertEqual(updates[0], set([piece2]))
b.addPiece(piece3, 0)
b.normalize()
self.assertEqual(updates[0], set([piece1, piece3]))
b.deletePiece(piece1)
self.assertEqual(updates[0], set([piece1]))
#-- addPiece onto a full column
#@unittest.skip("")
def testAddPiece(self):
b = Board(2, 3)
b.addPiece(DummyPiece(1,1), 1)
b.addPiece(DummyPiece(1,1), 1)
with self.assertRaises(IndexError):
b.addPiece(DummyPiece(1,1), 1)
#@unittest.skip("")
def testSlideFatty(self):
b = Board(4, 4)
#add two small pieces in front of fatty
piece1 = DummyPiece(1, 1, chargeable = False)
piece2 = DummyPiece(1, 1, chargeable = False)
fatpiece = DummyPiece(2, 2, chargeable = False)
fatpiece.name = "fatty"
b.addPiece(piece1, 0)
b.addPiece(piece2, 0)
b.addPiece(fatpiece, 0)
b.normalize()
#the two squares (0,1) and (1,1) in front of fatty should be empty
self.assertEqual(b[0,1], None)
self.assertEqual(b[1,1], None)
#fatty should be at position (2, 0)
self.assertEqual(fatpiece.position, [2,0])
self.assertTrue(b.selfConsistent)
#now make fatty slide in front
fatpiece.slidePriority = 5
b.normalize()
#fatty should occupy the (0,0) position
self.assertEqual(fatpiece.position,[0,0])
self.assertTrue(b.selfConsistent)
#@unittest.skip("")
def testSlidePairFatty(self):
b = Board(4,3)
#add a fatty to column 1 and another to column 0
fat1 = DummyPiece(2,2)
fat1.name = "fat right"
fat0 = DummyPiece(2,2)
fat0.name = "fat left"
fat0.slidePriority = 0
fat1.slidePriority = 0
b.addPiece(fat1, 1) #fat right is added first
b.addPiece(fat0, 0)
self.assertTrue(b.selfConsistent())
b.normalize()
#fat right (fat1) should be below fat left (fat0)
self.assertEqual(fat0.position, [2,0])
self.assertEqual(fat1.position, [0,1])
self.assertTrue(b.selfConsistent)
##make fat0 have priority
logging.debug("priority is about to be changed")
fat0.slidePriority = 5
b.normalize()
##fat0 should now be below fat1
self.assertEqual(fat0.position, [0,0])
self.assertEqual(fat1.position, [2,1])
self.assertTrue(b.selfConsistent)
def testColToAdd(self):
# We create a variety of boards in which there is only one place
# to add a new piece. Then we check that colToAdd puts the piece
# in the correct place.
b = Board(2, 4)
b.addPiece(DummyPiece(1, 1), 0)
b.addPiece(DummyPiece(1, 1), 1)
self.assertEqual(b.colToAdd(DummyPiece(2, 2)), 2)
b = Board(2, 4)
b.addPiece(DummyPiece(1, 1), 1)
self.assertEqual(b.colToAdd(DummyPiece(2, 2)), 2)
b = Board(2, 4)
b.addPiece(DummyPiece(1, 1), 2)
self.assertEqual(b.colToAdd(DummyPiece(2, 2)), 0)
b.addPiece(DummyPiece(1, 1), 1)
self.assertEqual(b.colToAdd(DummyPiece(2, 2)), None)
if __name__ == '__main__':
unittest.main()