qi 8.0.0 → 9.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/qi.rb +11 -31
- data/lib/qi/action.rb +61 -0
- data/lib/qi/move.rb +46 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 80f30262bee5c02c0df18d00b03848331e9bc673980e88c1c1949f0ac6915ff7
|
4
|
+
data.tar.gz: eca221290986aef674784c4c2f840f8485ea89d7deb5b19075df883e05cf574a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5945e941761301f153d6db897b710d9548e6c3a514f1edc61222773e9cbee15f4d28e5abf7d4b85f072b2fc3dc866d07489c6e8d42aabb3c7bc38be788b70092
|
7
|
+
data.tar.gz: fd96cf0829d263a9374e52992c61a2b97815f3c6431bee0373080dbdde85c1d0a62eb9e414e950d0bc9ee49ff5d44594f3286ae9996aec8f2a6725dcb0a9fbcb
|
data/README.md
CHANGED
data/lib/qi.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative File.join("qi", "move")
|
4
|
+
|
3
5
|
# The Qi abstraction.
|
4
6
|
#
|
5
|
-
# @example
|
7
|
+
# @example Apply a move to a classic Shogi problem
|
6
8
|
# Qi.call(
|
7
9
|
# [43, 13, "+B"],
|
8
|
-
#
|
9
|
-
#
|
10
|
+
# in_hand: %w[S r r b g g g g s n n n n p p p p p p p p p p p p p p p p p],
|
11
|
+
# square: {
|
10
12
|
# 3 => "s",
|
11
13
|
# 4 => "k",
|
12
14
|
# 5 => "s",
|
@@ -25,11 +27,11 @@ module Qi
|
|
25
27
|
# @see https://developer.sashite.com/specs/portable-chess-notation
|
26
28
|
# @see https://developer.sashite.com/specs/portable-move-notation
|
27
29
|
#
|
28
|
-
# @example
|
30
|
+
# @example Apply a move to a classic Shogi problem
|
29
31
|
# call(
|
30
32
|
# [43, 13, "+B"],
|
31
|
-
#
|
32
|
-
#
|
33
|
+
# in_hand: %w[S r r b g g g g s n n n n p p p p p p p p p p p p p p p p p],
|
34
|
+
# square: {
|
33
35
|
# 3 => "s",
|
34
36
|
# 4 => "k",
|
35
37
|
# 5 => "s",
|
@@ -39,30 +41,8 @@ module Qi
|
|
39
41
|
# )
|
40
42
|
# # => {:in_hand=>["S", "r", "r", "b", "g", "g", "g", "g", "s", "n", "n", "n", "n", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p"], :square=>{3=>"s", 4=>"k", 5=>"s", 22=>"+P", 13=>"+B"}}
|
41
43
|
#
|
42
|
-
# @return [Hash] The next position.
|
43
|
-
def self.call(move,
|
44
|
-
|
45
|
-
|
46
|
-
actions.each do |action|
|
47
|
-
src_square_id = action.fetch(0)
|
48
|
-
dst_square_id = action.fetch(1)
|
49
|
-
moved_piece_name = action.fetch(2)
|
50
|
-
captured_piece_name = action.fetch(3, nil)
|
51
|
-
|
52
|
-
if src_square_id.nil?
|
53
|
-
piece_in_hand_id = in_hand.index(moved_piece_name)
|
54
|
-
in_hand.delete_at(piece_in_hand_id) unless piece_in_hand_id.nil?
|
55
|
-
else
|
56
|
-
square.delete(src_square_id)
|
57
|
-
end
|
58
|
-
|
59
|
-
square[dst_square_id] = moved_piece_name
|
60
|
-
in_hand.push(captured_piece_name) unless captured_piece_name.nil?
|
61
|
-
end
|
62
|
-
|
63
|
-
{
|
64
|
-
in_hand: in_hand,
|
65
|
-
square: square
|
66
|
-
}
|
44
|
+
# @return [Hash] The piece set of the next position.
|
45
|
+
def self.call(move, in_hand:, square:)
|
46
|
+
Move.new(move).call(in_hand: in_hand, square: square)
|
67
47
|
end
|
68
48
|
end
|
data/lib/qi/action.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Qi
|
4
|
+
# The Action abstraction.
|
5
|
+
class Action
|
6
|
+
# Initialize an action instance.
|
7
|
+
#
|
8
|
+
# @param src_square_id [Integer, nil] The source square ID.
|
9
|
+
# @param dst_square_id [Integer] The target square ID.
|
10
|
+
# @param moved_piece_name [String] The moved piece name.
|
11
|
+
# @param captured_piece_name [String, nil] The captured piece name.
|
12
|
+
#
|
13
|
+
# @example Initialize a promoted bishop action from 43 to 13
|
14
|
+
# new(43, 13, "+B", nil)
|
15
|
+
#
|
16
|
+
# @see https://developer.sashite.com/specs/portable-action-notation
|
17
|
+
def initialize(src_square_id, dst_square_id, moved_piece_name, captured_piece_name = nil)
|
18
|
+
@src_square_id = src_square_id
|
19
|
+
@dst_square_id = dst_square_id
|
20
|
+
@moved_piece_name = moved_piece_name
|
21
|
+
@captured_piece_name = captured_piece_name
|
22
|
+
end
|
23
|
+
|
24
|
+
# Commit an action to the position.
|
25
|
+
#
|
26
|
+
# @param in_hand [Array] The list of pieces in hand.
|
27
|
+
# @param square [Hash] The index of each piece on the board.
|
28
|
+
#
|
29
|
+
# @example Commit a Shogi action to the piece set of a position
|
30
|
+
# call(
|
31
|
+
# 43, 13, "+B", nil,
|
32
|
+
# in_hand: %w[S r r b g g g g s n n n n p p p p p p p p p p p p p p p p p],
|
33
|
+
# square: {
|
34
|
+
# 3 => "s",
|
35
|
+
# 4 => "k",
|
36
|
+
# 5 => "s",
|
37
|
+
# 22 => "+P",
|
38
|
+
# 43 => "+B"
|
39
|
+
# }
|
40
|
+
# )
|
41
|
+
# # => {:in_hand=>["S", "r", "r", "b", "g", "g", "g", "g", "s", "n", "n", "n", "n", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p"], :square=>{3=>"s", 4=>"k", 5=>"s", 22=>"+P", 13=>"+B"}}
|
42
|
+
#
|
43
|
+
# @return [Hash] The next piece set.
|
44
|
+
def call(in_hand:, square:)
|
45
|
+
in_hand = in_hand.dup
|
46
|
+
square = square.dup
|
47
|
+
|
48
|
+
if @src_square_id.nil?
|
49
|
+
piece_in_hand_id = in_hand.index(@moved_piece_name)
|
50
|
+
in_hand.delete_at(piece_in_hand_id) unless piece_in_hand_id.nil?
|
51
|
+
else
|
52
|
+
square.delete(@src_square_id)
|
53
|
+
end
|
54
|
+
|
55
|
+
square[@dst_square_id] = @moved_piece_name
|
56
|
+
in_hand.push(@captured_piece_name) unless @captured_piece_name.nil?
|
57
|
+
|
58
|
+
{ in_hand: in_hand, square: square }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/qi/move.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "action"
|
4
|
+
|
5
|
+
module Qi
|
6
|
+
# The Move abstraction.
|
7
|
+
class Move
|
8
|
+
# Initialize a move instance.
|
9
|
+
#
|
10
|
+
# @param move [Array] The move to apply.
|
11
|
+
#
|
12
|
+
# @example Initialize a promoted bishop move from 43 to 13
|
13
|
+
# new([43, 13, "+B"])
|
14
|
+
#
|
15
|
+
# @see https://developer.sashite.com/specs/portable-move-notation
|
16
|
+
def initialize(move)
|
17
|
+
@actions = move.each_slice(4)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Apply a move to the piece set of a position.
|
21
|
+
#
|
22
|
+
# @param in_hand [Array] The list of pieces in hand.
|
23
|
+
# @param square [Hash] The index of each piece on the board.
|
24
|
+
#
|
25
|
+
# @example Apply a move to a classic Shogi problem
|
26
|
+
# call(
|
27
|
+
# [43, 13, "+B"],
|
28
|
+
# in_hand: %w[S r r b g g g g s n n n n p p p p p p p p p p p p p p p p p],
|
29
|
+
# square: {
|
30
|
+
# 3 => "s",
|
31
|
+
# 4 => "k",
|
32
|
+
# 5 => "s",
|
33
|
+
# 22 => "+P",
|
34
|
+
# 43 => "+B"
|
35
|
+
# }
|
36
|
+
# )
|
37
|
+
# # => {:in_hand=>["S", "r", "r", "b", "g", "g", "g", "g", "s", "n", "n", "n", "n", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p"], :square=>{3=>"s", 4=>"k", 5=>"s", 22=>"+P", 13=>"+B"}}
|
38
|
+
#
|
39
|
+
# @return [Hash] The piece set of the next position.
|
40
|
+
def call(in_hand:, square:)
|
41
|
+
@actions.inject(in_hand: in_hand, square: square) do |piece_set, attrs|
|
42
|
+
Action.new(*attrs).call(**piece_set)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
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:
|
4
|
+
version: 9.0.0.beta1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cyril Kato
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-10-
|
11
|
+
date: 2020-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: brutal
|
@@ -117,6 +117,8 @@ files:
|
|
117
117
|
- LICENSE.md
|
118
118
|
- README.md
|
119
119
|
- lib/qi.rb
|
120
|
+
- lib/qi/action.rb
|
121
|
+
- lib/qi/move.rb
|
120
122
|
homepage: https://developer.sashite.com/specs/
|
121
123
|
licenses:
|
122
124
|
- MIT
|
@@ -135,9 +137,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
135
137
|
version: 2.7.0
|
136
138
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
139
|
requirements:
|
138
|
-
- - "
|
140
|
+
- - ">"
|
139
141
|
- !ruby/object:Gem::Version
|
140
|
-
version:
|
142
|
+
version: 1.3.1
|
141
143
|
requirements: []
|
142
144
|
rubygems_version: 3.1.2
|
143
145
|
signing_key:
|