qi 10.0.0.beta7 → 10.0.0.beta8

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -5
  3. data/lib/qi/error/drop.rb +8 -0
  4. data/lib/qi.rb +34 -11
  5. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e850bab495ea23b4c438ec54439eb4b4b0486d0b6d1afd23f9d9d870eca46e43
4
- data.tar.gz: 0f0e3271857d1a513069416bcb246b439f3fb99b3f550a9a9f1e1bc15b286a35
3
+ metadata.gz: 816f97762efb0e9f7a603b93b5703b4b68c6a4629f438f39ed49d09227b9650a
4
+ data.tar.gz: 1c90d60785447ff965ef92816ed58e7a0d315ffb9b104c185c76d4d1da461435
5
5
  SHA512:
6
- metadata.gz: c94b483676c67796e208e2b354420c9fd20d0f286d2e3f4c6145a70945d61f601b8803af2ebed7bca964435881a27499a63f18151073f8bcf04126a65b265e2a
7
- data.tar.gz: 62665dedc27de056316e923fcf5e6e604de42c399b00b2b015b5a2e44049fae9f05cee3a95d08aaaf655f77a54f1ae923ebcfe69ec373ba37657aa45bb656b52
6
+ metadata.gz: '0906bf4de292b88d43e88241a8690753ca6776320de3070a5f575eb44070913a68807dbf25e9836a46c0361e43fdd6cbe5f869c558695828174f7dbc548934b0'
7
+ data.tar.gz: 495dd33b36e4141be5d79e989b54c735f7e86edd52b323780448f19ba16530b125564ccfb3ac9d0a668c3d841d03c889a733e5775d52f2cf210ed7d0448d719b
data/README.md CHANGED
@@ -13,7 +13,7 @@
13
13
  Add this line to your application's Gemfile:
14
14
 
15
15
  ```ruby
16
- gem "qi", ">= 10.0.0.beta7"
16
+ gem "qi", ">= 10.0.0.beta8"
17
17
  ```
18
18
 
19
19
  And then execute:
@@ -45,8 +45,8 @@ qi0.south_captures # => ["S"]
45
45
  qi0.squares # => {3=>"s", 4=>"k", 5=>"s", 22=>"+P", 43=>"+B"}
46
46
  qi0.north_turn? # => false
47
47
  qi0.south_turn? # => true
48
- qi0.serialize # => "SouthTurn===S,b,g,g,g,g,n,n,n,n,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,r,r,s===3:s,4:k,5:s,22:+P,43:+B"
49
- qi0.inspect # => "<Qi SouthTurn===S,b,g,g,g,g,n,n,n,n,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,r,r,s===3:s,4:k,5:s,22:+P,43:+B>"
48
+ qi0.serialize # => "south-turn===S,b,g,g,g,g,n,n,n,n,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,r,r,s===3:s,4:k,5:s,22:+P,43:+B"
49
+ qi0.inspect # => "<Qi south-turn===S,b,g,g,g,g,n,n,n,n,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,r,r,s===3:s,4:k,5:s,22:+P,43:+B>"
50
50
 
51
51
  qi0.to_a
52
52
  # [false,
@@ -61,8 +61,8 @@ qi1.south_captures # => ["S"]
61
61
  qi1.squares # => {3=>"s", 4=>"k", 5=>"s", 22=>"+P", 13=>"+B"}
62
62
  qi1.north_turn? # => true
63
63
  qi1.south_turn? # => false
64
- qi1.serialize # => "NorthTurn===S,b,g,g,g,g,n,n,n,n,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,r,r,s===3:s,4:k,5:s,22:+P,13:+B"
65
- qi1.inspect # => "<Qi NorthTurn===S,b,g,g,g,g,n,n,n,n,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,r,r,s===3:s,4:k,5:s,22:+P,13:+B>"
64
+ qi1.serialize # => "north-turn===S,b,g,g,g,g,n,n,n,n,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,r,r,s===3:s,4:k,5:s,22:+P,13:+B"
65
+ qi1.inspect # => "<Qi north-turn===S,b,g,g,g,g,n,n,n,n,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,p,r,r,s===3:s,4:k,5:s,22:+P,13:+B>"
66
66
 
