class ConnectFourCli::Computer

Attributes

board[R]
checker[R]

Public Class Methods

new(board) click to toggle source
# File lib/connect_four_cli/computer.rb, line 5
def initialize(board)
  @board = board
  @checker = Checker.new(:yellow)
end

Public Instance Methods

make_move() click to toggle source
# File lib/connect_four_cli/computer.rb, line 16
def make_move
  if board.winning_moves.length > 0
    move = board.winning_moves[0][0]
  else
    move = random_move
  end
  board.place_checker(checker, at: move)
end
random_move() click to toggle source
# File lib/connect_four_cli/computer.rb, line 10
def random_move
  (0...board.size).map do |i|
    board.column_full?(i) ? nil : i
  end.compact.sample
end