class Rubykon::GameState
Attributes
game[R]
Public Class Methods
new(game = Game.new, validator = MoveValidator.new, eye_detector = EyeDetector.new)
click to toggle source
# 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
Public Instance Methods
all_valid_moves()
click to toggle source
# 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()
click to toggle source
# File lib/rubykon/game_state.rb, line 28 def dup self.class.new @game.dup, @validator, @eye_detector end
finished?()
click to toggle source
# File lib/rubykon/game_state.rb, line 14 def finished? @game.finished? end
generate_move()
click to toggle source
# File lib/rubykon/game_state.rb, line 18 def generate_move generate_random_move end
last_turn_color()
click to toggle source
# File lib/rubykon/game_state.rb, line 48 def last_turn_color Game.other_color(next_turn_color) end
score()
click to toggle source
# File lib/rubykon/game_state.rb, line 44 def score @score ||= GameScorer.new.score(@game) end
set_move(move)
click to toggle source
# 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)
click to toggle source
# File lib/rubykon/game_state.rb, line 32 def won?(color) score[:winner] == color end
Private Instance Methods
generate_random_move()
click to toggle source
# File lib/rubykon/game_state.rb, line 53 def generate_random_move color = @game.next_turn_color cp_count = @game.board.cutting_point_count start_point = rand(cp_count) identifier = start_point passes = 0 until searched_whole_board?(identifier, passes, start_point) || plausible_move?(identifier, color) do if identifier >= cp_count - 1 identifier = 0 passes += 1 else identifier += 1 end end if searched_whole_board?(identifier, passes, start_point) pass_move(color) else [identifier, color] end end def searched_whole_board?(identifier, passes, start_point) passes > 0 && identifier >= start_point end def pass_move(color) [nil, color] end def next_turn_color @game.next_turn_color end def plausible_move?(identifier, color) @validator.trusted_valid?(identifier, color, @game) && !@eye_detector.is_eye?(identifier, @game.board) end end
next_turn_color()
click to toggle source
# File lib/rubykon/game_state.rb, line 85 def next_turn_color @game.next_turn_color end
pass_move(color)
click to toggle source
# File lib/rubykon/game_state.rb, line 81 def pass_move(color) [nil, color] end
plausible_move?(identifier, color)
click to toggle source
# File lib/rubykon/game_state.rb, line 89 def plausible_move?(identifier, color) @validator.trusted_valid?(identifier, color, @game) && !@eye_detector.is_eye?(identifier, @game.board) end
searched_whole_board?(identifier, passes, start_point)
click to toggle source
# File lib/rubykon/game_state.rb, line 77 def searched_whole_board?(identifier, passes, start_point) passes > 0 && identifier >= start_point end