Class: ConnectFourCli::Computer
- Inherits:
-
Object
- Object
- ConnectFourCli::Computer
- Defined in:
- lib/connect_four_cli/computer.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#checker ⇒ Object
readonly
Returns the value of attribute checker.
Instance Method Summary collapse
-
#initialize(board) ⇒ Computer
constructor
A new instance of Computer.
- #make_move ⇒ Object
- #random_move ⇒ Object
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
#board ⇒ Object (readonly)
Returns the value of attribute board.
3 4 5 |
# File 'lib/connect_four_cli/computer.rb', line 3 def board @board end |
#checker ⇒ Object (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_move ⇒ Object
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_move ⇒ Object
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 |