def test_game_good_move(self): game = self._init_game() game.apply_event(Event(Player('bob'), PickingMove(Piece(0)))) self.assertEqual(game.turn, 1) self.assertEqual(game.next_piece, Piece(0))
def test_game_pick_taken(self): game = self._init_game() game.apply_event(Event(Player('bob'), PickingMove(Piece(0)))) game.apply_event(Event(Player('sam'), PlacementMove(0, 0))) with self.assertRaises(Exception): game.apply_event(Event(Player('sam'), PickingMove(Piece(0))))
def test_check_no_win_horizontal(self): game = Game() a = Piece.from_attributes(True, True, False, True) b = Piece.from_attributes(True, False, False, True) c = Piece.from_attributes(False, True, False, False) d = Piece.from_attributes(True, False, True, False) game.board.put(a, 0, 0) game.board.put(b, 1, 0) game.board.put(c, 2, 0) game.board.put(d, 3, 0) self.assertEqual(game.check_win(0, 0), False)
def test_check_win_negative_diagonal(self): game = Game() a = Piece.from_attributes(True, True, False, True) b = Piece.from_attributes(True, False, False, True) c = Piece.from_attributes(True, True, False, False) d = Piece.from_attributes(True, False, True, False) game.board.put(a, 0, 3) game.board.put(b, 1, 2) game.board.put(c, 2, 1) game.board.put(d, 3, 0) self.assertEqual(game.check_win(0, 3), True)
def test_game_2nd_pick_good(self): game = self._init_game() game.apply_event(Event(Player('bob'), PickingMove(Piece(0)))) game.apply_event(Event(Player('sam'), PlacementMove(0, 0))) game.apply_event(Event(Player('sam'), PickingMove(Piece(1)))) self.assertEqual(game.turn, 3) self.assertEqual(game.next_piece.value, 1)
def test_check_win_vertical(self): game = Game() a = Piece.from_attributes(True, True, False, True) b = Piece.from_attributes(True, False, False, True) c = Piece.from_attributes(True, True, False, False) d = Piece.from_attributes(True, False, True, False) game.board.put(a, 0, 0) game.board.put(b, 0, 1) game.board.put(c, 0, 2) game.board.put(d, 0, 3) self.assertEqual(game.check_win(0, 0), True)
def test_creation_with_positive_attributes(self): standard_piece = Piece.from_attributes(True) self.assertEqual(standard_piece.value, 1) standard_piece = Piece.from_attributes(True, True) self.assertEqual(standard_piece.value, 3) standard_piece = Piece.from_attributes(True, True, True, True) self.assertEqual(standard_piece.value, 15) standard_piece = Piece.from_attributes(True, False, True, True) self.assertEqual(standard_piece.value, 13) standard_piece = Piece.from_attributes(True, False, False, True) self.assertEqual(standard_piece.value, 9)
def test_full_overlap(self): pieces = [] for black in (True, False): for tall in (True, False): for hole in (True, False): for square in (True, False): pieces.append( Piece.from_attributes(black, tall, hole, square)) for piece in pieces: overlaps = 0 for other_piece in pieces: if piece == other_piece: continue if Piece.overlap(piece, other_piece): overlaps += 1 self.assertEqual(overlaps, 14)
def test_to_dict(self): self.assertEqual( PickingMove(Piece(4)).to_dict(), { 'type': 'PickingMove', 'piece': { 'value': 4 } })
def picking_move(self, game_uuid, player_name, number): game = self.load_game(game_uuid) player = Player(player_name) piece = Piece(number) move = PickingMove(piece) self.apply_event(game, player, move) self._store_game(game, game_uuid) return game
def test_game_good_placement(self): game = self._init_game() game.apply_event(Event(Player('bob'), PickingMove(Piece(0)))) game.apply_event(Event(Player('sam'), PlacementMove(0, 0))) self.assertEqual(game.turn, 2) self.assertEqual(game.next_piece, None)
def test_game_victory_for_bob(self): game = self._init_game() game.apply_event(Event(Player('bob'), PickingMove(Piece(0)))) game.apply_event(Event(Player('sam'), PlacementMove(0, 0))) game.apply_event(Event(Player('sam'), PickingMove(Piece(1)))) game.apply_event(Event(Player('bob'), PlacementMove(0, 1))) game.apply_event(Event(Player('bob'), PickingMove(Piece(2)))) game.apply_event(Event(Player('sam'), PlacementMove(0, 2))) game.apply_event(Event(Player('sam'), PickingMove(Piece(3)))) self.assertEqual(game.winner, None) game.apply_event(Event(Player('bob'), PlacementMove(0, 3))) self.assertEqual(game.winner, Player('bob')) with self.assertRaises(Exception): game.apply_event(Event(Player('bob'), PickingMove(Piece(4))))
def test_to_dict(self): self.assertEqual( Event(Player('bob'), PickingMove(Piece(2))).to_dict(), { 'player': { 'name': 'bob' }, 'move': { 'type': 'PickingMove', 'piece': { 'value': 2 } } })
def test_game_bad_player(self): game = self._init_game() with self.assertRaises(Exception): game.apply_event(Event(Player('sam'), PickingMove(Piece(1))))
def test_from_dict(self): self.assertEqual(Piece.from_dict({'value': 5}), Piece(5))
def test_to_dict(self): self.assertEqual(Piece(5).to_dict(), {'value': 5})
def test_no_overlap(self): a = Piece.from_attributes(True, True, False, True) b = Piece.from_attributes(True, False, True, True) c = Piece.from_attributes(False, True, False, False) d = Piece.from_attributes(True, False, False, False) self.assertFalse(Piece.overlap(a, b, c, d))
def test_piece_overlap(self): standard_piece = Piece.from_attributes() opposite_piece = Piece.from_attributes(True, True, False, True) self.assertTrue(Piece.overlap(standard_piece, opposite_piece))
def test_creation(self): standard_piece = Piece.from_attributes() self.assertEqual(standard_piece.value, 0)
def test_put_simple(self): board = Board(2) piece = Piece.from_attributes(True, False) board.put(piece, 1, 1) with self.assertRaises(Exception): board.put(piece, 1, 1)