Class: Rubykon::GameState
- Inherits:
-
Object
- Object
- Rubykon::GameState
- Defined in:
- lib/rubykon/game_state.rb
Instance Attribute Summary collapse
-
#game ⇒ Object
readonly
Returns the value of attribute game.
Instance Method Summary collapse
- #all_valid_moves ⇒ Object
- #dup ⇒ Object
- #finished? ⇒ Boolean
- #generate_move ⇒ Object
-
#initialize(game = Game.new, validator = MoveValidator.new, eye_detector = EyeDetector.new) ⇒ GameState
constructor
A new instance of GameState.
- #last_turn_color ⇒ Object
- #score ⇒ Object
- #set_move(move) ⇒ Object
- #won?(color) ⇒ Boolean
Constructor Details
#initialize(game = Game.new, validator = MoveValidator.new, eye_detector = EyeDetector.new) ⇒ GameState
Returns a new instance of GameState.
6 7 8 9 10 11 12 |
# File 'lib/rubykon/game_state.rb', line 6 def initialize(game = Game.new, validator = MoveValidator.new, eye_detector = EyeDetector.new) @game = game @validator = validator @eye_detector = eye_detector end |
Instance Attribute Details
#game ⇒ Object (readonly)
Returns the value of attribute game.
4 5 6 |
# File 'lib/rubykon/game_state.rb', line 4 def game @game end |
Instance Method Details
#all_valid_moves ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/rubykon/game_state.rb', line 36 def all_valid_moves color = @game.next_turn_color @game.board.inject([]) do |valid_moves, (identifier, _field_color)| valid_moves << [identifier, color] if plausible_move?(identifier, color) valid_moves end end |
#dup ⇒ Object
28 29 30 |
# File 'lib/rubykon/game_state.rb', line 28 def dup self.class.new @game.dup, @validator, @eye_detector end |
#finished? ⇒ Boolean
14 15 16 |
# File 'lib/rubykon/game_state.rb', line 14 def finished? @game.finished? end |
#generate_move ⇒ Object
18 19 20 |
# File 'lib/rubykon/game_state.rb', line 18 def generate_move generate_random_move end |
#last_turn_color ⇒ Object
48 49 50 |
# File 'lib/rubykon/game_state.rb', line 48 def last_turn_color Game.other_color(next_turn_color) end |
#score ⇒ Object
44 45 46 |
# File 'lib/rubykon/game_state.rb', line 44 def score @score ||= GameScorer.new.score(@game) end |
#set_move(move) ⇒ Object
22 23 24 25 26 |
# File 'lib/rubykon/game_state.rb', line 22 def set_move(move) identifier = move.first color = move.last @game.set_valid_move identifier, color end |
#won?(color) ⇒ Boolean
32 33 34 |
# File 'lib/rubykon/game_state.rb', line 32 def won?(color) score[:winner] == color end |