Exemple #1
0
    def test_winner(self):
        board = Board()
        self.assertIsNone(board.winner)

        board = Board()
        board.state = ['x'] * 9
        self.assertEqual(board.winner, 'x')
Exemple #2
0
    def test_init(self):
        board = Board()
        self.assertEqual(board.state, [i for i in range(9)])

        custom_state = ['', 1, 2, 4, 6, 8, 11, 'b']
        board = Board(state=custom_state)
        self.assertEqual(board.state, custom_state)
Exemple #3
0
    def test_available_positions(self):
        initial_state = [i for i in range(9)]
        board = Board()
        self.assertEqual(board.available_positions(), initial_state)

        new_state = initial_state[:]
        new_state[0] = 'x'
        board.state = new_state
        self.assertEqual(board.available_positions(), initial_state[1:])
Exemple #4
0
    def get_column(self, board=None) -> int:

        print("\n*** Hmmm... Let me think... ***")
        if not board:
            board = Board()

        columns = get_possible_moves(board)
        column = get_winning_column(board, columns)
        if column:
            return column

        column = get_winning_column(board, columns, opponent=True)
        if column:
            return column

        games_per_column = self.number_of_games // len(columns)
        win_rate = []
        for column in columns:
            results = [
                self.get_random_game_result(board, column)
                for _ in range(games_per_column)
            ]
            win_rate.append((results.count(True), column))

        return max(win_rate)[1]
Exemple #5
0
 def test_many_diagonals(self):
     board = [EMPTY_CELL * 3, ''.join(['g', 'r', EMPTY_CELL])]
     b = Board(board)
     assert b.diagonals == [[EMPTY_CELL], ['g', EMPTY_CELL],
                            ['r', EMPTY_CELL], [EMPTY_CELL], [EMPTY_CELL],
                            [EMPTY_CELL, EMPTY_CELL], [EMPTY_CELL, 'r'],
                            ['g']]
Exemple #6
0
 def test_minimax(self):
     """
     I can't possibly test all the possibilities so I present you this one
     case.
     """
     bot = UnbeatableBot('x')
     board = Board(state=['x', 'x', 'o', 'o', 'x', 'o', 'x', 'o', 8])
     score = bot.minimax('x', board=board)
     self.assertEqual(score, 10)
Exemple #7
0
def get_winning_column(board: Board, columns: List[int], opponent=False):
    copied_board = board.rows
    for column in columns:
        g = Game(board=Board(rows=copied_board))
        try:
            color = g.get_color_to_move()
            if opponent:
                color = g.get_opponent_color()
            g.make_move(column, color)
        except GameDrawn:
            continue
        except GameWon:
            return column
Exemple #8
0
 def get_random_game_result(self, board, column) -> bool:
     board_copy = board.rows
     g = Game(player1=RandomBot(),
              player2=RandomBot(),
              board=Board(rows=board_copy))
     my_color = g.current_color
     try:
         g.make_move(column)
     except GameDrawn:
         return False
     except GameWon:
         return True
     while True:
         try:
             g.make_move()
         except IndexError:
             pass
         except GameDrawn:
             return False
         except GameWon:
             return g.current_color == my_color
Exemple #9
0
 def test_default_board(self):
     b = Board()
     b2 = Board([])
     assert b == b2
Exemple #10
0
 def test_one_cell(self):
     b = Board([EMPTY_CELL])
     assert b.__str__() == EMPTY_CELL
Exemple #11
0
 def test_one_diagonal(self):
     board = [EMPTY_CELL]
     b = Board(board)
     assert b.diagonals == [[EMPTY_CELL], [EMPTY_CELL]]
Exemple #12
0
 def test_diagonals(self):
     b = Board()
     assert len(b.diagonals) == (b.shape[0] + b.shape[1] - 1) * 2
Exemple #13
0
 def test_multiple_moves(self):
     b = Board(['--', 'g-'])
     assert len(get_possible_moves(b)) > 1
