Class: ComputerAI

Inherits:
Object
  • Object
show all
Defined in:
lib/computer_ai.rb

Constant Summary collapse

MIN_ALPHA =
-1.0/0
MAX_BETA =
1.0/0
TOP_DEPTH =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol, threshold) ⇒ ComputerAI

Returns a new instance of ComputerAI.



9
10
11
12
13
# File 'lib/computer_ai.rb', line 9

def initialize symbol, threshold 
    @threshold = threshold
    @symbol = symbol
    @type = :ai
end

Instance Attribute Details

#symbolObject (readonly)

Returns the value of attribute symbol.



7
8
9
# File 'lib/computer_ai.rb', line 7

def symbol
  @symbol
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



7
8
9
# File 'lib/computer_ai.rb', line 7

def threshold
  @threshold
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/computer_ai.rb', line 7

def type
  @type
end

Instance Method Details

#make_move(board) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/computer_ai.rb', line 15

def make_move board
    update_depth_limit(board)
    if use_mini_max? then best_options = mini_max_top(board, @symbol)
    else best_options = make_random_move(board) end
    choice = best_options.sample
    board.record_choice(choice, @symbol)
    choice
end

#make_random_move(board) ⇒ Object



68
69
70
# File 'lib/computer_ai.rb', line 68

def make_random_move board
    board.get_open_spots
end

#mini_max(board, player, depth, alpha, beta) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/computer_ai.rb', line 42

def mini_max board, player, depth, alpha, beta
    if depth >= @depth_limit || alpha >= beta then return 0 end

    if board.check_board_status == :tie 
        return 0
    elsif board.check_board_status != :continue_game 
        return -(@depth_limit/depth)
    end

    open_moves = board.get_open_spots
    open_moves.each do |move|
        board.record_choice(move,player)
        score = -mini_max(board, opponent(player), depth+1, -beta,-alpha)
        board.remove_choice(move)
        alpha = score if score > alpha
    end
    alpha
end

#mini_max_top(board, player) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/computer_ai.rb', line 24

def mini_max_top board, player
    best_moves = []
    best_score = -1.0/0
    open_moves = board.get_open_spots
    open_moves.each do |move|
        board.record_choice(move,player)
        score = -mini_max(board, opponent(player), TOP_DEPTH+1, MIN_ALPHA, MAX_BETA)
        board.remove_choice(move)
        if score > best_score
            best_score = score
            best_moves.clear << move
        elsif score == best_score
            best_moves << move
        end
    end
    best_moves
end

#opponent(player) ⇒ Object



76
77
78
# File 'lib/computer_ai.rb', line 76

def opponent player
    if player == :o then return :x else return :o end
end

#update_depth_limit(board) ⇒ Object



61
62
63
64
65
66
# File 'lib/computer_ai.rb', line 61

def update_depth_limit board
    if board.get_open_spots.size > 18 then @depth_limit = 4
    elsif board.get_open_spots.size > 14 then @depth_limit = 5 
    elsif board.get_open_spots.size > 8 then @depth_limit = 6
    else @depth_limit = 7 end
end

#use_mini_max?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/computer_ai.rb', line 72

def use_mini_max?
    if rand(100) < @threshold then true else false end
end