qi 10.0.0.beta6 → 10.0.0.beta8

Sign up to get free protection for your applications and to get access to all the features.
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 +50 -11
  5. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 638a4dafc9645e200d72d7799afb3f28cca3a81f7cd887e5f29c64974e5776e0
4
- data.tar.gz: 5e74a8a31f894187b404a3c41ed1a1434916880925934f2fe627e19472f99084
3
+ metadata.gz: 816f97762efb0e9f7a603b93b5703b4b68c6a4629f438f39ed49d09227b9650a
4
+ data.tar.gz: 1c90d60785447ff965ef92816ed58e7a0d315ffb9b104c185c76d4d1da461435
5
5
  SHA512:
6
- metadata.gz: 41a6e8c87db922ca87d992b62909c6cf5d87d8900d12c9bc0b43d94d1882040d168678ee9d3f6be85904b7f275a0eaa9c42e32364b0d7c5e4766ec84953fdb10
7
- data.tar.gz: 14323df90b7e942fbae1a1db4f3e23f071c4fdd62bd969797a39909c710e599534c5eaa2fd4b6a01ffbe91f48d25122a28c32b599fc5731f9b3c14b72c2f8a74
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.beta5"
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
@@ -69,6 +95,22 @@ class Qi
69
95
  ]
70
96
  end
71
97
 
98
+ # Returns a hash representation of the Qi object's attributes.
99
+ #
100
+ # @return [Hash{Symbol => Object}] a hash containing four key-value pairs:
101
+ # - is_north_turn: a boolean value indicating whose turn it is
102
+ # - north_captures: an array of pieces captured by the north player
103
+ # - south_captures: an array of pieces captured by the south player
104
+ # - squares: a hash of squares on the board
105
+ def to_h
106
+ {
107
+ is_north_turn: north_turn?,
108
+ north_captures:,
109
+ south_captures:,
110
+ squares:
111
+ }
112
+ end
113
+
72
114
  # Returns a string representation of the Qi object's attributes.
73
115
  #
74
116
  # @return [String] a string containing three parts separated by "===":
@@ -76,7 +118,7 @@ class Qi
76
118
  # - the captures, sorted and joined by ","
77
119
  # - the squares, mapped to "coordinate:piece" pairs and joined by ","
78
120
  def serialize
79
- serialized_turn = (north_turn? ? "NorthTurn" : "SouthTurn")
121
+ serialized_turn = "#{side_name}-turn"
80
122
  serialized_captures = (north_captures + south_captures).sort.join(",")
81
123
  serialized_squares = squares.keys.map { |i| "#{i}:#{squares.fetch(i)}" }.join(",")
82
124
 
@@ -100,14 +142,11 @@ class Qi
100
142
  def update_captures(in_hand, is_drop:)
101
143
  return [north_captures, south_captures] if in_hand.nil?
102
144
 
103
- captures = north_turn? ? north_captures : south_captures
104
- other_captures = north_turn? ? south_captures : north_captures
105
-
106
- if is_drop
107
- captures = remove_from_captures(in_hand, *captures)
108
- else
109
- captures += [in_hand]
110
- end
145
+ captures = if is_drop
146
+ remove_from_captures(in_hand, *owned_captures)
147
+ else
148
+ owned_captures + [in_hand]
149
+ end
111
150
 
112
151
  north_turn? ? [captures, other_captures] : [other_captures, captures]
113
152
  end
@@ -117,10 +156,10 @@ class Qi
117
156
  # @param piece [Object] the piece to be removed
118
157
  # @param captures [Array<Object>] the array of captures
119
158
  # @return [Array<Object>] the modified array of captures
120
- # @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
121
160
  def remove_from_captures(piece, *captures)
122
161
  index = captures.rindex(piece)
123
- raise ::IndexError, "#{piece} not found!" if index.nil?
162
+ raise Error::Drop, "There are no #{piece} in hand." if index.nil?
124
163
 
125
164
  captures.delete_at(index)
126
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.beta6
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-04-23 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