示例#1
0
 def test_from_dict(self):
     self.assertEqual(
         Piece.from_dict(
             {'value': 5}
         ),
         Piece(5)
     )
示例#2
0
    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))
示例#3
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))))
示例#4
0
 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
         )
     )
示例#5
0
 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
         )
     )
示例#6
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)
示例#7
0
    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)
示例#8
0
    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)
示例#9
0
    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)
示例#10
0
    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)
示例#11
0
 def test_to_dict(self):
     self.assertEqual(
         PickingMove(Piece(4)).to_dict(), {
             'type': 'PickingMove',
             'piece': {
                 'value': 4
             }
         })
示例#12
0
    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
示例#13
0
    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)
示例#14
0
    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
            )
示例#15
0
    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))))
示例#16
0
 def test_to_dict(self):
     self.assertEqual(
         Event(Player('bob'), PickingMove(Piece(2))).to_dict(), {
             'player': {
                 'name': 'bob'
             },
             'move': {
                 'type': 'PickingMove',
                 'piece': {
                     'value': 2
                 }
             }
         })
示例#17
0
 def check_win(self, x, y):
     # Check if we've just won a game, based on having added
     # a piece to position x, y
     for winning_sets in self.board.get_rows(x, y):
         pieces = []
         for _x, _y in winning_sets:
             last_piece = self.board.get(_x, _y)
             if last_piece is None:
                 break
             pieces.append(last_piece)
         if (
             len(pieces) == 4 and
             Piece.overlap(*pieces)
         ):
             return True
     return False
示例#18
0
 def __init_game(self):
     self.board = Board()
     for value in range(1 << 4):
         self.remaining_pieces.append(
             Piece(value)
         )
示例#19
0
 def from_dict(cls, in_dict):
     return cls(
         Piece.from_dict(in_dict['piece'])
     )
示例#20
0
 def test_creation(self):
     standard_piece = Piece.from_attributes()
     self.assertEqual(standard_piece.value, 0)
示例#21
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)
示例#22
0
 def test_to_dict(self):
     self.assertEqual(
         Piece(5).to_dict(),
         {'value': 5}
     )
示例#23
0
 def test_game_bad_player(self):
     game = self._init_game()
     with self.assertRaises(Exception):
         game.apply_event(Event(Player('sam'), PickingMove(Piece(1))))