Example #1
0
def player_turn():
    moves = game_state.get_possible_moves()
    selection = -1
    while selection not in moves:
        selection = int(input())

        if selection == 1:
            selection = [0, 0]
        if selection == 2:
            selection = [0, 1]
        if selection == 3:
            selection = [0, 2]
        if selection == 4:
            selection = [1, 0]
        if selection == 5:
            selection = [1, 1]
        if selection == 6:
            selection = [1, 2]
        if selection == 7:
            selection = [2, 0]
        if selection == 8:
            selection = [2, 1]
        if selection == 9:
            selection = [2, 2]

    game_state.update_board(selection, 'hn')
    game_state.show_game_state()
Example #2
0
def minimax(depth, isMaximizing):
    bestMove = 0
    result = game_state.check_for_win()

    if depth == 0 or result != None:
        return [game_state.evaluate(), -1]

    if isMaximizing:
        maxEval = -math.inf
        for move in game_state.get_possible_moves():
            game_state.update_board(move, 'ai')
            evaluation = minimax(depth - 1, False)
            game_state.undo(move)
            if evaluation[0] > maxEval:
                maxEval = evaluation[0]
                bestMove = move
        return [maxEval, bestMove]

    else:
        minEval = math.inf
        for move in game_state.get_possible_moves():
            game_state.update_board(move, 'hn')
            evaluation = minimax(depth - 1, True)
            game_state.undo(move)
            if evaluation[0] < minEval:
                minEval = evaluation[0]
                bestMove = move
        return [minEval, bestMove]
Example #3
0
def computer_turn():
    global difficulty
    global flip_flop
    #move = computer.minimax(9, True)[1]
    moves = game_state.get_possible_moves()
    move = None
    if flip_flop:
        move = computer.minimax_with_pruning(len(moves), True)[1]
    else:
        move = moves[random.randint(0, len(moves) - 1)]
    if difficulty == 2:
        flip_flop = not flip_flop
    game_state.update_board(move, 'ai')
    game_state.show_game_state()
Example #4
0
def MakeMove(i, j):
    global toggle, count, pc, flip_flop, difficulty

    if buttons[i][j]['text'] == ' ' and toggle:
        buttons[i][j]['text'] = 'X'
        buttons[i][j].config(state=DISABLED)
        game_state.update_board([i, j], 'hn')

    if buttons[i][j]['text'] == ' ' and not toggle:
        buttons[i][j]['text'] = 'O'
        buttons[i][j].config(state=DISABLED)
        game_state.update_board([i, j], 'ai')

    toggle = not toggle
    count += 1

    state = game_state.check_for_win()
    if state != None:
        if state == -1:
            ClearWindow()
            messagebox.showerror('End Game', 'X wins!')
            return
        if state == 1:
            ClearWindow()
            messagebox.showerror('End Game', 'O wins!')
            return
        if state == 0:
            ClearWindow()
            messagebox.showerror('End Game', 'Game ended in tie!')
            return

    if pc and not toggle:
        moves = game_state.get_possible_moves()
        move = None

        if flip_flop:
            move = computer.minimax_with_pruning(len(moves), True)[1]
        else:
            move = moves[random.randint(0, len(moves) - 1)]

        if difficulty == 2:
            flip_flop = not flip_flop

        MakeMove(move[0], move[1])