qi 10.0.0.beta7 → 10.0.0.beta9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -5
- data/lib/qi/error/drop.rb +8 -0
- data/lib/qi.rb +47 -11
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 807fc7b14621e81663ea476f2d737352d455d4d2ab1d49efbe722c3ce571ed15
|
4
|
+
data.tar.gz: 9244deacf36f2717897a23b56de132af40ce308cd5977ee92899f57c49368f0e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 87cedbb94ddcecb3026647893aac63ce6559c92d37dc65121850efca0eb1ddd7a1616e441a0c40463a2bbe664de59d4498bbcfcfd3aa3caae6075a65851c57c5
|
7
|
+
data.tar.gz: 62a213dcaeb4d68cc6c4bc01b4d9b4a2df33b3d8bcca09c5e494e6a5b0eb349b236907437caa12c11777c6eaac559402e927f3db972f89acb4cd07f77408c4d9
|
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.
|
16
|
+
gem "qi", ">= 10.0.0.beta9"
|
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 # => "
|
49
|
-
qi0.inspect # => "<Qi
|
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 # => "
|
65
|
-
qi1.inspect # => "<Qi
|
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,
|
data/lib/qi.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "digest"
|
4
|
+
require_relative "qi/error/drop"
|
5
|
+
|
3
6
|
# A class that represents the state of a game.
|
4
7
|
class Qi
|
5
8
|
# @!attribute [r] north_captures
|
@@ -39,6 +42,37 @@ class Qi
|
|
39
42
|
self.class.new(!north_turn?, *modified_captures, modified_squares)
|
40
43
|
end
|
41
44
|
|
45
|
+
def eql?(other)
|
46
|
+
return false unless other.respond_to?(:serialize)
|
47
|
+
|
48
|
+
other.serialize == serialize
|
49
|
+
end
|
50
|
+
alias == eql?
|
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
|
+
|
68
|
+
def side_name
|
69
|
+
if north_turn?
|
70
|
+
"north"
|
71
|
+
else
|
72
|
+
"south"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
42
76
|
# Checks if it is the north turn or not.
|
43
77
|
#
|
44
78
|
# @return [Boolean] true if it is the north turn and false otherwise
|
@@ -85,6 +119,11 @@ class Qi
|
|
85
119
|
}
|
86
120
|
end
|
87
121
|
|
122
|
+
# Returns the hash-code for the position.
|
123
|
+
def hash
|
124
|
+
::Digest::SHA256.hexdigest(serialize)
|
125
|
+
end
|
126
|
+
|
88
127
|
# Returns a string representation of the Qi object's attributes.
|
89
128
|
#
|
90
129
|
# @return [String] a string containing three parts separated by "===":
|
@@ -92,7 +131,7 @@ class Qi
|
|
92
131
|
# - the captures, sorted and joined by ","
|
93
132
|
# - the squares, mapped to "coordinate:piece" pairs and joined by ","
|
94
133
|
def serialize
|
95
|
-
serialized_turn =
|
134
|
+
serialized_turn = "#{side_name}-turn"
|
96
135
|
serialized_captures = (north_captures + south_captures).sort.join(",")
|
97
136
|
serialized_squares = squares.keys.map { |i| "#{i}:#{squares.fetch(i)}" }.join(",")
|
98
137
|
|
@@ -116,14 +155,11 @@ class Qi
|
|
116
155
|
def update_captures(in_hand, is_drop:)
|
117
156
|
return [north_captures, south_captures] if in_hand.nil?
|
118
157
|
|
119
|
-
captures =
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
else
|
125
|
-
captures += [in_hand]
|
126
|
-
end
|
158
|
+
captures = if is_drop
|
159
|
+
remove_from_captures(in_hand, *owned_captures)
|
160
|
+
else
|
161
|
+
owned_captures + [in_hand]
|
162
|
+
end
|
127
163
|
|
128
164
|
north_turn? ? [captures, other_captures] : [other_captures, captures]
|
129
165
|
end
|
@@ -133,10 +169,10 @@ class Qi
|
|
133
169
|
# @param piece [Object] the piece to be removed
|
134
170
|
# @param captures [Array<Object>] the array of captures
|
135
171
|
# @return [Array<Object>] the modified array of captures
|
136
|
-
# @raise [
|
172
|
+
# @raise [Qi::Error::Drop] if the piece is not found in the array
|
137
173
|
def remove_from_captures(piece, *captures)
|
138
174
|
index = captures.rindex(piece)
|
139
|
-
raise ::
|
175
|
+
raise Error::Drop, "There are no #{piece} in hand." if index.nil?
|
140
176
|
|
141
177
|
captures.delete_at(index)
|
142
178
|
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.
|
4
|
+
version: 10.0.0.beta9
|
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-
|
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
|