Class: Sashite::GAN::Piece
- Inherits:
-
Object
- Object
- Sashite::GAN::Piece
- Defined in:
- lib/sashite/gan/piece.rb
Overview
A piece abstraction.
Instance Attribute Summary collapse
-
#abbr ⇒ Object
readonly
The abbreviation of the piece.
-
#style ⇒ Object
readonly
The piece’s style.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#bottomside ⇒ Piece
The bottom-side side version of the piece.
-
#bottomside? ⇒ Boolean
Is it owned by bottom-side player?.
- #eql?(other) ⇒ Boolean
-
#initialize(type, is_king:, is_promoted:, is_topside:, style:) ⇒ Piece
constructor
Initialize a piece.
- #inspect ⇒ Object
- #king? ⇒ Boolean
-
#oppositeside ⇒ Piece
The opposite side version of the piece.
-
#promote ⇒ Piece
The promoted version of the piece.
-
#to_s ⇒ String
The notation of the piece.
-
#topside ⇒ Piece
The top-side side version of the piece.
-
#topside? ⇒ Boolean
Is it owned by top-side player?.
-
#unpromote ⇒ Piece
The unpromoted version of the piece.
Constructor Details
#initialize(type, is_king:, is_promoted:, is_topside:, style:) ⇒ Piece
Initialize a piece.
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
#abbr ⇒ Object (readonly)
The abbreviation of the piece.
11 12 13 |
# File 'lib/sashite/gan/piece.rb', line 11 def abbr @abbr end |
#style ⇒ Object (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 |
#bottomside ⇒ Piece
Returns 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?
51 52 53 |
# File 'lib/sashite/gan/piece.rb', line 51 def bottomside? !topside? end |
#eql?(other) ⇒ Boolean
109 110 111 |
# File 'lib/sashite/gan/piece.rb', line 109 def eql?(other) self == other end |
#inspect ⇒ Object
61 62 63 |
# File 'lib/sashite/gan/piece.rb', line 61 def inspect to_s end |
#king? ⇒ Boolean
35 36 37 |
# File 'lib/sashite/gan/piece.rb', line 35 def king? abbr.king? end |
#oppositeside ⇒ Piece
Returns 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 |
#promote ⇒ Piece
Returns 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_s ⇒ String
Returns The notation of the piece.
57 58 59 |
# File 'lib/sashite/gan/piece.rb', line 57 def to_s topside? ? raw.downcase : raw.upcase end |
#topside ⇒ Piece
Returns 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?
43 44 45 |
# File 'lib/sashite/gan/piece.rb', line 43 def topside? @is_topside end |
#unpromote ⇒ Piece
Returns 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 |