def on_click(button):
    """
    This function determines the action of any button.
    """
    global ttt, choices, count, sym, result, x_pos, o_pos

    if count % 2 == 0:
        sym = "X"
    else:
        sym = "O"
    count += 1

    button.config(
        text=sym,
        state='disabled',
        disabledforeground="red")  # For cross

    x, y = get_coordinates(button)
    x += 1
    y += 1
    x_pos.append((x, y))
    state = gen_state(to_move='O', x_positions=x_pos,
                      o_positions=o_pos)
    try:
        choice = choices.get()
        if "Random" in choice:
            a, b = random_player(ttt, state)
        elif "Pro" in choice:
            a, b = minmax_decision(state, ttt)
        else:
            a, b = alpha_beta_player(ttt, state)
    except (ValueError, IndexError, TypeError) as e:
        disable_game()
        result.set("It's a draw :|")
        return
    if 1 <= a <= 3 and 1 <= b <= 3:
        o_pos.append((a, b))
        button_to_change = get_button(a - 1, b - 1)
        if count % 2 == 0:  # Used again, will become handy when user is given the choice of turn.
            sym = "X"
        else:
            sym = "O"
        count += 1

        if check_victory(button):
            result.set("You win :)")
            disable_game()
        else:
            button_to_change.config(text=sym, state='disabled',
                                    disabledforeground="black")
            if check_victory(button_to_change):
                result.set("You lose :(")
                disable_game()
def on_click(button):
    """
    This function determines the action of any button.
    """
    global ttt, choices, count, sym, result, x_pos, o_pos

    if count % 2 == 0:
        sym = "X"
    else:
        sym = "O"
    count += 1

    button.config(
        text=sym,
        state='disabled',
        disabledforeground="red")  # For cross

    x, y = get_coordinates(button)
    x += 1
    y += 1
    x_pos.append((x, y))
    state = gen_state(to_move='O', x_positions=x_pos,
                      o_positions=o_pos)
    try:
        choice = choices.get()
        if "Random" in choice:
            a, b = random_player(ttt, state)
        elif "Pro" in choice:
            a, b = minimax_decision(state, ttt)
        else:
            a, b = alphabeta_player(ttt, state)
    except (ValueError, IndexError, TypeError) as e:
        disable_game()
        result.set("It's a draw :|")
        return
    if 1 <= a <= 3 and 1 <= b <= 3:
        o_pos.append((a, b))
        button_to_change = get_button(a - 1, b - 1)
        if count % 2 == 0:  # Used again, will become handy when user is given the choice of turn.
            sym = "X"
        else:
            sym = "O"
        count += 1

        if check_victory(button):
            result.set("You win :)")
            disable_game()
        else:
            button_to_change.config(text=sym, state='disabled',
                                    disabledforeground="black")
            if check_victory(button_to_change):
                result.set("You lose :(")
                disable_game()
Exemple #3
0
    def mouse_click(self, x, y):
        player = self.players[self.turn]
        if self.ttt.terminal_test(self.state):
            if 0.55 <= x/self.width <= 0.95 and 6/7 <= y/self.height <= 6/7+1/8:
                self.state = self.ttt.initial
                self.turn = 0
                self.draw_board()
            return

        if player == 'human':
            x, y = int(3*x/self.width) + 1, int(3*y/(self.height*6/7)) + 1
            if (x, y) not in self.ttt.actions(self.state):
                # Invalid move
                return
            move = (x, y)
        elif player == 'alphabeta':
            move = alphabeta_player(self.ttt, self.state)
        else:
            move = random_player(self.ttt, self.state)
        self.state = self.ttt.result(self.state, move)
        self.turn ^= 1
        self.draw_board()
Exemple #4
0
    def mouse_click(self, x, y):
        player = self.players[self.turn]
        if self.ttt.terminal_test(self.state):
            if 0.55 <= x/self.width <= 0.95 and 6/7 <= y/self.height <= 6/7+1/8:
                self.state = self.ttt.initial
                self.turn = 0
                self.draw_board()
            return

        if player == 'human':
            x, y = int(3*x/self.width) + 1, int(3*y/(self.height*6/7)) + 1
            if (x, y) not in self.ttt.actions(self.state):
                # Invalid move
                return
            move = (x, y)
        elif player == 'alphabeta':
            move = alphabeta_player(self.ttt, self.state)
        else:
            move = random_player(self.ttt, self.state)
        self.state = self.ttt.result(self.state, move)
        self.turn ^= 1
        self.draw_board()
Exemple #5
0
    #     b1.initial.board.move("R", "L")
    #     print(b1.terminal_test(b1.initial))
    # if(move == "d"):
    #     b1.initial.board.move("R", "D")
    #     print(b1.terminal_test(b1.initial))
    # if(move == "u"):
    #     b1.initial.board.move("R", "U")
    #     print(b1.terminal_test(b1.initial))
    # if(move == "di"):
    #     b1.initial.board.display()
    # if(move == "e"):
    #     break

    # Random player para pruebas
    print("-------------------------")
    b1.result(b1, random_player(b1, b1.states[-1]))
    b1.display()
    print("-------------------------")
    b1.result(b1, random_player(b1, b1.states[-1]))
    b1.display()

    # Alpha vs random
    # print("-------------------------")
    # b1.result(b1,alphabeta_player(b1, b1.states[-1]))
    # b1.display()
    # print("-------------------------")
    # b1.result(b1,random_player(b1, b1.states[-1]))
    # b1.display()

print("WIN-------------------------")
b1.initial.board.display()
Exemple #6
0
# create the current game state with the available information
gameState = GameState(to_move=nextToMove,
                      utility=utilityValue,
                      board=board,
                      moves=moves)

print()
print("moves: ", end="")
# The below print will give all the legal actions possible from this gameState
print(ticTacToe.actions(gameState))

#Below is the alphabeta_player who choses to play optimally
alphabeta_player(ticTacToe, gameState)

#Below is the random_player who choses to play normally
random_player(ticTacToe, gameState)

print()
print("current game state: ")
#the below command displays the state of game
ticTacToe.display(gameState)

print()
print("================================")
print("Whose turn is it now?")
print(nextToMove)
print("===================================================")
print("How many states did the minimax algorithm evaluate?")
numberOfStates = 0
if (len(moves) > 0):
    optimalMove, numberOfStates = minimax_decision(gameState, ticTacToe, True)