예제 #1
0
    def test_utility(self):
        # X wins
        player_board = \
            [[self.X, self.O, self.O],
            [self.O, self.O, self.X],
            [self.X, self.X, self.X]]
        self.assertEqual(1, ttt.utility(player_board))

        # O wins
        player_board = \
            [[self.X, self.O, self.X],
            [self.O, self.O, self.X],
            [self.X, self.O, self.X]]
        self.assertEqual(-1, ttt.utility(player_board))

        # tie
        player_board = \
            [[self.O, self.X, self.X],
            [self.X, self.O, self.O],
            [self.X, self.O, self.X]]
        self.assertEqual(0, ttt.utility(player_board))

        player_board = \
            [[self.X, self.O, self.X],
            [self.O, self.O, self.EMPTY],
            [self.X, self.EMPTY, self.EMPTY]]
        self.assertEqual(0, ttt.utility(player_board))
예제 #2
0
def game_loop():
    WIDTH = 3
    b = ttt.make_board(WIDTH)

    while ttt.utility(b) is None:
        ttt.display(b)

        ### Human moves
        # sanitize user input

        user_move = ttt.prompt_move()
        while not ttt.valid_move(user_move, b):
            user_move = ttt.prompt_move()
        # process move
        b[user_move[0]][user_move[1]] = 'O'

        # end game if human plays winning move
        ttt.display(b)
        if ttt.utility(b) is None:
            ### AI moves
            current_node = SearchNode(None, None, b)
            best_node = minimax(current_node,
                                -9999999999,
                                9999999999,
                                maximize=True)
            b = best_node.board
예제 #3
0
    def test_utility(self):

        utility_4 = ttt.utility(self.board_4)
        self.assertEqual(utility_4, 1)

        utility_5 = ttt.utility(self.board_5)
        self.assertEqual(utility_5, -1)

        utility_6 = ttt.utility(self.board_6)
        self.assertEqual(utility_6, 1)
예제 #4
0
    def test_utility(self):
        board = [[tictactoe.O, tictactoe.X, tictactoe.O],
                 [tictactoe.X, tictactoe.O, tictactoe.X],
                 [tictactoe.X, tictactoe.O, tictactoe.X]]

        utilityScore = tictactoe.utility(board)

        print(utilityScore)
예제 #5
0
    def game_over(self):
        score = tictactoe.utility(self.state)

        if score == tictactoe.RESULT_X_WINS:
            msg = "Player x wins"
        elif score == tictactoe.RESULT_O_WINS:
            msg = "Player o wins"
        else:
            msg = "It's a draw"

        messagebox.showinfo("Game Over", msg)

        self.reset()
예제 #6
0
    def max_value(state, depth=0):

        if ttt.terminal(state):
            return (None, ttt.utility(state))

        v = (None, -2)

        for action in ttt.actions(state):
            v = max(
                v,
                (action, min_value(ttt.result(state, action), depth + 1)[1] -
                 (depth / 10)),
                key=lambda x: x[1])
        return v
예제 #7
0
    def test_utility(self):
        # Game is a tie
        board = [
            [tictactoe.X, tictactoe.O, tictactoe.O],
            [tictactoe.O, tictactoe.X, tictactoe.X],
            [tictactoe.X, tictactoe.X, tictactoe.O],
        ]
        self.assertEqual(tictactoe.utility(board), 0)

        # X has won
        board = [
            [tictactoe.X, tictactoe.EMPTY, tictactoe.EMPTY],
            [tictactoe.O, tictactoe.X, tictactoe.O],
            [tictactoe.EMPTY, tictactoe.EMPTY, tictactoe.X],
        ]
        self.assertEqual(tictactoe.utility(board), 1)

        # O has won
        board = [
            [tictactoe.X, tictactoe.EMPTY, tictactoe.EMPTY],
            [tictactoe.O, tictactoe.O, tictactoe.O],
            [tictactoe.X, tictactoe.EMPTY, tictactoe.X],
        ]
        self.assertEqual(tictactoe.utility(board), -1)
