Class: ComputerPlayer

Inherits:
Object
  • Object
show all
Defined in:
lib/ttt_malisa/computer_player.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mark, board, display) ⇒ ComputerPlayer

Returns a new instance of ComputerPlayer.



4
5
6
7
8
# File 'lib/ttt_malisa/computer_player.rb', line 4

def initialize(mark, board, display)
  @board = board
  @display = display
  @mark = mark
end

Instance Attribute Details

#markObject (readonly)

Returns the value of attribute mark.



2
3
4
# File 'lib/ttt_malisa/computer_player.rb', line 2

def mark
  @mark
end

Instance Method Details

#moveObject



10
11
12
13
14
15
16
17
# File 'lib/ttt_malisa/computer_player.rb', line 10

def move
  @display.make_move_message(@mark)
  index = retrieve_computer_index(1)
  @display.computer_thinking
  sleep 1
  @display.computer_move(index + 1)
  @board.valid_move?(index) ? @board.mark(index, @mark) : move
end

#retrieve_computer_index(seed) ⇒ Object



19
20
21
22
23
# File 'lib/ttt_malisa/computer_player.rb', line 19

def retrieve_computer_index(seed)
  srand(seed)
  available_move = @board.available_moves
  available_move[0]
end