class EightyOne::Piece

Attributes

turn[R]

Public Class Methods

decode(code) click to toggle source
# File lib/eighty-one/piece.rb, line 67
def self.decode(code)
  turn = code[0] == ?+ ? :sente : :gote
  symbol = code[1, 2].to_sym
  piece_class = Pieces::ALL.find{|p| p.forward.symbol == symbol }
  if piece_class
    piece_class.new(turn)
  else
    piece_class = ALL.find{|p| p.backword.symbol == symbol }
    if piece_class
      piece_class.new(turn).promote
    end
  end
end
new(turn) click to toggle source
# File lib/eighty-one/piece.rb, line 18
def initialize(turn)
  assert(turn == :sente || turn == :gote)
  @turn = turn
  @promoted = false
end
new_class(forward, backword) click to toggle source
# File lib/eighty-one/piece.rb, line 5
def self.new_class(forward, backword)
  Class.new(self) do |c|
    c.instance_eval do
      define_method(:forward) { forward }
      define_singleton_method(:forward) { forward }
      define_method(:backward) { backward }
      define_singleton_method(:backward) { backward }
    end
  end
end

Public Instance Methods

encode() click to toggle source
# File lib/eighty-one/piece.rb, line 59
def encode
  (@turn == :sente ? ?+ : ?-) + face.symbol.to_s
end
face() click to toggle source
# File lib/eighty-one/piece.rb, line 24
def face
  @promoted ? backward : forward
end
forward?() click to toggle source
# File lib/eighty-one/piece.rb, line 43
def forward?
  !@promoted
end
gote?() click to toggle source
# File lib/eighty-one/piece.rb, line 55
def gote?
  @turn == :gote
end
opposite() click to toggle source
# File lib/eighty-one/piece.rb, line 47
def opposite
  @turn.sente? ? :gote : :sente
end
promote() click to toggle source
# File lib/eighty-one/piece.rb, line 34
def promote
  @promoted = true
  self
end
promoted?() click to toggle source
reset(turn) click to toggle source
# File lib/eighty-one/piece.rb, line 28
def reset(turn)
  @turn = turn
  @promote = false
  self
end
sente?() click to toggle source
# File lib/eighty-one/piece.rb, line 51
def sente?
  @turn == :sente
end
to_s() click to toggle source
# File lib/eighty-one/piece.rb, line 63
def to_s
  self.encode
end