Class: Rubykon::GameState

Inherits:
Object
  • Object
show all
Defined in:
lib/rubykon/game_state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#gameObject (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_movesObject



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

#dupObject



28
29
30
# File 'lib/rubykon/game_state.rb', line 28

def dup
  self.class.new @game.dup, @validator, @eye_detector
end

#finished?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/rubykon/game_state.rb', line 14

def finished?
  @game.finished?
end

#generate_moveObject



18
19
20
# File 'lib/rubykon/game_state.rb', line 18

def generate_move
  generate_random_move
end

#last_turn_colorObject



48
49
50
# File 'lib/rubykon/game_state.rb', line 48

def last_turn_color
  Game.other_color(next_turn_color)
end

#scoreObject



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

Returns:

  • (Boolean)


32
33
34
# File 'lib/rubykon/game_state.rb', line 32

def won?(color)
  score[:winner] == color
end