예제 #8
0
def minimax(current_node, a, b, maximize=True):
    # check if we're at a leaf node
    util = ttt.utility(current_node.board)
    if util is not None:
        # the game is over, return the reward
        # ttt.display(current_node.board)
        return SearchNode(current_node, util, current_node.board)

    if maximize:
        child_node = SearchNode(current_node, -9999999999, current_node.board)

        # if it's our turn, return the move that maximizes utility
        for state in ttt.successors(current_node.board, 'X'):
            child_node = max(child_node,
                             minimax(SearchNode(current_node, None, state), a,
                                     b, not maximize),
                             key=lambda node: node.utility)

            # Alpha-Beta pruning
            if child_node.utility >= b:
                return child_node

            a = max(a, child_node.utility)

        return child_node
    else:
        child_node = SearchNode(current_node, 9999999999, current_node.board)

        # If opponent's turn, return the move that minimizes utility
        for state in ttt.successors(current_node.board, 'O'):
            child_node = min(child_node,
                             minimax(SearchNode(current_node, None, state), a,
                                     b, not maximize),
                             key=lambda node: node.utility)

            # Alpha-Beta pruning
            if child_node.utility <= a:
                return child_node

            b = min(b, child_node.utility)

        return child_node
예제 #9
0
from tictactoe import actions, result, winner, terminal, utility, minimax

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

board = [[EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY]]

board = [[X, X, O], [O, X, O], [X, EMPTY, EMPTY]]
print(winner(board))
print(terminal(board))
print(utility(board))
print(minimax(board))
예제 #10
0
파일: test.py 프로젝트: ssingla2k/tictactoe
import tictactoe as t

board = t.initial_state()

board[0][2] = t.X
board[1][1] = t.X
board[2][0] = t.X

print(t.utility(board))
print(t.terminal(board))
print(board)
for row in board:
	print(row.count(t.O))
print (t.player(board))
print(t.actions(board))

for a in t.actions(board):
	print(t.result(board,a))
예제 #11
0
def test_utiltity_X_wins():
    board = [["X", "O", EMPTY],
             ["O", "X", "O"],
             ["X", EMPTY, "X"]]
    assert ttt.utility(board) == 1
예제 #12
0
def test_utiltity_nobody_wins():
    board = [["X", "O", "O"],
             ["O", "X", "X"],
             ["X", "X", "O"]]
    assert ttt.utility(board) == 0
예제 #13
0
from tictactoe import utility, EMPTY, X, O

print(
    utility([[EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY],
             [EMPTY, EMPTY, EMPTY]]))
예제 #14
0
 def test_utility_tie(self):
     board = [[X, X, O], [O, O, X], [X, O, X]]
     self.assertEqual(utility(board), 0)
예제 #15
0
 def test_utility_o_wins(self):
     board = [[O, O, O], [X, X, EMPTY], [O, X, X]]
     self.assertEqual(utility(board), -1)
예제 #16
0
파일: test.py 프로젝트: LeoZorzoli/CS50AI
from tictactoe import actions, terminal, utility, winner, player, initial_state

board = initial_state()

utility(board)
예제 #17
0
from tictactoe import initial_state
from tictactoe import player
from tictactoe import actions
from tictactoe import result
from tictactoe import winner
from tictactoe import terminal
from tictactoe import utility
from tictactoe import minimax

board = initial_state()
actions = actions(board)
winner = winner(board)
terminal = terminal(board)
utility = utility(board)
minimax = minimax(board)
print(minimax)
예제 #18
0
파일: tests.py 프로젝트: iAmoghy/CS50AI
assert all_actions == ttt.actions(empty_board)

# First move
first_move = deepcopy(empty_board)
first_move[0][0] = X

assert ttt.result(empty_board, (0, 0)) == first_move

winner_boardX = deepcopy(first_move)
winner_boardX[0][1] = X
winner_boardX[0][2] = X

