sashite-gan 2.2.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,150 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Sashite
4
- module GAN
5
- # A piece abstraction.
6
- class Piece
7
- # The abbreviation of the piece.
8
- #
9
- # @!attribute [r] abbr
10
- # @return [String] The abbreviation of the piece.
11
- attr_reader :abbr
12
-
13
- # The piece's style.
14
- #
15
- # @!attribute [r] style
16
- # @return [String] The piece's style.
17
- attr_reader :style
18
-
19
- # Initialize a piece.
20
- #
21
- # @param type [String] The type of the piece.
22
- # @param is_king [Boolean] Is it a King (or a Xiangqi General),
23
- # so it could be checkmated?
24
- # @param is_promoted [Boolean] Is it promoted?
25
- # @param is_topside [Boolean] Is it owned by top-side player?
26
- # @param style [String] The piece's style.
27
- def initialize(type, is_king:, is_promoted:, is_topside:, style:)
28
- @abbr = Abbr.new(type, is_king: is_king, is_promoted: is_promoted)
29
- @is_topside = Boolean(is_topside)
30
- @style = StyleString(style)
31
-
32
- freeze
33
- end
34
-
35
- def king?
36
- abbr.king?
37
- end
38
-
39
- # Is it owned by top-side player?
40
- #
41
- # @return [Boolean] Returns `true` if the top-side player own the piece,
42
- # `false` otherwise.
43
- def topside?
44
- @is_topside
45
- end
46
-
47
- # Is it owned by bottom-side player?
48
- #
49
- # @return [Boolean] Returns `true` if the bottom-side player own the
50
- # piece, `false` otherwise.
51
- def bottomside?
52
- !topside?
53
- end
54
-
55
- # @see https://developer.sashite.com/specs/general-actor-notation
56
- # @return [String] The notation of the piece.
57
- def to_s
58
- topside? ? raw.downcase : raw.upcase
59
- end
60
-
61
- def inspect
62
- to_s
63
- end
64
-
65
- # @return [Piece] The top-side side version of the piece.
66
- def topside
67
- topside? ? self : oppositeside
68
- end
69
-
70
- # @return [Piece] The bottom-side side version of the piece.
71
- def bottomside
72
- topside? ? oppositeside : self
73
- end
74
-
75
- # @return [Piece] The opposite side version of the piece.
76
- def oppositeside
77
- self.class.new(abbr.type,
78
- is_king: abbr.king?,
79
- is_promoted: abbr.promoted?,
80
- is_topside: !topside?,
81
- style: style
82
- )
83
- end
84
-
85
- # @return [Piece] The promoted version of the piece.
86
- def promote
87
- self.class.new(abbr.type,
88
- is_king: abbr.king?,
89
- is_promoted: true,
90
- is_topside: topside?,
91
- style: style
92
- )
93
- end
94
-
95
- # @return [Piece] The unpromoted version of the piece.
96
- def unpromote
97
- self.class.new(abbr.type,
98
- is_king: abbr.king?,
99
- is_promoted: false,
100
- is_topside: topside?,
101
- style: style
102
- )
103
- end
104
-
105
- def ==(other)
106
- other.to_s == to_s
107
- end
108
-
109
- def eql?(other)
110
- self == other
111
- end
112
-
113
- private
114
-
115
- # @return [String] The style and the abbreviation of the piece (without
116
- # case).
117
- def raw
118
- params.join(SEPARATOR_CHAR)
119
- end
120
-
121
- # @return [Array] The style and the abbreviation of the piece.
122
- def params
123
- [style, abbr]
124
- end
125
-
126
- # rubocop:disable Naming/MethodName
127
-
128
- # Ensures `arg` is a boolean, and returns it. Otherwise, raises a
129
- # `TypeError`.
130
- def Boolean(arg)
131
- raise ::TypeError, arg.class.inspect unless [false, true].include?(arg)
132
-
133
- arg
134
- end
135
-
136
- # Ensures `arg` is a style, and returns it. Otherwise, raises an error.
137
- def StyleString(arg)
138
- raise ::TypeError, arg.class.inspect unless arg.is_a?(::String)
139
- raise Error::Style, arg.inspect unless arg.match?(/\A[a-z_]+\z/i)
140
-
141
- arg
142
- end
143
-
144
- # rubocop:enable Naming/MethodName
145
- end
146
- end
147
- end
148
-
149
- require_relative 'abbr'
150
- require_relative 'error'