Class: Tictactoe::State
- Inherits:
-
Object
- Object
- Tictactoe::State
- Defined in:
- lib/tictactoe/state.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#marks ⇒ Object
readonly
Returns the value of attribute marks.
Instance Method Summary collapse
- #available_moves ⇒ Object
-
#initialize(board, marks = [nil] * board.locations.length) ⇒ State
constructor
A new instance of State.
- #is_finished? ⇒ Boolean
- #make_move(location, mark) ⇒ Object
- #played_moves ⇒ Object
- #winner ⇒ Object
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
#board ⇒ Object (readonly)
Returns the value of attribute board.
3 4 5 |
# File 'lib/tictactoe/state.rb', line 3 def board @board end |
#marks ⇒ Object (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_moves ⇒ Object
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
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_moves ⇒ Object
14 15 16 |
# File 'lib/tictactoe/state.rb', line 14 def played_moves @played_moves ||= board.locations.length - available_moves.length end |
#winner ⇒ Object
28 29 30 |
# File 'lib/tictactoe/state.rb', line 28 def winner @winner ||= find_winner end |