assert not ttt.terminal(empty_board)
assert ttt.terminal(winner_boardX)
assert ttt.winner(winner_boardX) == X
assert ttt.utility(winner_boardX) == 1

winner_boardO = deepcopy(empty_board)
winner_boardO[0][1] = O
winner_boardO[1][1] = O
winner_boardO[2][1] = O

assert ttt.terminal(winner_boardO)
assert ttt.winner(winner_boardO) == O
assert ttt.utility(winner_boardO) == -1

winner_board_diagonal = deepcopy(empty_board)
winner_board_diagonal[2][0] = O
winner_board_diagonal[1][1] = O
winner_board_diagonal[0][2] = O
winner_board_diagonal[0][0] = X
예제 #19
0
from tictactoe import X, O, EMPTY, utility

tie1 = [[O, X, O], [O, X, O], [X, O, X]]

x1 = [[X, X, X], [O, X, O], [X, O, X]]

o1 = [[O, O, O], [X, X, O], [X, O, X]]

if utility(tie1) == 0:
    print("Tie test works")
else:
    print("Tie test failed")

if utility(x1) == 1:
    print("X test works")
else:
    print("X test failed")

if utility(o1) == -1:
    print("O test works")
else:
    print("O test failed")
예제 #20
0
파일: runner.py 프로젝트: CI0N3/tictactoe
def game():

    board = ttt.initial_state()
    user = input("X or O? ")
    game_over = ttt.terminal(board)

    def print_board(board):
        print("")
        print(board[0])
        print(board[1])
        print(board[2])

    while game_over is False:
        """""
        If the user is X and the ai is O
        """ ""
        if user == "X":
            player = ttt.player(board)
            if player == "X":
                print_board(board)
                square = int(input("Which square (0-8)? "))
                if square < 3 and square > -1 and board[0][square] == None:
                    board = ttt.result(board, ("(0, " + str(square) + ")"))
                elif square > 5 and square < 9 and board[2][square -
                                                            6] == None:
                    board = ttt.result(board, (2, square - 6))
                elif square > 2 and square < 6 and board[1][square -
                                                            3] == None:
                    board = ttt.result(board, (1, square - 3))
                else:
                    print("Try again.\n")
                game_over = ttt.terminal(board)

            elif player == "O":
                board = ttt.result(board, ttt.minimax(board))
                game_over = ttt.terminal(board)
        """""
        If the user is O and the ai is X
        """ ""
        if user == "O":
            player = ttt.player(board)
            if player == "X":
                board = ttt.result(board, ttt.minimax(board))
                game_over = ttt.terminal(board)

            elif player == "O":
                print_board(board)
                square = int(input("Which square (0-8)? "))
                if square < 3 and square > -1 and board[0][square] == None:
                    board = ttt.result(board, ("(0, " + str(square) + ")"))
                elif square > 5 and square < 9 and board[2][square -
                                                            6] == None:
                    board = ttt.result(board, (2, square - 6))
                elif square > 2 and square < 6 and board[1][square -
                                                            3] == None:
                    board = ttt.result(board, (1, square - 3))
                else:
                    print("Try again.\n")
                game_over = ttt.terminal(board)
    """""
    Once the while loop is broken (when a terminal state of the board is reached) then it prints the results of the game
    """ ""
    print_board(board)
    if ttt.utility(board) != 0:
        if ttt.utility(board) == 1:
            print("X has won!")
        else:
            print("O has won!")
    else:
        print("Tie!")
예제 #21
0
 def testUtility(self):
     self.assertEqual(ttt.utility(board_tie), 0)
     self.assertEqual(ttt.utility(board_win_horizontally), 1)
     self.assertEqual(ttt.utility(board_win_vertically), -1)
     self.assertEqual(ttt.utility(board_win_diagonally), 1)
예제 #22
0
def test_utility(board, utility):
    assert tictactoe.utility(board) == utility