67
67
  qi1.to_a
68
68
  # [true,
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Qi
4
+ module Error
5
+ class Drop < ::IndexError
6
+ end
7
+ end
8
+ end
data/lib/qi.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "qi/error/drop"
4
+
3
5
  # A class that represents the state of a game.
4
6
  class Qi
5
7
  # @!attribute [r] north_captures
@@ -39,6 +41,30 @@ class Qi
39
41
  self.class.new(!north_turn?, *modified_captures, modified_squares)
40
42
  end
41
43
 
44
+ def side_name
45
+ if north_turn?
46
+ "north"
47
+ else
48
+ "south"
49
+ end
50
+ end
51
+
52
+ def other_captures
53
+ if north_turn?
54
+ south_captures
55
+ else
56
+ north_captures
57
+ end
58
+ end
59
+
60
+ def owned_captures
61
+ if north_turn?
62
+ north_captures
63
+ else
64
+ south_captures
65
+ end
66
+ end
67
+
42
68
  # Checks if it is the north turn or not.
43
69
  #
44
70
  # @return [Boolean] true if it is the north turn and false otherwise
@@ -92,7 +118,7 @@ class Qi
92
118
  # - the captures, sorted and joined by ","
93
119
  # - the squares, mapped to "coordinate:piece" pairs and joined by ","
94
120
  def serialize
95
- serialized_turn = (north_turn? ? "NorthTurn" : "SouthTurn")
121
+ serialized_turn = "#{side_name}-turn"
96
122
  serialized_captures = (north_captures + south_captures).sort.join(",")
97
123
  serialized_squares = squares.keys.map { |i| "#{i}:#{squares.fetch(i)}" }.join(",")
98
124
 
@@ -116,14 +142,11 @@ class Qi
116
142
  def update_captures(in_hand, is_drop:)
117
143
  return [north_captures, south_captures] if in_hand.nil?
118
144
 
119
- captures = north_turn? ? north_captures : south_captures
120
- other_captures = north_turn? ? south_captures : north_captures
121
-
122
- if is_drop
123
- captures = remove_from_captures(in_hand, *captures)
124
- else
125
- captures += [in_hand]
126
- end
145
+ captures = if is_drop
146
+ remove_from_captures(in_hand, *owned_captures)
147
+ else
148
+ owned_captures + [in_hand]
149
+ end
127
150
 
128
151
  north_turn? ? [captures, other_captures] : [other_captures, captures]
129
152
  end
@@ -133,10 +156,10 @@ class Qi
133
156
  # @param piece [Object] the piece to be removed
134
157
  # @param captures [Array<Object>] the array of captures
135
158
  # @return [Array<Object>] the modified array of captures
136
- # @raise [IndexError] if the piece is not found in the array
159
+ # @raise [Qi::Error::Drop] if the piece is not found in the array
137
160
  def remove_from_captures(piece, *captures)
138
161
  index = captures.rindex(piece)
139
- raise ::IndexError, "#{piece} not found!" if index.nil?
162
+ raise Error::Drop, "There are no #{piece} in hand." if index.nil?
140
163
 
141
164
  captures.delete_at(index)
142
165
  captures
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qi
3
3
  version: !ruby/object:Gem::Version
4
- version: 10.0.0.beta7
4
+ version: 10.0.0.beta8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyril Kato
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-02 00:00:00.000000000 Z
11
+ date: 2023-05-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: An abstraction that could help to update positions for games like Shogi.
14
14
  email: contact@cyril.email
@@ -19,6 +19,7 @@ files:
19
19
  - LICENSE.md
20
20
  - README.md
21
21
  - lib/qi.rb
22
+ - lib/qi/error/drop.rb
22
23
  homepage: https://github.com/sashite/qi.rb
23
24
  licenses:
24
25
  - MIT