class Gomasio::Game

Constants

Command
Status

Attributes

order[R]

Public Class Methods

new(size: 19, order: Gomasio::Disk.black) { |self| ... } click to toggle source
# File lib/gomasio/game.rb, line 20
def initialize(size: 19, order: Gomasio::Disk.black)
  @board = Gomasio::Board.new(size)
  @order = order

  yield self if block_given?

  @runloop = Fiber.new do
    @board.clear
    runloop
  end
end

Public Instance Methods

<<(other) click to toggle source
# File lib/gomasio/game.rb, line 33
def <<(other)
  fail ArgumentError.new unless other[:player].is_a? Gomasio::Player
  fail ArgumentError.new unless other[:order].is_a? Gomasio::Disk

  @players ||= {}
  @players[other[:order]] = other[:player]
end
board() click to toggle source
# File lib/gomasio/game.rb, line 51
def board
  @board.to_matrix
end
ready?() click to toggle source
# File lib/gomasio/game.rb, line 47
def ready?
  @players.count == 2
end
run() click to toggle source
# File lib/gomasio/game.rb, line 56
def run
  @runloop.resume
end
size() click to toggle source
# File lib/gomasio/game.rb, line 42
def size
  @board.size
end

Private Instance Methods

runloop() click to toggle source
# File lib/gomasio/game.rb, line 63
def runloop
  @commands ||= []
  status = Status.new
  begin
    loop do
      pos = @players[@order].turn_responding(order: @order, status: status, board: @board.clone)
      raise OutOfRangeError.new unless @board.include?(pos.row, pos.col)

      _, win = @board.mount(row: pos.row, col: pos.col, disk: @order)
      @commands << Command.new(pos.row, pos.col, @order)
      status.clear

      Fiber.yield(win)
      @order = @order.reverse
    end
  rescue InSamePlaceError, OutOfRangeError => e
    status.retry     = true
    status.in_same   = true if e.is_a? InSamePlaceError
    status.out_range = true if e.is_a? OutOfRangeError
    retry
  end
end