Esempio n. 1
0
class AIHelper():
    MAX_PLAYER = 'w'
    MIN_PLAYER = 'b'
    INFINITY = 1.0e+10
    """
    Helper interface class for the AI
        $1. available moves (board, player)
        $2. get_resulting_board -> (board, player, coord)
        $3. player pools (board, player)
        $4. check if game has ended (board)
    """

    # it is created when the game starts
    def __init__(self, board=None):
        self.game = Reversi()
        if board:
            self.set_board(board)

    # changes to board form ai to game board
    def set_board(self, board):
        self.game.board = FormatConverter.ai_to_game_board(board)

    # sets a player
    def set_player(self, player):
        self.game.player = Player(player)

    # finding available moves
    def available_moves(self, board, player):
        self.set_board(board)
        self.set_player(player)
        return self.game.available_fields()

    # gets the changes of the human player
    def get_resulting_board(self, board, player, coord):
        self.set_board(board)
        self.set_player(player)
        self.game.play(coord)
        return FormatConverter.game_to_ai_board(self.game.board)

    def player_pool(self, board, player):
        self.set_board(board)
        # probably this is an error
        return ''.join(''.join(row) for row in self.board).count(player)

    # defines if the game is over or not
    def is_game_over(self, board):
        self.set_board(board)
        return self.game.outcome() != self.game.GAME_STATES["IN_PROGRESS"]
Esempio 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)
Esempio n. 3
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)
Esempio n. 4
0
class AIHelper():
    MAX_PLAYER = 'w'
    MIN_PLAYER = 'b'
    INFINITY = 1.0e+10

    """
    Helper interface class for the AI
        $1. available moves (board, player)
        $2. get_resulting_board -> (board, player, coord)
        $3. player pools (board, player)
        $4. check if game has ended (board)
    """

    def __init__(self, board=None):
        self.game = Reversi()
        if board:
            self.set_board(board)

    def set_board(self, board):
        self.game.board = FormatConverter.ai_to_game_board(board)

    def set_player(self, player):
        self.game.player = Player(player)

    def available_moves(self, board, player):
        self.set_board(board)
        self.set_player(player)
        return self.game.available_fields()

    def get_resulting_board(self, board, player, coord):
        self.set_board(board)
        self.set_player(player)
        self.game.play(coord)
        return FormatConverter.game_to_ai_board(self.game.board)

    def player_pool(self, board, player):
        self.set_board(board)
        return ''.join(''.join(row) for row in self.board).count(player)

    def is_game_over(self, board):
        self.set_board(board)
        return self.game.outcome() != self.game.GAME_STATES["IN_PROGRESS"]
Esempio n. 5
0
class AIHelper():
    MAX_PLAYER = 'w'
    MIN_PLAYER = 'b'
    INFINITY = 1.0e+10
    """
    Helper interface class for the AI
        $1. available moves (board, player)
        $2. get_resulting_board -> (board, player, coord)
        $3. player pools (board, player)
        $4. check if game has ended (board)
    """
    def __init__(self, board=None):
        self.game = Reversi()
        if board:
            self.set_board(board)

    def set_board(self, board):
        self.game.board = FormatConverter.ai_to_game_board(board)

    def set_player(self, player):
        self.game.player = Player(player)

    def available_moves(self, board, player):
        self.set_board(board)
        self.set_player(player)
        return self.game.available_fields()

    def get_resulting_board(self, board, player, coord):
        self.set_board(board)
        self.set_player(player)
        self.game.play(coord)
        return FormatConverter.game_to_ai_board(self.game.board)

    def player_pool(self, board, player):
        self.set_board(board)
        return ''.join(''.join(row) for row in self.board).count(player)

    def is_game_over(self, board):
        self.set_board(board)
        return self.game.outcome() != self.game.GAME_STATES["IN_PROGRESS"]
Esempio n. 6
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)
Esempio n. 7
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)