class Sashite::GAN::Piece
A piece abstraction.
Attributes
The abbreviation of the piece.
@!attribute [r] abbr
@return [String] The abbreviation of the piece.
The piece's style.
@!attribute [r] style
@return [String] The piece's style.
Public Class Methods
Initialize a piece.
@param type [String] The type of the piece. @param is_king [Boolean] Is it a King (or a Xiangqi General),
so it could be checkmated?
@param is_promoted [Boolean] Is it promoted? @param is_topside [Boolean] Is it owned by top-side player? @param style [String] The piece's style.
# File lib/sashite/gan/piece.rb, line 27 def initialize(type, is_king:, is_promoted:, is_topside:, style:) @abbr = Abbr.new(type, is_king: is_king, is_promoted: is_promoted) @is_topside = Boolean(is_topside) @style = StyleString(style) freeze end
Public Instance Methods
# File lib/sashite/gan/piece.rb, line 105 def ==(other) other.to_s == to_s end
@return [Piece] The bottom-side side version of the piece.
# File lib/sashite/gan/piece.rb, line 71 def bottomside topside? ? oppositeside : self end
Is it owned by bottom-side player?
@return [Boolean] Returns `true` if the bottom-side player own the
piece, `false` otherwise.
# File lib/sashite/gan/piece.rb, line 51 def bottomside? !topside? end
# File lib/sashite/gan/piece.rb, line 109 def eql?(other) self == other end
# File lib/sashite/gan/piece.rb, line 61 def inspect to_s end
# File lib/sashite/gan/piece.rb, line 35 def king? abbr.king? end
@return [Piece] The opposite side version of the piece.
# File lib/sashite/gan/piece.rb, line 76 def oppositeside self.class.new(abbr.type, is_king: abbr.king?, is_promoted: abbr.promoted?, is_topside: !topside?, style: style ) end
@return [Piece] The promoted version of the piece.
# File lib/sashite/gan/piece.rb, line 86 def promote self.class.new(abbr.type, is_king: abbr.king?, is_promoted: true, is_topside: topside?, style: style ) end
@see developer.sashite.com/specs/general-actor-notation @return [String] The notation of the piece.
# File lib/sashite/gan/piece.rb, line 57 def to_s topside? ? raw.downcase : raw.upcase end
@return [Piece] The top-side side version of the piece.
# File lib/sashite/gan/piece.rb, line 66 def topside topside? ? self : oppositeside end
Is it owned by top-side player?
@return [Boolean] Returns `true` if the top-side player own the piece,
`false` otherwise.
# File lib/sashite/gan/piece.rb, line 43 def topside? @is_topside end
@return [Piece] The unpromoted version of the piece.
# File lib/sashite/gan/piece.rb, line 96 def unpromote self.class.new(abbr.type, is_king: abbr.king?, is_promoted: false, is_topside: topside?, style: style ) end
Private Instance Methods
Ensures `arg` is a boolean, and returns it. Otherwise, raises a
`TypeError`.
# File lib/sashite/gan/piece.rb, line 130 def Boolean(arg) raise ::TypeError, arg.class.inspect unless [false, true].include?(arg) arg end
Ensures `arg` is a style, and returns it. Otherwise, raises an error.
# File lib/sashite/gan/piece.rb, line 137 def StyleString(arg) raise ::TypeError, arg.class.inspect unless arg.is_a?(::String) raise Error::Style, arg.inspect unless arg.match?(/\A[a-z_]+\z/i) arg end
@return [Array] The style and the abbreviation of the piece.
# File lib/sashite/gan/piece.rb, line 122 def params [style, abbr] end
@return [String] The style and the abbreviation of the piece (without
case).
# File lib/sashite/gan/piece.rb, line 117 def raw params.join(SEPARATOR_CHAR) end