Class: Computer

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

Constant Summary collapse

HUMAN =
"X"
COMPUTER =
"O"
MAX_DEPTH =

to control length of calc time

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeComputer

Returns a new instance of Computer.



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

def initialize
  @sub_alpha = []
  @best_move = 0
  @best_moves =[]
end

Instance Attribute Details

#active_playerObject

Returns the value of attribute active_player.



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

def active_player
  @active_player
end

#best_moveObject

Returns the value of attribute best_move.



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

def best_move
  @best_move
end

#sub_alphaObject

Returns the value of attribute sub_alpha.



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

def sub_alpha
  @sub_alpha
end

Instance Method Details

#check_winner(board, depth) ⇒ Object



39
40
41
42
43
44
45
46
# 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) ⇒ Object



15
16
17
18
19
20
# 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) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object



35
36
37
# 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) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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