Class: Computer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Indexable

#index_column

Constructor Details

#initialize(board) ⇒ Computer

Returns a new instance of Computer.



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

def initialize(board)
  @piece = 'o'
  @board = board
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



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

def board
  @board
end

#pieceObject (readonly)

Returns the value of attribute piece.



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

def piece
  @piece
end

Instance Method Details

#computer_turn(board) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/computer.rb', line 12

def computer_turn(board)
  # puts "The computer will now make a turn."
  column = @board.columns.sample
  integer = index_column(column)
  until @board.valid_column(column) == true
    column = @board.columns.sample
  end
  #if @board.valid_column(column) == true && @board.column_full(integer) == false
  integer = index_column(column)
  until @board.column_full(integer) == false
    column = @board.columns.sample
    integer = index_column(column)
  end
  board.drop_disc(integer, @piece)
end