Exemplo n.º 1
0
class ReversiInitialBoardTest(unittest.TestCase):

    def setUp(self):
        self.game = Reversi()

    def test_initial_board(self):
        self.assertEqual(len(self.game.board), 64)
        self.assertEqual(self.game.board[Coord(0, 1)], ' ')
        self.assertEqual(self.game.board[Coord(3, 4)], self.game.BLACK)

    def test_valid_move(self):
        self.assertTrue(self.game.is_valid_move(Coord(4, 5)))
        self.assertTrue(self.game.is_valid_move(Coord(3, 2)))
        self.assertFalse(self.game.is_valid_move(Coord(3, 1)))
        self.assertFalse(self.game.is_valid_move(Coord(3, 3)))

    def test_is_enemy_disc(self):
        self.assertTrue(self.game.is_enemy_disc(Coord(3, 3)))
        self.assertFalse(self.game.is_enemy_disc(Coord(7, 0)))
        self.assertFalse(self.game.is_enemy_disc(Coord(4, 3)))

    def test_play(self):
        self.game.play(Coord(4, 5))
        self.assertEqual(self.game.board[Coord(4, 4)], self.game.BLACK)
        self.assertEqual(self.game.board[Coord(4, 5)], self.game.BLACK)

    def test_available_fields(self):
        fields = {Coord(4, 5), Coord(5, 4), Coord(3, 2), Coord(2, 3)}
        self.assertEqual(fields, set(self.game.available_fields()))

    def test_outcome(self):
        self.assertEqual(
            self.game.outcome(), self.game.GAME_STATES['IN_PROGRESS'])

    def test_outcome_after_play(self):
        self.game.play(Coord(5, 4))
        self.assertEqual(
            self.game.outcome(), self.game.GAME_STATES['IN_PROGRESS'])

    def test_players_result(self):
        self.assertEqual(self.game.black_player.result, 2)
        self.assertEqual(self.game.white_player.result, 2)

    def test_change_current_player(self):
        self.game.change_current_player()
        self.assertEqual(self.game.player.field, self.game.WHITE)
Exemplo n.º 2
0
class ReversiInitialBoardTest(unittest.TestCase):
    def setUp(self):
        self.game = Reversi()

    def test_initial_board(self):
        self.assertEqual(len(self.game.board), 64)
        self.assertEqual(self.game.board[Coord(0, 1)], ' ')
        self.assertEqual(self.game.board[Coord(3, 4)], self.game.BLACK)

    def test_valid_move(self):
        self.assertTrue(self.game.is_valid_move(Coord(4, 5)))
        self.assertTrue(self.game.is_valid_move(Coord(3, 2)))
        self.assertFalse(self.game.is_valid_move(Coord(3, 1)))
        self.assertFalse(self.game.is_valid_move(Coord(3, 3)))

    def test_is_enemy_disc(self):
        self.assertTrue(self.game.is_enemy_disc(Coord(3, 3)))
        self.assertFalse(self.game.is_enemy_disc(Coord(7, 0)))
        self.assertFalse(self.game.is_enemy_disc(Coord(4, 3)))

    def test_play(self):
        self.game.play(Coord(4, 5))
        self.assertEqual(self.game.board[Coord(4, 4)], self.game.BLACK)
        self.assertEqual(self.game.board[Coord(4, 5)], self.game.BLACK)

    def test_available_fields(self):
        fields = {Coord(4, 5), Coord(5, 4), Coord(3, 2), Coord(2, 3)}
        self.assertEqual(fields, set(self.game.available_fields()))

    def test_outcome(self):
        self.assertEqual(self.game.outcome(),
                         self.game.GAME_STATES['IN_PROGRESS'])

    def test_outcome_after_play(self):
        self.game.play(Coord(5, 4))
        self.assertEqual(self.game.outcome(),
                         self.game.GAME_STATES['IN_PROGRESS'])

    def test_players_result(self):
        self.assertEqual(self.game.black_player.result, 2)
        self.assertEqual(self.game.white_player.result, 2)

    def test_change_current_player(self):
        self.game.change_current_player()
        self.assertEqual(self.game.player.field, self.game.WHITE)
