Class: Sashite::GAN::Piece

Inherits:
Object
  • Object
show all
Defined in:
lib/sashite/gan/piece.rb

Overview

A piece abstraction.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, is_king:, is_promoted:, is_topside:, style:) ⇒ Piece

Initialize a piece.

Parameters:

  • type (String)

    The type of the piece.

  • is_king (Boolean)

    Is it a King (or a Xiangqi General), so it could be checkmated?

  • is_promoted (Boolean)

    Is it promoted?

  • is_topside (Boolean)

    Is it owned by top-side player?

  • style (String)

    The piece’s style.



27
28
29
30
31
32
33
# 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

Instance Attribute Details

#abbrObject (readonly)

The abbreviation of the piece.



11
12
13
# File 'lib/sashite/gan/piece.rb', line 11

def abbr
  @abbr
end

#styleObject (readonly)

The piece’s style.



17
18
19
# File 'lib/sashite/gan/piece.rb', line 17

def style
  @style
end

Instance Method Details

#==(other) ⇒ Object



105
106
107
# File 'lib/sashite/gan/piece.rb', line 105

def ==(other)
  other.to_s == to_s
end

#bottomsidePiece

Returns The bottom-side side version of the piece.

Returns:

  • (Piece)

    The bottom-side side version of the piece.



71
72
73
# File 'lib/sashite/gan/piece.rb', line 71

def bottomside
  topside? ? oppositeside : self
end

#bottomside?Boolean

Is it owned by bottom-side player?

Returns:

  • (Boolean)

    Returns ‘true` if the bottom-side player own the piece, `false` otherwise.



51
52
53
# File 'lib/sashite/gan/piece.rb', line 51

def bottomside?
  !topside?
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/sashite/gan/piece.rb', line 109

def eql?(other)
  self == other
end

#inspectObject



61
62
63
# File 'lib/sashite/gan/piece.rb', line 61

def inspect
  to_s
end

#king?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/sashite/gan/piece.rb', line 35

def king?
  abbr.king?
end

#oppositesidePiece

Returns The opposite side version of the piece.

Returns:

  • (Piece)

    The opposite side version of the piece.



76
77
78
79
80
81
82
83
# 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

#promotePiece

Returns The promoted version of the piece.

Returns:

  • (Piece)

    The promoted version of the piece.



86
87
88
89
90
91
92
93
# 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_sString

Returns The notation of the piece.

Returns:

  • (String)

    The notation of the piece.

See Also:



57
58
59
# File 'lib/sashite/gan/piece.rb', line 57

def to_s
  topside? ? raw.downcase : raw.upcase
end

#topsidePiece

Returns The top-side side version of the piece.

Returns:

  • (Piece)

    The top-side side version of the piece.



66
67
68
# File 'lib/sashite/gan/piece.rb', line 66

def topside
  topside? ? self : oppositeside
end

#topside?Boolean

Is it owned by top-side player?

Returns:

  • (Boolean)

    Returns ‘true` if the top-side player own the piece, `false` otherwise.



43
44
45
# File 'lib/sashite/gan/piece.rb', line 43

def topside?
  @is_topside
end

#unpromotePiece

Returns The unpromoted version of the piece.

Returns:

  • (Piece)

    The unpromoted version of the piece.



96
97
98
99
100
101
102
103
# 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