Class: ComputerPlayer
- Inherits:
-
Object
- Object
- ComputerPlayer
- Defined in:
- lib/ttt_malisa/computer_player.rb
Instance Attribute Summary collapse
-
#mark ⇒ Object
readonly
Returns the value of attribute mark.
Instance Method Summary collapse
-
#initialize(mark, board, display) ⇒ ComputerPlayer
constructor
A new instance of ComputerPlayer.
- #move ⇒ Object
- #retrieve_computer_index(seed) ⇒ Object
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
#mark ⇒ Object (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
#move ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/ttt_malisa/computer_player.rb', line 10 def move @display.(@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 |