Class: Tictactoe::State

Inherits:
Object
  • Object
show all
Defined in:
lib/tictactoe/state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board, marks = [nil] * board.locations.length) ⇒ State

Returns a new instance of State.



5
6
7
8
# File 'lib/tictactoe/state.rb', line 5

def initialize(board, marks=[nil] * board.locations.length)
  @board = board
  @marks = marks
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



3
4
5
# File 'lib/tictactoe/state.rb', line 3

def board
  @board
end

#marksObject (readonly)

Returns the value of attribute marks.



3
4
5
# File 'lib/tictactoe/state.rb', line 3

def marks
  @marks
end

Instance Method Details

#available_movesObject



10
11
12
# File 'lib/tictactoe/state.rb', line 10

def available_moves
  @available ||= board.locations.select{|location| marks[location].nil?}
end

#is_finished?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/tictactoe/state.rb', line 24

def is_finished?
  is_full? || has_winner?
end

#make_move(location, mark) ⇒ Object



18
19
20
21
22
# File 'lib/tictactoe/state.rb', line 18

def make_move(location, mark)
  new_marks = marks.clone
  new_marks[location] = mark
  self.class.new(board, new_marks)
end

#played_movesObject



14
15
16
# File 'lib/tictactoe/state.rb', line 14

def played_moves
  @played_moves ||= board.locations.length - available_moves.length
end

#winnerObject



28
29
30
# File 'lib/tictactoe/state.rb', line 28

def winner
  @winner ||= find_winner
end