def build(self, node, turn, alpha, beta):
     states = next_states(node.value, turn)
     if not states:
         if winner(node.value, 1):
             node.score = 1
         elif winner(node.value, 0):
             node.score = -1
         else:
             node.score = 0
         return node.score
     if turn == 1:
         for state in states:
             child = Node(state)
             node.add_child(child)
             alpha = max(alpha, self.build(child, 0, alpha, beta))
             if beta <= alpha:
                 break
         score = alpha
     else:
         for state in states:
             child = Node(state)
             node.add_child(child)
             beta = min(beta, self.build(child, 1, alpha, beta))
             if beta <= alpha:
                 break
         score = beta
     node.score = score
     return node.score
Beispiel #2
0
    def test_winner(self):
        player_board = \
            [[self.X, self.O, self.O],
            [self.O, self.O, self.X],
            [self.X, self.X, self.X]]
        self.assertEqual(self.X, ttt.winner(player_board))

        player_board = \
            [[self.X, self.O, self.X],
            [self.O, self.O, self.X],
            [self.X, self.O, self.X]]
        self.assertEqual(self.O, ttt.winner(player_board))

        player_board = \
            [[self.X, self.O, self.X],
            [self.X, self.X, self.O],
            [self.O, self.O, self.X]]
        self.assertEqual(self.X, ttt.winner(player_board))

        player_board = \
            [[self.O, self.X, self.X],
            [self.O, self.X, self.O],
            [self.X, self.O, self.X]]
        self.assertEqual(self.X, ttt.winner(player_board))

        player_board = \
            [[self.X, self.X, self.X],
            [self.EMPTY, self.EMPTY, self.O],
            [self.O, self.EMPTY, self.EMPTY]]
        self.assertEqual(self.X, ttt.winner(player_board))
 def score_leaves(self):
     for leaf in self.leaves:
         if winner(leaf.value, 1):
             leaf.score = 1
         elif winner(leaf.value, 0):
             leaf.score = -1
         else:
             leaf.score = 0
Beispiel #4
0
def test_winner_horizontal1():
    board = [["X", "X", "X"],
             [EMPTY, EMPTY, EMPTY],
             [EMPTY, EMPTY, EMPTY]]
    assert ttt.winner(board) == "X"
    board = [["X", "X", "O"],
             [EMPTY, EMPTY, EMPTY],
             [EMPTY, EMPTY, EMPTY]]
    assert ttt.winner(board) == None
def test_winner(marker, winner_move_idx):
    init = ttt.initial_state()
    assert ttt.winner(init) is None
    assert not ttt.terminal(init)

    # perform the winning move
    for i, j in ttt._winner_moves()[winner_move_idx]:
        init[i][j] = marker

    # and win!
    assert ttt.winner(init) is marker
    assert ttt.terminal(init)
Beispiel #6
0
    def test_no_winner(self):
        player_board = \
            [[self.X, self.O, self.X],
            [self.O, self.O, self.EMPTY],
            [self.X, self.EMPTY, self.EMPTY]]
        self.assertIsNone(ttt.winner(player_board))

        player_board = \
            [[self.O, self.X, self.X],
            [self.X, self.O, self.O],
            [self.X, self.O, self.X]]
        self.assertIsNone(ttt.winner(player_board))
Beispiel #7
0
 def testWinner(self):
     self.assertEqual(ttt.winner(board_win_horizontally), 'X')
     self.assertEqual(ttt.winner(board_win_vertically), 'O')
     self.assertEqual(ttt.winner(board_win_diagonally), 'X')
     self.assertEqual(ttt.winner(board_0), None)
     self.assertEqual(ttt.winner(board_1), None)
     self.assertEqual(ttt.winner(board_2), None)
     self.assertEqual(ttt.winner(board_3), None)
     self.assertEqual(ttt.winner(board_4), None)
