Class: ConnectFourCli::Computer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board) ⇒ Computer

Returns a new instance of Computer.



5
6
7
8
# File 'lib/connect_four_cli/computer.rb', line 5

def initialize(board)
  @board = board
  @checker = Checker.new(:yellow)
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



3
4
5
# File 'lib/connect_four_cli/computer.rb', line 3

def board
  @board
end

#checkerObject (readonly)

Returns the value of attribute checker.



3
4
5
# File 'lib/connect_four_cli/computer.rb', line 3

def checker
  @checker
end

Instance Method Details

#make_moveObject



16
17
18
19
20
21
22
23
# 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_moveObject



10
11
12
13
14
# 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