rotation action
[naja.git] / naja / tests / test_gameboard.py
1 from unittest import TestCase
2
3 from naja.constants import BITS, MOVES
4 from naja.gameboard import GameBoard, LocationCard
5 from naja import actions
6
7
8 class TestGameBoard(TestCase):
9     def assert_state(self, state1, state2, exclude=(), player_exclude=()):
10         def filter_dict(source, exclude_keys):
11             return dict((k, v) for k, v in source.items()
12                         if k not in exclude_keys)
13
14         state1 = filter_dict(state1, exclude)
15         if 'player' in state1:
16             state1['player'] = filter_dict(state1['player'], player_exclude)
17         state2 = filter_dict(state2, exclude)
18         if 'player' in state2:
19             state2['player'] = filter_dict(state2['player'], player_exclude)
20
21         self.assertEqual(state1, state2)
22
23     def test_export_new_board(self):
24         board = GameBoard.new_game([{'actions': [{
25             'action_class': 'LoseHealthOrMSB',
26             'required_bits': [],
27         }]}])
28         exported_state = board.export()
29         board_locations = exported_state.pop('board_locations')
30         self.assertEqual(exported_state, {
31             'max_health': 4,
32             'health': 4,
33             'wins_required': 4,
34             'wins': 0,
35             'locations': [{'actions': [{
36                 'action_class': 'LoseHealthOrMSB',
37                 'required_bits': [],
38             }]}],
39             'player': board.player.export(),
40         })
41         self.assertEqual(
42             set(board_locations.keys()),
43             set((x, y) for x in range(5) for y in range(5)))
44         for location_state in board_locations.values():
45             self.assertEqual(
46                 sorted(location_state.keys()), ['actions', 'bitwise_operand'])
47             self.assertEqual(location_state['actions'], [{
48                 'action_class': 'LoseHealthOrMSB',
49                 'required_bits': [],
50                 'data': {},
51             }])
52             self.assertTrue(2 <= len(location_state['bitwise_operand']) <= 3)
53
54     def test_lose_health(self):
55         board = GameBoard.new_game([{'actions': []}])
56         self.assertEqual(board.health, 4)
57         state_1 = board.export()
58
59         board.lose_health()
60         self.assertEqual(board.health, 3)
61         state_2 = board.export()
62
63         self.assert_state(state_1, state_2, exclude=['health'])
64
65     def test_gain_health(self):
66         board = GameBoard.new_game([{'actions': []}])
67         board.health = 2
68         self.assertEqual(board.health, 2)
69         state_1 = board.export()
70
71         board.gain_health()
72         self.assertEqual(board.health, 3)
73         state_2 = board.export()
74
75         self.assert_state(state_1, state_2, exclude=['health'])
76
77     def test_gain_health_at_max(self):
78         board = GameBoard.new_game([{'actions': []}])
79         self.assertEqual(board.health, 4)
80         state_1 = board.export()
81
82         board.gain_health()
83         self.assertEqual(board.health, 4)
84         state_2 = board.export()
85
86         self.assert_state(state_1, state_2)
87
88     def generate_locations(self, override_dict=None):
89         locations_dict = dict(((x, y), '%s%s' % (x, y))
90                               for x in range(5) for y in range(5))
91         if override_dict:
92             locations_dict.update(override_dict)
93         return locations_dict
94
95     def test_shift_locations_north(self):
96         board = GameBoard.new_game([{'actions': []}])
97         board.board_locations = self.generate_locations()
98         board.shift_locations('NORTH')
99         self.assertEqual(board.board_locations, self.generate_locations({
100             (2, 0): '21', (2, 1): '23', (2, 3): '24', (2, 4): '20',
101         }))
102
103     def test_shift_locations_south(self):
104         board = GameBoard.new_game([{'actions': []}])
105         board.board_locations = self.generate_locations()
106         board.shift_locations('SOUTH')
107         self.assertEqual(board.board_locations, self.generate_locations({
108             (2, 0): '24', (2, 1): '20', (2, 3): '21', (2, 4): '23',
109         }))
110
111     def test_shift_locations_east(self):
112         board = GameBoard.new_game([{'actions': []}])
113         board.board_locations = self.generate_locations()
114         board.shift_locations('EAST')
115         self.assertEqual(board.board_locations, self.generate_locations({
116             (0, 2): '42', (1, 2): '02', (3, 2): '12', (4, 2): '32',
117         }))
118
119     def test_shift_locations_west(self):
120         board = GameBoard.new_game([{'actions': []}])
121         board.board_locations = self.generate_locations()
122         board.shift_locations('WEST')
123         self.assertEqual(board.board_locations, self.generate_locations({
124             (0, 2): '12', (1, 2): '32', (3, 2): '42', (4, 2): '02',
125         }))
126
127     def test_rotate_locations_clockwise(self):
128         board = GameBoard.new_game([{'actions': []}])
129         board.board_locations = self.generate_locations()
130         board.rotate_locations('CLOCKWISE')
131         self.assertEqual(board.board_locations, self.generate_locations({
132             (1, 1): '21', (2, 1): '31', (3, 1): '32',
133             (1, 2): '11',               (3, 2): '33',
134             (1, 3): '12', (2, 3): '13', (3, 3): '23',
135         }))
136
137     def test_rotate_locations_clockwise_top(self):
138         board = GameBoard.new_game([{'actions': []}], initial_pos=(2, 0))
139         board.board_locations = self.generate_locations()
140         board.rotate_locations('CLOCKWISE')
141         self.assertEqual(board.board_locations, self.generate_locations({
142             (1, 0): '30',               (3, 0): '31',
143             (1, 1): '10', (2, 1): '11', (3, 1): '21',
144         }))
145
146     def test_rotate_locations_clockwise_right(self):
147         board = GameBoard.new_game([{'actions': []}], initial_pos=(0, 2))
148         board.board_locations = self.generate_locations()
149         board.rotate_locations('CLOCKWISE')
150         self.assertEqual(board.board_locations, self.generate_locations({
151             (0, 1): '11', (1, 1): '12',
152                           (1, 2): '13',
153             (0, 3): '01', (1, 3): '03',
154         }))
155
156     def test_rotate_locations_clockwise_corner(self):
157         board = GameBoard.new_game([{'actions': []}], initial_pos=(0, 4))
158         board.board_locations = self.generate_locations()
159         board.rotate_locations('CLOCKWISE')
160         self.assertEqual(board.board_locations, self.generate_locations({
161             (0, 3): '13', (1, 3): '14',
162                           (1, 4): '03',
163         }))
164
165     def test_rotate_locations_anticlockwise(self):
166         board = GameBoard.new_game([{'actions': []}])
167         board.board_locations = self.generate_locations()
168         board.rotate_locations('ANTICLOCKWISE')
169         self.assertEqual(board.board_locations, self.generate_locations({
170             (1, 1): '12', (2, 1): '11', (3, 1): '21',
171             (1, 2): '13',               (3, 2): '31',
172             (1, 3): '23', (2, 3): '33', (3, 3): '32',
173         }))
174
175     def test_allow_chess_move_knight(self):
176         board = GameBoard.new_game([{'actions': []}])
177         board.allow_chess_move(MOVES.KNIGHT)
178         self.assertEqual(board.player.movement_mode, MOVES.KNIGHT)
179
180     def test_allow_chess_move_bishop(self):
181         board = GameBoard.new_game([{'actions': []}])
182         board.allow_chess_move(MOVES.BISHOP)
183         self.assertEqual(board.player.movement_mode, MOVES.BISHOP)
184
185     def test_allow_chess_move_castle(self):
186         board = GameBoard.new_game([{'actions': []}])
187         board.allow_chess_move(MOVES.CASTLE)
188         self.assertEqual(board.player.movement_mode, MOVES.CASTLE)
189
190
191 class TestLocationCard(TestCase):
192     def test_generate_bitwise_operand(self):
193         # This is testing a random process, so it may fail occasionally.
194         operand_sets = []
195         for _ in range(100):
196             operand_sets.append(LocationCard.generate_bitwise_operand())
197         sizes = {2: 0, 3: 0}
198         bits = set()
199         for operand_set in operand_sets:
200             sizes[len(operand_set)] += 1
201             bits.update(operand_set)
202             # TODO: Test that there's at least one condition and one direction.
203         self.assertTrue(sizes[2] > 0)
204         self.assertTrue(sizes[3] > 0)
205         self.assertTrue(sizes[2] > sizes[3])
206         self.assertEqual(bits, set(BITS.values()))
207
208     def test_new_location_no_actions(self):
209         location = LocationCard.new_location({'actions': []})
210         [action] = location.actions
211         self.assertEqual(type(action), actions.DoNothing)
212         self.assertEqual(action.required_bits, set())
213
214     def test_new_location_one_action(self):
215         location = LocationCard.new_location({'actions': [
216             {'required_bits': [], 'action_class': 'DoNothing'},
217         ]})
218         [action] = location.actions
219         self.assertEqual(type(action), actions.DoNothing)
220         self.assertEqual(action.required_bits, set())
221
222     def test_parse_bits(self):
223         self.assertEqual(
224             LocationCard.parse_bits([]), frozenset([]))
225         self.assertEqual(
226             LocationCard.parse_bits(['RED']), frozenset([BITS.RED]))
227         self.assertEqual(
228             LocationCard.parse_bits([BITS.BLUE]), frozenset([BITS.BLUE]))
229         self.assertEqual(
230             LocationCard.parse_bits([BITS.NORTH, 'MSB']),
231             frozenset([BITS.NORTH, BITS.MSB]))