Beispiel #8
0
    def test_winner(self):
        board = [[tictactoe.EMPTY, tictactoe.EMPTY, tictactoe.EMPTY],
                 [tictactoe.O, tictactoe.O, tictactoe.EMPTY],
                 [tictactoe.O, tictactoe.O, tictactoe.O]]

        winner = tictactoe.winner(board)

        print(winner)
    def test_winner(self):
        # Check no winner
        board = [
            [tictactoe.X, tictactoe.O, tictactoe.EMPTY],
            [tictactoe.O, tictactoe.O, tictactoe.X],
            [tictactoe.X, tictactoe.X, tictactoe.EMPTY],
        ]
        result = tictactoe.winner(board)
        self.assertIsNone(result)

        # Check vertical match
        board = [
            [tictactoe.X, tictactoe.O, tictactoe.EMPTY],
            [tictactoe.O, tictactoe.O, tictactoe.X],
            [tictactoe.X, tictactoe.O, tictactoe.X],
        ]
        result = tictactoe.winner(board)
        self.assertEqual(result, tictactoe.O)

        # Check horizontal match
        board = [
            [tictactoe.X, tictactoe.O, tictactoe.O],
            [tictactoe.O, tictactoe.O, tictactoe.X],
            [tictactoe.X, tictactoe.X, tictactoe.X],
        ]
        result = tictactoe.winner(board)
        self.assertEqual(result, tictactoe.X)

        # Check main diagonal match
        board = [
            [tictactoe.X, tictactoe.O, tictactoe.O],
            [tictactoe.X, tictactoe.X, tictactoe.EMPTY],
            [tictactoe.O, tictactoe.EMPTY, tictactoe.X],
        ]
        result = tictactoe.winner(board)
        self.assertEqual(result, tictactoe.X)

        # Check anti diagonal match
        board = [
            [tictactoe.X, tictactoe.O, tictactoe.O],
            [tictactoe.X, tictactoe.O, tictactoe.EMPTY],
            [tictactoe.O, tictactoe.X, tictactoe.X],
        ]
        result = tictactoe.winner(board)
        self.assertEqual(result, tictactoe.O)
Beispiel #10
0
    def test_winner(self):
        state_history = []
        state = tictactoe.new_game()
        state_history.append(state)

        # Horizontal win
        state['board'] = [['X', '', 'X'], ['O', 'O', 'O'], ['', '', 'X']]
        self.assertEqual(tictactoe.winner(state_history), 1)

        # Vertical win
        state['board'] = [['X', '', 'O'], ['X', 'O', ''], ['X', '', 'X']]
        self.assertEqual(tictactoe.winner(state_history), 0)

        # Diagonal win
        state['board'] = [['X', 'X', 'O'], ['', 'O', ''], ['O', '', 'X']]
        self.assertEqual(tictactoe.winner(state_history), 1)

        # No win yet
        state['board'] = [['X', '', 'X'], ['', 'O', 'O'], ['', '', 'X']]
        self.assertEqual(tictactoe.winner(state_history), -1)

        # Tied game
        state['board'] = [['X', 'O', 'X'], ['O', 'X', 'O'], ['O', 'X', 'O']]
        self.assertEqual(tictactoe.winner(state_history), -2)
Beispiel #11
0
    def rollout(self, node):
        """ Randomly plays from the state defined by node until
        the end of the game, returning the final outcome.

        """

        # Available moves from this point
        board = node.board
        moves = ttt.actions(board)

        # Used to store the winner
        w = None

        # Play until there's a winner
        while w is None:

            # Pick a random move
            n = randint(low=0, high=len(moves))
            action = moves[n]

            # Update the board (result automatically checks
            # whose go it is)
            board = ttt.result(board, action)

            # List all possible moves
            moves = ttt.actions(board)

            # See if anyone has won
            w = ttt.winner(board)

            # Break if we've run out of moves
            if len(moves) == 0:
                break

        if w is 'O':
            reward = 2
        elif w is None:
            reward = 1
        elif w is 'X':
            reward = 0

        return reward