Exemple #14
0
 def test_full_column(self):
     b = Board(['g'])
     c = b.get_column(0)
     assert is_column_full(c)
Exemple #15
0
 def test_zero_moves(self):
     b = Board(['g'])
     assert get_possible_moves(b) == []
Exemple #16
0
 def test_no_win(self):
     b = Board(['ggg-'])
     columns = [3]
     assert not get_winning_column(b, columns, opponent=True)
Exemple #17
0
 def test_win(self):
     b = Board(['ggg-'])
     columns = [3]
     assert get_winning_column(b, columns) == 3
Exemple #18
0
 def test_many_cells(self):
     b = Board(['rr', 'gg'])
     assert b.__repr__() == f"<Board(['rr', 'gg'])>"
Exemple #19
0
 def test_one_cell(self):
     b = Board([EMPTY_CELL])
     assert b.__repr__() == f"<Board(['{EMPTY_CELL}'])>"
Exemple #20
0
 def test_many_cells(self):
     b = Board(['rr', 'gg'])
     assert b.__str__() == 'r r\ng g'
Exemple #21
0
 def test_no_win(self):
     b = Board(['rrr-'])
     columns = [3]
     assert not get_winning_column(b, columns)
Exemple #22
0
 def test_unqual_boards(self):
     b = Board(['r'])
     b2 = Board(['g'])
     assert b != b2
Exemple #23
0
 def test_win(self):
     b = Board(['rrr-'])
     columns = [3]
     assert get_winning_column(b, columns, opponent=True) == 3
Exemple #24
0
 def test_equal_boards(self):
     b = Board(['g'])
     b2 = Board(['g'])
     assert b == b2
Exemple #25
0
 def test_not_full_column(self):
     b = Board([EMPTY_CELL])
     c = b.get_column(0)
     assert not is_column_full(c)
Exemple #26
0
 def test_default_board(self):
     b = Board()
     assert len(b) == 42
Exemple #27
0
 def test_one_move(self):
     b = Board(['-'])
     assert len(get_possible_moves(b)) == 1
Exemple #28
0
 def test_custom_board(self):
     board = [EMPTY_CELL * 12 for _ in range(6)]
     b = Board(board)
     assert len(b) == 72
Exemple #29
0
def main():

    # clear terminal
    os.system("clear")

    # print game info
    print()
    print("   --------------------------------")
    print("   |                              |")
    print("   |   CONNECT 4 - UAIS PROJECT   |")
    print("   |                              |")
    print("   --------------------------------")

    # get player 1
    print()
    print("   Select player 1 type:")
    print("   1. Human")
    print("   2. Random")
    print("   3. Minimax")

    choice = input()
    while choice not in ("1", "2", "3"):
        print("  invalid input, please try again")
        choice = input()

    if choice == "1":
        player1 = HumanPlayer()
    elif choice == "2":
        player1 = RandomPlayer()
    elif choice == "3":
        player1 = MinimaxPlayer()

    # get player 2
    print()
    print("   Select player 2 type:")
    print("   1. Human")
    print("   2. Random")
    print("   3. Minimax")

    choice = input()
    while choice not in ("1", "2", "3"):
        print("  invalid input, please try again")
        choice = input()

    if choice == "1":
        player2 = HumanPlayer()
    elif choice == "2":
        player2 = RandomPlayer()
    elif choice == "3":
        player2 = MinimaxPlayer()

    board = Board(player1, player2)

    playAgain = "Y"
    while playAgain == "Y":

        # this actually plays the game
        board.playGame()

        print("   Play again? (Y/N)")
        playAgain = input().upper()
        while playAgain not in ("Y", "N"):
            playAgain = input().upper()
Exemple #30
0
 def test_default(self):
     b = Board(shape=(1, 2))
     b2 = Board(shape=(2, 1))
     assert str(b.rows) == str(b2.columns)