class Rubykon::Game

Constants

DEFAULT_KOMI

Attributes

board[R]
captures[R]
group_tracker[R]
ko[R]
komi[RW]
move_count[R]

Public Class Methods

from(string) click to toggle source
# File lib/rubykon/game.rb, line 81
def self.from(string)
  game = new(string.index("\n") / Board::CHARS_PER_GLYPH)
  Board.each_move_from(string) do |identifier, color|
    game.safe_set_move(identifier, color)
  end
  game
end
new(size = 19, komi = DEFAULT_KOMI, board = Board.new(size), move_count = 0, consecutive_passes = 0, ko = nil, captures = initial_captures, move_validator = MoveValidator.new, group_tracker = GroupTracker.new) click to toggle source

the freakish constructor is here so that we can have a decent dup

# File lib/rubykon/game.rb, line 9
def initialize(size = 19, komi = DEFAULT_KOMI, board = Board.new(size),
               move_count = 0, consecutive_passes = 0,
               ko = nil, captures = initial_captures,
               move_validator = MoveValidator.new,
               group_tracker = GroupTracker.new)
  @board              = board
  @komi               = komi
  @move_count         = move_count
  @consecutive_passes = consecutive_passes
  @ko                 = ko
  @captures           = captures
  @move_validator     = move_validator
  @group_tracker      = group_tracker
end
other_color(color) click to toggle source
# File lib/rubykon/game.rb, line 69
def self.other_color(color)
  if color == :black
    :white
  else
    :black
  end
end
pass?(identifier) click to toggle source
# File lib/rubykon/game.rb, line 77
def self.pass?(identifier)
  identifier.nil?
end

Public Instance Methods

dup() click to toggle source
# File lib/rubykon/game.rb, line 64
def dup
  self.class.new @size, @komi, @board.dup, @move_count, @consecutive_passes,
                 @ko, @captures.dup, @move_validator, @group_tracker.dup
end
finished?() click to toggle source
# File lib/rubykon/game.rb, line 46
def finished?
  @consecutive_passes >= 2
end
next_turn_color() click to toggle source
# File lib/rubykon/game.rb, line 42
def next_turn_color
  move_count.even? ? Board::BLACK : Board::WHITE
end
no_moves_played?() click to toggle source
# File lib/rubykon/game.rb, line 38
def no_moves_played?
  @move_count == 0
end
play(x, y, color) click to toggle source
# File lib/rubykon/game.rb, line 24
def play(x, y, color)
  identifier = @board.identifier_for(x, y)
  if valid_move?(identifier, color)
    set_valid_move(identifier, color)
    true
  else
    false
  end
end
play!(x, y, color) click to toggle source
# File lib/rubykon/game.rb, line 34
def play!(x, y, color)
  raise IllegalMoveException unless play(x, y, color)
end
safe_set_move(identifier, color) click to toggle source
# File lib/rubykon/game.rb, line 59
def safe_set_move(identifier, color)
  return if color == Board::EMPTY
  set_valid_move(identifier, color)
end
set_valid_move(identifier, color) click to toggle source
# File lib/rubykon/game.rb, line 50
def set_valid_move(identifier, color)
  @move_count += 1
  if Game.pass?(identifier)
    @consecutive_passes += 1
  else
    set_move(color, identifier)
  end
end

Private Instance Methods

determine_ko_move(captures, potential_eye) click to toggle source
# File lib/rubykon/game.rb, line 107
def determine_ko_move(captures, potential_eye)
  if captures.size == 1 && potential_eye
    @ko = captures[0]
  else
    @ko = nil
  end
end
initial_captures() click to toggle source
# File lib/rubykon/game.rb, line 90
def initial_captures
  {Board::BLACK => 0, Board::WHITE => 0}
end
set_move(color, identifier) click to toggle source
# File lib/rubykon/game.rb, line 98
def set_move(color, identifier)
  @board[identifier] = color
  potential_eye = EyeDetector.new.candidate_eye_color(identifier, @board)
  captures = @group_tracker.assign(identifier, color, board)
  determine_ko_move(captures, potential_eye)
  @captures[color] += captures.size
  @consecutive_passes = 0
end
valid_move?(identifier, color) click to toggle source
# File lib/rubykon/game.rb, line 94
def valid_move?(identifier, color)
  @move_validator.valid?(identifier, color, self)
end