Beispiel #12
0
    def test_winner(self):
        winner_1 = ttt.winner(self.board_1)
        self.assertEqual(winner_1, None)

        winner_2 = ttt.winner(self.board_2)
        self.assertEqual(winner_2, None)

        winner_3 = ttt.winner(self.board_3)
        self.assertEqual(winner_3, None)

        winner_4 = ttt.winner(self.board_4)
        self.assertEqual(winner_4, X)

        winner_5 = ttt.winner(self.board_5)
        self.assertEqual(winner_5, O)

        winner_6 = ttt.winner(self.board_6)
        self.assertEqual(winner_6, X)

        winner_7 = ttt.winner(self.board_7)
        self.assertEqual(winner_7, O)
Beispiel #13
0
def resolve_and_alert(game):
    if still_going(game.board):
        next_up = whos_next(game.board)
        player = game.player1 if next_up == 'X' else game.player2
        # ping player/play with the game.board
        requests.post("{}/play".format(player),
                      json={
                          "board": game.board,
                          "game": game.game_id
                      })

    else:
        winning_side = winner(game.board)
        if winning_side == 'X':
            requests.post("{}/finish".format(game.player1), json={"winner": 1})
            requests.post("{}/finish".format(game.player2),
                          json={"winner": -1})
        if winning_side == 'O':
            requests.post("{}/finish".format(game.player2), json={"winner": 1})
            requests.post("{}/finish".format(game.player1),
                          json={"winner": -1})
        else:
            requests.post("{}/finish".format(game.player1), json={"winner": 0})
            requests.post("{}/finish".format(game.player2), json={"winner": 0})
Beispiel #14
0
 def test_winner_diagonal(self):
     board = [[O, O, EMPTY],
         [X, O, X],
         [X, X, O]]
     self.assertEqual(winner(board), O)
Beispiel #15
0
 def test_winner_col(self):
     board = [[X, O, EMPTY],
         [X, O, EMPTY],
         [X, X, O]]
     self.assertEqual(winner(board), X)
Beispiel #16
0
 def test_winner_row_2(self):
     board = [[X, X, X],
         [X, O, EMPTY],
         [EMPTY, X, EMPTY]]
     self.assertEqual(winner(board), X)
Beispiel #17
0
 def test_winner_row(self):
     board = [[O, O, EMPTY],
         [X, O, EMPTY],
         [X, X, X]]
     self.assertEqual(winner(board), X)
 def test_winner_vertical_2(self):
     board = [[X, EMPTY, O], [X, EMPTY, O], [EMPTY, EMPTY, O]]
     self.assertEqual(winner(board), O)
Beispiel #19
0
                pygame.draw.rect(screen, white, rect, 3)

                if board[i][j] != ttt.EMPTY:
                    move = moveFont.render(board[i][j], True, white)
                    moveRect = move.get_rect()
                    moveRect.center = rect.center
                    screen.blit(move, moveRect)
                row.append(rect)
            tiles.append(row)

        game_over = ttt.terminal(board)
        player = ttt.player(board)

        # Show title
        if game_over:
            winner = ttt.winner(board)
            if winner is None:
                title = f"Game Over: Tie."
            else:
                title = f"Game Over: {winner} wins."
        elif user == player:
            title = f"Play as {user}"
        else:
            title = f"Computer thinking..."
        title = largeFont.render(title, True, white)
        titleRect = title.get_rect()
        titleRect.center = ((width / 2), 30)
        screen.blit(title, titleRect)

        # Check for AI move
        if user != player and not game_over:
Beispiel #20
0
def terminal(state):
    return winner(state, 1) or winner(state, 0) or state.count(None) == 0
Beispiel #21
0
def test_empty_winner():
    board = [[EMPTY, EMPTY, EMPTY],
             [EMPTY, EMPTY, EMPTY],
             [EMPTY, EMPTY, EMPTY]]
    assert ttt.winner(board) == None