Exemplo n.º 3
0
class ReversiTest(unittest.TestCase):
    def setUp(self):
        # set board
        '''
        0   1   2   3   4   5   6   7
           │   │   │   │   │   │   │ O │0
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │   │   │   │   │ * │   │1
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │ O │   │   │ * │   │   │2
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │ * │ O │ * │   │   │   │3
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │   │ * │ O │   │   │   │4
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │ W │ * │ * │ O │   │   │5
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │ * │   │ * │   │   │   │   │6
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │   │   │ O │   │   │   │7
        ───┼───┼───┼───┼───┼───┼───┼───┼
        '''
        self.game = Reversi()
        self.game.board[Coord(0, 7)] = self.game.WHITE
        self.game.board[Coord(1, 6)] = self.game.BLACK
        self.game.board[Coord(2, 2)] = self.game.WHITE
        self.game.board[Coord(2, 5)] = self.game.BLACK

        self.game.board[Coord(3, 2)] = self.game.BLACK

        self.game.board[Coord(5, 3)] = self.game.BLACK
        self.game.board[Coord(5, 4)] = self.game.BLACK
        self.game.board[Coord(5, 5)] = self.game.WHITE

        self.game.board[Coord(6, 1)] = self.game.BLACK
        self.game.board[Coord(6, 3)] = self.game.BLACK
        self.game.board[Coord(7, 4)] = self.game.WHITE
        self.game.player = self.game.white_player

    def test_valid_move(self):
        self.assertTrue(self.game.is_valid_move(Coord(7, 3)))
        self.assertTrue(self.game.is_valid_move(Coord(4, 2)))
        self.assertTrue(self.game.is_valid_move(Coord(2, 4)))
        self.assertTrue(self.game.is_valid_move(Coord(6, 4)))
        self.assertTrue(self.game.is_valid_move(Coord(6, 2)))
        self.assertFalse(self.game.is_valid_move(Coord(6, 0)))

    def test_play_new_board(self):
        self.game.play(Coord(5, 2))
        # board after the turn
        '''
        0   1   2   3   4   5   6   7
           │   │   │   │   │   │   │ O │0
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │   │   │   │   │ O │   │1
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │ O │   │   │ O │   │   │2
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │ * │ O │ O │   │   │   │3
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │   │ O │ O │   │   │   │4
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │ O │ O │ O │ O │   │   │5
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │ * │   │ O │   │   │   │   │6
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │   │   │ O │   │   │   │7
        ───┼───┼───┼───┼───┼───┼───┼───┼
        '''
        new_board = self.game.board.copy()
        new_board[Coord(5, 2)] = self.game.WHITE
        new_board[Coord(1, 6)] = self.game.WHITE
        new_board[Coord(2, 5)] = self.game.WHITE
        new_board[Coord(4, 3)] = self.game.WHITE
        new_board[Coord(3, 4)] = self.game.WHITE
        new_board[Coord(6, 3)] = self.game.WHITE

        new_board[Coord(5, 3)] = self.game.WHITE
        new_board[Coord(5, 4)] = self.game.WHITE

        self.assertEqual(new_board, self.game.board)
        self.assertEqual(self.game.black_player.result, 2)
        self.assertEqual(self.game.white_player.result, 14)

    def test_available_fields(self):
        fields = {
            Coord(7, 3),
            Coord(4, 2),
            Coord(2, 4),
            Coord(6, 4),
            Coord(6, 2),
            Coord(5, 2),
            Coord(3, 1),
            Coord(3, 5)
        }
        self.assertEqual(fields, set(self.game.available_fields()))

    def test_outcome(self):
        self.assertEqual(self.game.outcome(),
                         self.game.GAME_STATES['IN_PROGRESS'])

    def test_outcome_after_play(self):
        self.game.play(Coord(2, 4))
        self.assertEqual(self.game.outcome(),
                         self.game.GAME_STATES['IN_PROGRESS'])

    def black_player_discs_test(self):
        self.assertEqual(len(self.game.black_player_discs()), 9)

    def white_player_discs_test(self):
        self.assertEqual(len(self.game.white_player_discs()), 6)
