chess_data 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,106 @@
1
+ module ChessData
2
+
3
+ # A PositionDefinition is a collection of criteria (matchers) which define
4
+ # a valid type of position.
5
+ #
6
+ # e.g. some kind of Rook endings.
7
+ #
8
+ # database.select do
9
+ # at_least 5, "P"
10
+ # exactly 1, "R"
11
+ # at_most 4, "p"
12
+ # exactly 1, "r"
13
+ # end
14
+ #
15
+ class PositionDefinition
16
+
17
+ # The constructor evaluates the provided _block_, to set up the user's
18
+ # board criteria. It then sets up some default criteria for the king's and
19
+ # pieces not covered by the block.
20
+ def initialize(&block)
21
+ @matchers = []
22
+ instance_eval(&block)
23
+
24
+ # fill in a default matcher for any not present
25
+ ["K", "k"].each do |piece|
26
+ unless @matchers.any? {|matcher| matcher.piece == piece}
27
+ exactly 1, piece
28
+ end
29
+ end
30
+ ["P", "p", "N", "n", "B", "b", "R", "r", "Q", "q"].each do |piece|
31
+ unless @matchers.any? {|matcher| matcher.piece == piece}
32
+ exactly 0, piece
33
+ end
34
+ end
35
+ end
36
+
37
+ # Checks that all the criteria are matched by the given board.
38
+ def check board
39
+ @matchers.all? do |matcher|
40
+ matcher.check board
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ # following methods used in block to constructor as DSL
47
+ # for a pattern definition
48
+
49
+ def exactly n, *pieces
50
+ pieces.each do |piece|
51
+ @matchers << ExactCount.new(n, piece)
52
+ end
53
+ end
54
+
55
+ def at_least n, *pieces
56
+ pieces.each do |piece|
57
+ @matchers << AtLeastCount.new(n, piece)
58
+ end
59
+ end
60
+
61
+ def at_most n, *pieces
62
+ pieces.each do |piece|
63
+ @matchers << AtMostCount.new(n, piece)
64
+ end
65
+ end
66
+ end
67
+
68
+ # parent class for the different types of count comparators
69
+ class Counter
70
+ attr_reader :piece
71
+
72
+ # Each counter has a target _piece_ and reference number _n_.
73
+ def initialize n, piece
74
+ @n = n
75
+ @piece = piece
76
+ end
77
+ end
78
+
79
+ # The ExactCount class checks that there are exactly _n_ of the
80
+ # named piece on the board.
81
+ class ExactCount < Counter
82
+ # Returns true if board contains _n_ of _piece_
83
+ def check board
84
+ board.count(@piece) == @n
85
+ end
86
+ end
87
+
88
+ # The AtLeastCount class checks that there are at least _n_ of the
89
+ # named piece on the board.
90
+ class AtLeastCount < Counter
91
+ # Returns true if board contains at least _n_ of _piece_
92
+ def check board
93
+ board.count(@piece) >= @n
94
+ end
95
+ end
96
+
97
+ # The AtMostCount class checks that there are at most _n_ of the
98
+ # named piece on the board.
99
+ class AtMostCount < Counter
100
+ # Returns true if board contains at most _n_ of _piece_
101
+ def check board
102
+ board.count(@piece) <= @n
103
+ end
104
+ end
105
+ end
106
+
data/lib/chess_data.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'word_wrap'
2
+
3
+ require 'chess_data/board.rb'
4
+ require 'chess_data/moves.rb'
5
+ require 'chess_data/game.rb'
6
+ require 'chess_data/database.rb'
7
+ require 'chess_data/position-definition.rb'
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chess_data
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.6
5
+ platform: ruby
6
+ authors:
7
+ - Peter Lane
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: word_wrap
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.0
33
+ description: "For searching/filtering datasets of chess games. This gem allows you
34
+ to read \ncollections of games from PGN files, select games which reach positions\nmatching
35
+ specific combinations of pieces, and save these games back in PGN\nformat.\n"
36
+ email: peterlane@gmx.com
37
+ executables: []
38
+ extensions: []
39
+ extra_rdoc_files:
40
+ - README.rdoc
41
+ - LICENSE.rdoc
42
+ files:
43
+ - LICENSE.rdoc
44
+ - README.rdoc
45
+ - lib/chess_data.rb
46
+ - lib/chess_data/board.rb
47
+ - lib/chess_data/database.rb
48
+ - lib/chess_data/game.rb
49
+ - lib/chess_data/moves.rb
50
+ - lib/chess_data/position-definition.rb
51
+ homepage:
52
+ licenses:
53
+ - MIT
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options:
57
+ - "-m"
58
+ - README.rdoc
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '2.5'
66
+ - - "<"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubygems_version: 3.4.5
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: For searching/filtering datasets of chess games based on combinations of
79
+ pieces.
80
+ test_files: []