class Sashite::GAN::Piece

A piece abstraction.

Attributes

abbr[R]

The abbreviation of the piece.

@!attribute [r] abbr

@return [String] The abbreviation of the piece.
style[R]

The piece's style.

@!attribute [r] style

@return [String] The piece's style.

Public Class Methods

new(type, is_king:, is_promoted:, is_topside:, style:) click to toggle source

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

==(other) click to toggle source
# File lib/sashite/gan/piece.rb, line 105
def ==(other)
  other.to_s == to_s
end
bottomside() click to toggle source

@return [Piece] The bottom-side side version of the piece.

# File lib/sashite/gan/piece.rb, line 71
def bottomside
  topside? ? oppositeside : self
end
bottomside?() click to toggle source

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
eql?(other) click to toggle source
# File lib/sashite/gan/piece.rb, line 109
def eql?(other)
  self == other
end
inspect() click to toggle source
# File lib/sashite/gan/piece.rb, line 61
def inspect
  to_s
end
king?() click to toggle source
# File lib/sashite/gan/piece.rb, line 35
def king?
  abbr.king?
end
oppositeside() click to toggle source

@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
promote() click to toggle source

@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
to_s() click to toggle source

@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
topside() click to toggle source

@return [Piece] The top-side side version of the piece.

# File lib/sashite/gan/piece.rb, line 66
def topside
  topside? ? self : oppositeside
end
topside?() click to toggle source

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
unpromote() click to toggle source

@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

Boolean(arg) click to toggle source

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
StyleString(arg) click to toggle source

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
params() click to toggle source

@return [Array] The style and the abbreviation of the piece.

# File lib/sashite/gan/piece.rb, line 122
def params
  [style, abbr]
end
raw() click to toggle source

@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