Beispiel #22
0
def test_winner_diagonal1():
    board = [["X", EMPTY, "X"],
             [EMPTY, "X", EMPTY],
             [EMPTY, EMPTY, "X"]]
    assert ttt.winner(board) == "X"
 def test_winner_horizontal_0(self):
     board = [[X, X, X], [O, EMPTY, O], [EMPTY, EMPTY, O]]
     self.assertEqual(winner(board), X)
Beispiel #24
0
 def test_winner_back_diagonal(self):
     board = [[X, O, O],
         [X, O, X],
         [O, X, O]]
     self.assertEqual(winner(board), O)
Beispiel #25
0
                pygame.draw.rect(screen, white, rect, 3)

                if board[i][j] != tct.EMPTY:
                    move = moveFont.render(board[i][j], True, white)
                    moveRect = move.get_rect()
                    moveRect.center = rect.center
                    screen.blit(move, moveRect)
                row.append(rect)
            tiles.append(row)

        game_over = tct.terminal(board)
        player = tct.player(board)

        # Show title
        if game_over:
            winner = tct.winner(board)
            if winner is None:
                title = f"Game Over: Tie."
            else:
                title = f"Game Over: {winner} wins."
        elif user == player:
            title = f"Play as {user}"
        else:
            title = f"Computer thinking..."
        title = largeFont.render(title, True, white)
        titleRect = title.get_rect()
        titleRect.center = ((width / 2), 30)
        screen.blit(title, titleRect)

        # Check for AI move
        if user != player and not game_over:
Beispiel #26
0
from tictactoe import X, O, EMPTY, winner

if __name__ == "__main__":
    boards = [[[EMPTY, X, EMPTY], [EMPTY, X, O], [EMPTY, X, O]],
              [[X, EMPTY, EMPTY], [O, O, O], [X, X, EMPTY]],
              [[O, X, X], [X, O, X], [X, X, O]],
              [[EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY],
               [EMPTY, EMPTY, EMPTY]]]

    answers = [X, O, O, None]

    for ind, board in enumerate(boards):
        assert winner(board) == answers[
            ind], f"Winner returned {winner(board)}. Expected {answers[ind]}."

    print("Tests complete.")
Beispiel #27
0
from tictactoe import winner

X = "X"
O = "O"
EMPTY = None

# X
print(X is winner([[X, X, X], [O, O, EMPTY], [X, EMPTY, EMPTY]]))

# O
print(O is winner([[X, X, EMPTY], [O, O, O], [X, EMPTY, EMPTY]]))

# None
print(None is winner([[X, X, EMPTY], [O, O, EMPTY], [X, EMPTY, EMPTY]]))

# X
print(X is winner([[X, X, EMPTY], [X, O, EMPTY], [X, EMPTY, EMPTY]]))

# O
print(O is winner([[X, O, EMPTY], [EMPTY, O, EMPTY], [X, O, EMPTY]]))

# X
print(X is winner([[X, O, EMPTY], [EMPTY, X, EMPTY], [X, O, X]]))

# O
print(O is winner([[X, EMPTY, O], [EMPTY, O, EMPTY], [O, O, X]]))

# X
print(X is winner([[O, O, X], [EMPTY, X, O], [X, EMPTY, EMPTY]]))
Beispiel #28
0
 def test_no_winner(self):
     board = [[EMPTY, O, EMPTY],
         [X, EMPTY, EMPTY],
         [EMPTY, X, EMPTY]]
     self.assertEqual(winner(board), None)
Beispiel #29
0
from tictactoe import player, actions, result, winner, terminal, utility, minimax, max_value, min_value

X = "X"
O = "O"
EMPTY = None

board = [[O, X, EMPTY], [O, X, X], [O, O, X]]

# print(player(board))
# print(actions(board))
# print(result(board,(0,1)))
# print(winner(board))
# print(terminal(board))
# print(utility(board))
print(terminal(board))
print(winner(board))
 def test_winner_vertical_1(self):
     board = [[O, X, O], [X, X, O], [O, X, X]]
     self.assertEqual(winner(board), X)