class Computer

Constants

COMPUTER
HUMAN
MAX_DEPTH

Attributes

active_player[RW]
best_move[RW]
sub_alpha[RW]

Public Class Methods

new() click to toggle source
# File lib/ultimate_tic_tac_toe/computer.rb, line 9
def initialize
        @sub_alpha = []
        @best_move = 0
        @best_moves =[]
end

Public Instance Methods

check_winner(board, depth) click to toggle source
# File lib/ultimate_tic_tac_toe/computer.rb, line 39
def check_winner(board, depth)
        winner = 'None'
        winner = COMPUTER if board.winner?(COMPUTER)
        winner = HUMAN if board.winner?(HUMAN)
        winner = 'tie' if board.tie?
        winner = 'tie' if (depth >= MAX_DEPTH && board.grid.size > 10)
        winner
end
computer_move(board) click to toggle source
# File lib/ultimate_tic_tac_toe/computer.rb, line 15
def computer_move(board)
        if board.player_moves > board.computer_moves
                @active_player = COMPUTER
                computers_move(board)
        end
end
computers_move(board) click to toggle source
# File lib/ultimate_tic_tac_toe/computer.rb, line 22
def computers_move(board)
        if board.computer_moves < 0
                if board.unoccupied(6)
                        board.store_position(6, COMPUTER)
                else
                        board.store_position(8, COMPUTER)
                end
        else
                minimax(board, @active_player)
                board.store_position(@best_move, @active_player)
        end
end
minimax(board, current_player) click to toggle source
# File lib/ultimate_tic_tac_toe/computer.rb, line 35
def minimax(board, current_player)
        minimax_recurse(board, current_player, 0)
end
minimax_recurse(board, player, depth) click to toggle source
# File lib/ultimate_tic_tac_toe/computer.rb, line 49
def minimax_recurse(board, player, depth)
        winner = check_winner(board, depth)
        unless winner == 'None'
                if winner == 'tie'
                        return 0
                elsif winner ==      @active_player
                        return 1
                else
                        return -1
                end
        end

        next_player = (player == COMPUTER ? HUMAN : COMPUTER )

        if player == @active_player
                alpha = -1
        else
                alpha = 1
        end

        possible_moves = board.get_move_list(board.grid)

        possible_moves.each do |move|
                new_board = board.dup
                new_board.grid = board.grid.dup
                next_board = new_board.simulate_move(new_board, move, player)
                @sub_alpha = minimax_recurse(next_board, next_player, depth + 1)

                if player == @active_player
                        if ((depth == 0 ) && (alpha <= @sub_alpha ))
                                @best_move = move
                        end
                        alpha = [alpha, @sub_alpha].max
                else
                        alpha = [alpha, @sub_alpha].min
                end
        end
        alpha
end