glyph for return
[naja.git] / naja / tests / test_player.py
1 from unittest import TestCase
2
3 from naja.constants import BITS, MOVES
4 from naja.player import PlayerBits, Player
5 from naja.gameboard import GameBoard
6
7
8 class TestPlayerBits(TestCase):
9     def test_range_limits(self):
10         bits = PlayerBits(0x00)
11
12         def set_value(value):
13             bits.bits = value
14
15         self.assertEqual(bits.bits, 0x00)
16         self.assertRaises(AssertionError, set_value, -1)
17         self.assertEqual(bits.bits, 0x00)
18         bits.bits = 255
19         self.assertEqual(bits.bits, 0xff)
20         bits.bits = 0
21         self.assertEqual(bits.bits, 0x00)
22         self.assertRaises(AssertionError, set_value, 256)
23         self.assertEqual(bits.bits, 0x00)
24
25     def test_check_bit(self):
26         bits = PlayerBits(0x01)
27         self.assertEqual(bits.check_bit(BITS.NORTH), True)
28         self.assertEqual(bits.check_bit(BITS.SOUTH), False)
29
30     def test_set_bit(self):
31         bits = PlayerBits(0x00)
32         self.assertEqual(bits._bits, 0x00)
33         bits.set_bit(BITS.NORTH)
34         self.assertEqual(bits._bits, 0x01)
35         bits.set_bit(BITS.NORTH)
36         self.assertEqual(bits._bits, 0x01)
37
38     def test_clear_bit(self):
39         bits = PlayerBits(0x01)
40         self.assertEqual(bits._bits, 0x01)
41         bits.clear_bit(BITS.NORTH)
42         self.assertEqual(bits._bits, 0x00)
43         bits.clear_bit(BITS.NORTH)
44         self.assertEqual(bits._bits, 0x00)
45
46     def test_toggle_bit(self):
47         bits = PlayerBits(0x00)
48         self.assertEqual(bits._bits, 0x00)
49         bits.toggle_bit(BITS.NORTH)
50         self.assertEqual(bits._bits, 0x01)
51         bits.toggle_bit(BITS.NORTH)
52         self.assertEqual(bits._bits, 0x00)
53
54     def test_check_bits(self):
55         bits = PlayerBits(0x03)
56         self.assertEqual(bits.check_bits([BITS.NORTH]), True)
57         self.assertEqual(bits.check_bits([BITS.NORTH, BITS.SOUTH]), True)
58         self.assertEqual(bits.check_bits([BITS.BLUE]), False)
59         self.assertEqual(bits.check_bits([BITS.BLUE, BITS.MSB]), False)
60         self.assertEqual(
61             bits.check_bits([BITS.NORTH, BITS.SOUTH, BITS.BLUE]), False)
62
63     def test_set_bits(self):
64         bits = PlayerBits(0x03)
65         self.assertEqual(bits._bits, 0x03)
66         bits.set_bits([BITS.NORTH, BITS.BLUE])
67         self.assertEqual(bits._bits, 0x13)
68         bits.set_bits([BITS.NORTH, BITS.BLUE, BITS.MSB])
69         self.assertEqual(bits._bits, 0x93)
70
71     def test_clear_bits(self):
72         bits = PlayerBits(0x03)
73         self.assertEqual(bits._bits, 0x03)
74         bits.clear_bits([BITS.NORTH, BITS.BLUE])
75         self.assertEqual(bits._bits, 0x02)
76         bits.clear_bits([BITS.NORTH, BITS.BLUE, BITS.MSB])
77         self.assertEqual(bits._bits, 0x02)
78
79     def test_toggle_bits(self):
80         bits = PlayerBits(0x03)
81         self.assertEqual(bits._bits, 0x03)
82         bits.toggle_bits([BITS.NORTH, BITS.BLUE])
83         self.assertEqual(bits._bits, 0x12)
84         bits.toggle_bits([BITS.NORTH, BITS.BLUE])
85         self.assertEqual(bits._bits, 0x03)
86         bits.toggle_bits([BITS.NORTH, BITS.BLUE, BITS.MSB])
87         self.assertEqual(bits._bits, 0x92)
88
89     def test_shift_bits_left(self):
90         bits = PlayerBits(0x03)
91         self.assertEqual(bits._bits, 0x03)
92         bits.shift_bits_left(1)
93         self.assertEqual(bits._bits, 0x06)
94         bits.shift_bits_left(6)
95         self.assertEqual(bits._bits, 0x81)
96
97     def test_shift_bits_right(self):
98         bits = PlayerBits(0x06)
99         self.assertEqual(bits._bits, 0x06)
100         bits.shift_bits_right(1)
101         self.assertEqual(bits._bits, 0x03)
102         bits.shift_bits_right(1)
103         self.assertEqual(bits._bits, 0x81)
104
105
106 class TestPlayer(TestCase):
107
108     def setUp(self):
109         self.board = GameBoard.new_game({'cards': [
110             {'card_name': 'card1', 'actions': [
111                 {
112                     'action_class': 'LoseHealthOrMSB',
113                     'required_bits': [],
114                 }, {
115                     'action_class': 'GainHealth',
116                     'required_bits': [BITS.RED],
117                 },
118             ]}
119         ]})
120
121     def test_new_player(self):
122         player = Player(0x0f, (0, 1), self.board)
123         self.assertEqual(player.bits.bits, 0x0f)
124         self.assertEqual(player.position, (0, 1))
125
126     def test_import_player(self):
127         player = Player.import_player({
128             'bits': 0xaa,
129             'position': [1, 2],
130             'movement_mode': MOVES.CASTLE,
131         })
132         self.assertEqual(player.bits.bits, 0xaa)
133         self.assertEqual(player.position, (1, 2))
134         self.assertEqual(player.movement_mode, MOVES.CASTLE)
135
136     def test_export_player(self):
137         player = Player(0xaa, (1, 2), MOVES.CASTLE)
138         self.assertEqual(player.export(), {
139             'bits': 0xaa,
140             'position': [1, 2],
141             'movement_mode': MOVES.CASTLE,
142         })
143
144     def test_export_player_default_movement_mode(self):
145         player = Player(0xaa, (1, 2))
146         self.assertEqual(player.export(), {
147             'bits': 0xaa,
148             'position': [1, 2],
149             'movement_mode': MOVES.ADJACENT,
150         })
151
152     def test_legal_moves_all_available(self):
153         player = Player(0x0f, (2, 2), gameboard=self.board)
154         self.assertEqual(
155             player.legal_moves(), [(2, 2), (2, 1), (2, 3), (3, 2), (1, 2)])
156
157     def test_legal_moves_some_unavailable(self):
158         player = Player(0x0f, (0, 2), gameboard=self.board)
159         player.bits.clear_bit(BITS.NORTH)
160         self.assertEqual(player.legal_moves(), [(0, 2), (0, 3), (1, 2)])
161
162     def test_legal_moves_castle(self):
163         player = Player(0x0f, (1, 3), MOVES.CASTLE, gameboard=self.board)
164         self.assertEqual(player.legal_moves(), [
165             (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 3), (3, 3),
166             (4, 3)])
167
168     def test_legal_moves_bishop(self):
169         player = Player(0x0f, (1, 3), MOVES.BISHOP, gameboard=self.board)
170         self.assertEqual(player.legal_moves(), [
171             (0, 2), (0, 4), (1, 3), (2, 2), (2, 4), (3, 1), (4, 0)])
172
173     def test_legal_moves_knight(self):
174         player = Player(0x0f, (1, 3), MOVES.KNIGHT, gameboard=self.board)
175         self.assertEqual(player.legal_moves(), [
176             (0, 1), (1, 3), (2, 1), (3, 2), (3, 4)])
177
178     def test_set_position(self):
179         player = Player(0x0f, (3, 3), MOVES.BISHOP, gameboard=self.board)
180         player.set_position((4, 4))
181         self.assertEqual(player.movement_mode, MOVES.ADJACENT)