Exemplo n.º 4
0
class ReversiTest(unittest.TestCase):

    def setUp(self):
        # set board
        '''
        0   1   2   3   4   5   6   7
           │   │   │   │   │   │   │ O │0
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │   │   │   │   │ * │   │1
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │ O │   │   │ * │   │   │2
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │ * │ O │ * │   │   │   │3
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │   │ * │ O │   │   │   │4
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │ W │ * │ * │ O │   │   │5
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │ * │   │ * │   │   │   │   │6
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │   │   │ O │   │   │   │7
        ───┼───┼───┼───┼───┼───┼───┼───┼
        '''
        self.game = Reversi()
        self.game.board[Coord(0, 7)] = self.game.WHITE
        self.game.board[Coord(1, 6)] = self.game.BLACK
        self.game.board[Coord(2, 2)] = self.game.WHITE
        self.game.board[Coord(2, 5)] = self.game.BLACK

        self.game.board[Coord(3, 2)] = self.game.BLACK

        self.game.board[Coord(5, 3)] = self.game.BLACK
        self.game.board[Coord(5, 4)] = self.game.BLACK
        self.game.board[Coord(5, 5)] = self.game.WHITE

        self.game.board[Coord(6, 1)] = self.game.BLACK
        self.game.board[Coord(6, 3)] = self.game.BLACK
        self.game.board[Coord(7, 4)] = self.game.WHITE
        self.game.player = self.game.white_player

    def test_valid_move(self):
        self.assertTrue(self.game.is_valid_move(Coord(7, 3)))
        self.assertTrue(self.game.is_valid_move(Coord(4, 2)))
        self.assertTrue(self.game.is_valid_move(Coord(2, 4)))
        self.assertTrue(self.game.is_valid_move(Coord(6, 4)))
        self.assertTrue(self.game.is_valid_move(Coord(6, 2)))
        self.assertFalse(self.game.is_valid_move(Coord(6, 0)))

    def test_play_new_board(self):
        self.game.play(Coord(5, 2))
        # board after the turn
        '''
        0   1   2   3   4   5   6   7
           │   │   │   │   │   │   │ O │0
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │   │   │   │   │ O │   │1
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │ O │   │   │ O │   │   │2
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │ * │ O │ O │   │   │   │3
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │   │ O │ O │   │   │   │4
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │ O │ O │ O │ O │   │   │5
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │ * │   │ O │   │   │   │   │6
        ───┼───┼───┼───┼───┼───┼───┼───┼
           │   │   │   │ O │   │   │   │7
        ───┼───┼───┼───┼───┼───┼───┼───┼
        '''
        new_board = self.game.board.copy()
        new_board[Coord(5, 2)] = self.game.WHITE
        new_board[Coord(1, 6)] = self.game.WHITE
        new_board[Coord(2, 5)] = self.game.WHITE
        new_board[Coord(4, 3)] = self.game.WHITE
        new_board[Coord(3, 4)] = self.game.WHITE
        new_board[Coord(6, 3)] = self.game.WHITE

        new_board[Coord(5, 3)] = self.game.WHITE
        new_board[Coord(5, 4)] = self.game.WHITE

        self.assertEqual(new_board, self.game.board)
        self.assertEqual(self.game.black_player.result, 2)
        self.assertEqual(self.game.white_player.result, 14)

    def test_available_fields(self):
        fields = {Coord(7, 3), Coord(4, 2), Coord(2, 4), Coord(6, 4),
                  Coord(6, 2), Coord(5, 2), Coord(3, 1), Coord(3, 5)}
        self.assertEqual(fields, set(self.game.available_fields()))

    def test_outcome(self):
        self.assertEqual(
            self.game.outcome(), self.game.GAME_STATES['IN_PROGRESS'])

    def test_outcome_after_play(self):
        self.game.play(Coord(2, 4))
        self.assertEqual(
            self.game.outcome(), self.game.GAME_STATES['IN_PROGRESS'])

    def black_player_discs_test(self):
        self.assertEqual(len(self.game.black_player_discs()), 9)

    def white_player_discs_test(self):
        self.assertEqual(len(self.game.white_player_discs()), 6)