just_chess 0.1.0 → 1.0.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6272f4c1c746d2ef9c3dd9ca9569c4564f714a3b
4
- data.tar.gz: 96f5372a8b9220f720abac49e2a7d8c586a81556
3
+ metadata.gz: adb4a267ab8fd196ea121bcf3cf1031d13bce76c
4
+ data.tar.gz: 114b58a5d9d564fba83939d99bd0eaa5a5a23c78
5
5
  SHA512:
6
- metadata.gz: 71e99a15d466599fd0e286e9c51afe765c04b50191e5588c616b925e34520bd5e78fb230e2fdf9d09df9ff664644f196711b8326450c2aebd14a52f989d2cdec
7
- data.tar.gz: a0cbf2a6cd01e4540d31020570fa0f8ac306d911b97fe113e022184022eb0148cc04a432dee72cacf08e863ff81bb0742ce2b033fdacebeccecb01e44a8a7dd4
6
+ metadata.gz: aac77f24dfb1bc701c5e4c35aa1886a46e77f5c8c054c4dcd56ef152a618e87d70d92d47007a02347d56c29beb56587073058b2e1a5a685993dfc233cf2c5df5
7
+ data.tar.gz: 24fef72e911e7478b5817e3848114cc010c35b37cad00946b5e247779e92c41424fb6610942720829246faa37418b6bf769291aa10142500895972c1a6f0aef3
data/just_chess.gemspec CHANGED
@@ -26,4 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "bundler", "~> 1.15"
27
27
  spec.add_development_dependency "rake", "~> 10.0"
28
28
  spec.add_development_dependency "minitest", "~> 5.0"
29
+
30
+ spec.add_runtime_dependency "board_game_grid", "~> 0.1.0"
31
+
29
32
  end
@@ -4,6 +4,7 @@ require 'just_chess/errors/invalid_move_error'
4
4
  require 'just_chess/errors/invalid_promotion_error'
5
5
  require 'just_chess/errors/moved_into_check_error'
6
6
  require 'just_chess/square_set'
7
+ require 'board_game_grid'
7
8
 
8
9
  module JustChess
9
10
 
@@ -223,7 +224,7 @@ module JustChess
223
224
  to = squares.find_by_id(to_id)
224
225
 
225
226
  captured = captured_square(from, to)
226
- double_step_pawn = from.piece.is_a?(JustChess::Pawn) && Vector.new(from,to).magnitude == 2
227
+ double_step_pawn = from.piece.is_a?(JustChess::Pawn) && BoardGameGrid::Vector.new(from,to).magnitude == 2
227
228
  @last_double_step_pawn_id = double_step_pawn ? from.piece.id : nil
228
229
 
229
230
  @last_change = { type: 'move', data: {player_number: player_number, from: from_id, to: to_id} }
@@ -272,7 +273,7 @@ module JustChess
272
273
 
273
274
  def rook_castle_move(from, to)
274
275
  if from.occupied? && from.piece.is_a?(King) && from.piece.castle(from, self).include?(to)
275
- vector = Vector.new(from, to)
276
+ vector = BoardGameGrid::Vector.new(from, to)
276
277
 
277
278
  rook_from_x = vector.direction.x > 0 ? 7 : 0
278
279
  rook_from_y = from.y
@@ -47,7 +47,7 @@ module JustChess
47
47
 
48
48
  if has_not_moved? && rooks.any?
49
49
  _squares = rooks.map do |s|
50
- vector = Vector.new(square, s)
50
+ vector = BoardGameGrid::Vector.new(square, s)
51
51
  x = square.x + (2 * vector.direction.x)
52
52
  y = square.y
53
53
  game_state.squares.find_by_x_and_y(x, y)
@@ -1,5 +1,5 @@
1
1
  require 'just_chess/pieces/piece'
2
- require 'just_chess/vector.rb'
2
+ require 'board_game_grid'
3
3
 
4
4
  module JustChess
5
5
 
@@ -73,7 +73,7 @@ module JustChess
73
73
  def en_passant_square(square, game_state)
74
74
  if square.rank_number(player_number) == 5 && game_state.last_double_step_pawn_id
75
75
  double_step = game_state.squares.find_by_piece_id(game_state.last_double_step_pawn_id)
76
- vector = Vector.new(square, double_step)
76
+ vector = BoardGameGrid::Vector.new(square, double_step)
77
77
  if vector.magnitude.abs == 1
78
78
  x = double_step.x
79
79
  y = square.y + forwards_direction
@@ -1,5 +1,3 @@
1
- require 'just_chess/vector'
2
-
3
1
  module JustChess
4
2
 
5
3
  # = Piece
@@ -1,12 +1,12 @@
1
1
  require 'just_chess/piece_factory'
2
- require 'just_chess/point'
2
+ require 'board_game_grid'
3
3
 
4
4
  module JustChess
5
5
 
6
6
  # = Square
7
7
  #
8
8
  # A Square on a checker board
9
- class Square
9
+ class Square < BoardGameGrid::Square
10
10
 
11
11
  # New objects can be instantiated by passing in a hash with
12
12
  #
@@ -37,32 +37,6 @@ module JustChess
37
37
  @piece = PieceFactory.new(piece).build
38
38
  end
39
39
 
40
- # @return [String] the unique identifier of the square.
41
- attr_reader :id
42
-
43
- # @return [Fixnum] the x co-ordinate of the square.
44
- attr_reader :x
45
-
46
- # @return [Fixnum] the y co-ordinate of the square.
47
- attr_reader :y
48
-
49
- # @return [Piece,NilClass] The piece on the square if any.
50
- attr_accessor :piece
51
-
52
- # Is the square occupied by a piece?
53
- #
54
- # @return [Boolean]
55
- def occupied?
56
- !!piece
57
- end
58
-
59
- # Is the square unoccupied by a piece?
60
- #
61
- # @return [Boolean]
62
- def unoccupied?
63
- !piece
64
- end
65
-
66
40
  # Is the square occupied by the specified player?
67
41
  #
68
42
  # @return [Boolean]
@@ -95,20 +69,6 @@ module JustChess
95
69
  rank_number(player_number) == 8
96
70
  end
97
71
 
98
- # Is the square the same as another one?
99
- #
100
- # @return [Boolean]
101
- def ==(other)
102
- self.id == other.id
103
- end
104
-
105
- # A point object with the square's co-ordinates.
106
- #
107
- # @return [Point]
108
- def point
109
- Point.new(x, y)
110
- end
111
-
112
72
  # A serialized version of the square as a hash
113
73
  #
114
74
  # @return [Hash]
@@ -1,13 +1,12 @@
1
- require 'forwardable'
2
1
  require 'just_chess/square'
2
+ require 'board_game_grid'
3
3
 
4
4
  module JustChess
5
5
 
6
6
  # = Square Set
7
7
  #
8
8
  # A collection of Squares with useful filtering functions
9
- class SquareSet
10
- extend Forwardable
9
+ class SquareSet < BoardGameGrid::SquareSet
11
10
 
12
11
  # New objects can be instantiated by passing in a hash with squares.
13
12
  # They can be square objects or hashes.
@@ -32,113 +31,11 @@ module JustChess
32
31
  end
33
32
  end
34
33
 
35
- # @return [Array<Square>] The squares in the set.
36
- attr_reader :squares
37
-
38
- def_delegator :squares, :find
39
- def_delegator :squares, :size
40
- def_delegator :squares, :any?
41
- def_delegator :squares, :all?
42
- def_delegator :squares, :none?
43
- def_delegator :squares, :include?
44
- def_delegator :squares, :map
45
- def_delegator :squares, :empty?
46
-
47
- # Concat two SquareSets together
48
- #
49
- # @param [SquareSet] other
50
- # the second SquareSet
51
- #
52
- # @return [SquareSet]
53
- # ==== Example:
54
- # # Concat two SquareSets together
55
- # square_set + other
56
- def +(other)
57
- _squares = squares + other.squares
58
-
59
- self.class.new(squares: _squares)
60
- end
61
-
62
- # Remove squares from one SquareSet from another
63
- #
64
- # @param [SquareSet] other
65
- # the second SquareSet
66
- #
67
- # @return [SquareSet]
68
- # ==== Example:
69
- # # Remove squares from one SquareSet
70
- # square_set - other
71
- def -(other)
72
- _squares = squares - other.squares
73
-
74
- self.class.new(squares: _squares)
75
- end
76
-
77
- # Push a Square onto a SquareSet
78
- #
79
- # @param [Square] square
80
- # the square being pushed on
81
- #
82
- # @return [SquareSet]
83
- # ==== Example:
84
- # # Push a Square onto a SquareSet
85
- # square_set << square
86
- def <<(square)
87
- _squares = squares << square
88
-
89
- self.class.new(squares: _squares)
90
- end
91
-
92
- # Find the intersection of Squares between sets
93
- #
94
- # @param [SquareSet] other
95
- # the second SquareSet
96
- #
97
- # @return [SquareSet]
98
- # ==== Example:
99
- # # Find the intersection of Squares
100
- # square_set & other
101
- def &(other)
102
- select { |square| other.include?(square) }
103
- end
104
-
105
- # Filter the squares with a block and behaves like Enumerable#select.
106
- # It returns a SquareSet with the filtered squares.
107
- #
108
- # @return [SquareSet]
109
- def select(&block)
110
- _squares = squares.select(&block)
111
-
112
- self.class.new(squares: _squares)
113
- end
114
-
115
- # Find the square with the matching unique identifier
116
- #
117
- # @param [Fixnum] id
118
- # the unique identifier.
119
- #
120
- # @return [Square]
121
- # ==== Example:
122
- # # Find the square with id 4
123
- # square_set.find_by_id(4)
124
- def find_by_id(id)
125
- find { |s| s.id == id }
126
- end
127
-
128
- # Find the square with the matching x and y co-ordinates
129
- #
130
- # @param [Fixnum] x
131
- # the x co-ordinate.
132
- #
133
- # @param [Fixnum] y
134
- # the y co-ordinate.
34
+ # serializes the squares as a hash
135
35
  #
136
- # @return [Square]
137
- # ==== Example:
138
- # # Find the square at 4,2
139
- # square_set.find_by_x_and_y(4, 2)
140
- def find_by_x_and_y(x, y)
141
- find { |s| s.x == x && s.y == y }
36
+ # @return [Hash]
37
+ def as_json
38
+ squares.map(&:as_json)
142
39
  end
143
40
 
144
41
  # Find the square with the matching piece identifier
@@ -167,74 +64,6 @@ module JustChess
167
64
  find { |s| s.piece && s.piece.is_a?(JustChess::King) && s.occupied_by_player?(player_number) }
168
65
  end
169
66
 
170
- # Returns squares between a and b.
171
- # Only squares that are in the same diagonal will return squares.
172
- #
173
- # @param [Square] a
174
- # a square.
175
- #
176
- # @param [Square] b
177
- # another square.
178
- #
179
- # @return [SquareSet]
180
- #
181
- # ==== Example:
182
- # # Get all squares between square_a and square_b
183
- # square_set.between(square_a, square_b)
184
- def between(a, b)
185
- vector = Vector.new(a, b)
186
-
187
- if vector.diagonal? || vector.orthogonal?
188
- point_counter = a.point
189
- direction = vector.direction
190
- _squares = []
191
-
192
- while point_counter != b.point
193
- point_counter = point_counter + direction
194
- square = find_by_x_and_y(point_counter.x, point_counter.y)
195
- if square && square.point != b.point
196
- _squares.push(square)
197
- end
198
- end
199
- else
200
- _squares = []
201
- end
202
-
203
- self.class.new(squares: _squares)
204
- end
205
-
206
- # Find all squares within distance of square
207
- #
208
- # @param [Square] square
209
- # the originating square
210
- #
211
- # @param [Fixnum] distance
212
- # the specified distance from the square
213
- #
214
- # @return [SquareSet]
215
- # ==== Example:
216
- # # Get all squares within 2 squares of square_a
217
- # square_set.in_range(square_a, 2)
218
- def in_range(square, distance)
219
- select { |s| Vector.new(square, s).magnitude.abs <= distance }
220
- end
221
-
222
- # Find all squares at distance of square
223
- #
224
- # @param [Square] square
225
- # the originating square
226
- #
227
- # @param [Fixnum] distance
228
- # the specified distance from the square
229
- #
230
- # @return [SquareSet]
231
- # ==== Example:
232
- # # Get all squares at 2 squares from square_a
233
- # square_set.at_range(square_a, 2)
234
- def at_range(square, distance)
235
- select { |s| Vector.new(square, s).magnitude.abs == distance }
236
- end
237
-
238
67
  # Find all squares in the y direction of square
239
68
  #
240
69
  # @param [Square] square
@@ -248,66 +77,7 @@ module JustChess
248
77
  # # Get all squares up from square_a
249
78
  # square_set.in_direction(square_a, -1)
250
79
  def in_direction(square, direction_y)
251
- select { |s| Vector.new(square, s).direction.y == direction_y }
252
- end
253
-
254
- # Find all squares orthogonal from square
255
- #
256
- # @param [Square] square
257
- # the originating square
258
- #
259
- # @return [SquareSet]
260
- # ==== Example:
261
- # # Get all squares orthogonal from square_a
262
- # square_set.orthogonal(square_a)
263
- def orthogonal(square)
264
- select { |s| Vector.new(square, s).orthogonal? }
265
- end
266
-
267
- # Find all squares diagonal from square
268
- #
269
- # @param [Square] square
270
- # the originating square
271
- #
272
- # @return [SquareSet]
273
- # ==== Example:
274
- # # Get all squares diagonal from square_a
275
- # square_set.diagonal(square_a)
276
- def diagonal(square)
277
- select { |s| Vector.new(square, s).diagonal? }
278
- end
279
-
280
- # Find all squares orthogonal or diagonal from square
281
- #
282
- # @param [Square] square
283
- # the originating square
284
- #
285
- # @return [SquareSet]
286
- # ==== Example:
287
- # # Get all squares orthogonal or diagonal from square_a
288
- # square_set.orthogonal_or_diagonal(square_a)
289
- def orthogonal_or_diagonal(square)
290
- select { |s| Vector.new(square, s).orthogonal_or_diagonal? }
291
- end
292
-
293
- # Find all squares not orthogonal or diagonal from square
294
- #
295
- # @param [Square] square
296
- # the originating square
297
- #
298
- # @return [SquareSet]
299
- # ==== Example:
300
- # # Get all squares not orthogonal or diagonal from square_a
301
- # square_set.not_orthogonal_or_diagonal(square_a)
302
- def not_orthogonal_or_diagonal(square)
303
- select { |s| Vector.new(square, s).not_orthogonal_or_diagonal? }
304
- end
305
-
306
- # Find all squares without pieces on them.
307
- #
308
- # @return [SquareSet]
309
- def unoccupied
310
- select { |s| s.unoccupied? }
80
+ select { |s| BoardGameGrid::Vector.new(square, s).direction.y == direction_y }
311
81
  end
312
82
 
313
83
  # Takes a player number and returns all squares occupied by the player
@@ -360,19 +130,6 @@ module JustChess
360
130
  select { |s| s.piece && !s.piece.is_a?(piece_type) }
361
131
  end
362
132
 
363
- # Returns destination from the origin that have a clear path
364
- #
365
- # @param [Square] origin
366
- # the originating square.
367
- #
368
- # @param [SquareSet] square_set
369
- # the board position.
370
- #
371
- # @return [SquareSet]
372
- def unblocked(origin, square_set)
373
- select { |destination| square_set.between(origin, destination).all?(&:unoccupied?) }
374
- end
375
-
376
133
  # Returns all squares with pieces that haven't moved
377
134
  #
378
135
  # @return [SquareSet]
@@ -396,12 +153,5 @@ module JustChess
396
153
 
397
154
  self.class.new(squares: _squares)
398
155
  end
399
-
400
- # serializes the squares as a hash
401
- #
402
- # @return [Hash]
403
- def as_json
404
- squares.map(&:as_json)
405
- end
406
156
  end
407
157
  end
@@ -1,4 +1,4 @@
1
1
  module JustChess
2
2
  # :nodoc:
3
- VERSION = "0.1.0"
3
+ VERSION = "1.0.0"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: just_chess
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Humphreys
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-07-12 00:00:00.000000000 Z
11
+ date: 2018-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: board_game_grid
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.1.0
55
69
  description: Provides a representation of a chess game complete with rules enforcement
56
70
  and serialisation.
57
71
  email:
@@ -70,7 +84,6 @@ files:
70
84
  - bin/setup
71
85
  - just_chess.gemspec
72
86
  - lib/just_chess.rb
73
- - lib/just_chess/direction.rb
74
87
  - lib/just_chess/errors/causes_check_error.rb
75
88
  - lib/just_chess/errors/error.rb
76
89
  - lib/just_chess/errors/invalid_move_error.rb
@@ -88,10 +101,8 @@ files:
88
101
  - lib/just_chess/pieces/piece.rb
89
102
  - lib/just_chess/pieces/queen.rb
90
103
  - lib/just_chess/pieces/rook.rb
91
- - lib/just_chess/point.rb
92
104
  - lib/just_chess/square.rb
93
105
  - lib/just_chess/square_set.rb
94
- - lib/just_chess/vector.rb
95
106
  - lib/just_chess/version.rb
96
107
  homepage: https://github.com/mrlhumphreys/just_chess
97
108
  licenses:
@@ -1,35 +0,0 @@
1
- module JustChess
2
-
3
- # = Direction
4
- #
5
- # The Direction that something is moving on a 2d plane
6
- class Direction
7
-
8
- # New objects can be instantiated with
9
- #
10
- # @param [Fixnum] dx
11
- # the dx magnitude.
12
- #
13
- # @param [Fixnum] dy
14
- # the dy magnitude.
15
- #
16
- # ==== Example:
17
- # # Instantiates a new Direction
18
- # JustChess::Direction.new({
19
- # dx: 1,
20
- # dy: 1
21
- # })
22
- def initialize(dx, dy)
23
- x = dx == 0 ? dx : dx/dx.abs
24
- y = dy == 0 ? dy : dy/dy.abs
25
-
26
- @x, @y = x, y
27
- end
28
-
29
- # @return [Fixnum] the x magnitude.
30
- attr_reader :x
31
-
32
- # @return [Fixnum] the y magnitude.
33
- attr_reader :y
34
- end
35
- end
@@ -1,52 +0,0 @@
1
- module JustChess
2
-
3
- # = Point
4
- #
5
- # A point with an x and y co-ordinates
6
- class Point
7
-
8
- # New objects can be instantiated with
9
- #
10
- # @param [Fixnum] x
11
- # the x co-ordinate.
12
- #
13
- # @param [Fixnum] y
14
- # the y co-ordinate.
15
- #
16
- # ==== Example:
17
- # # Instantiates a new Point
18
- # JustChess::Point.new({
19
- # x: 1,
20
- # y: 1
21
- # })
22
- def initialize(x, y)
23
- @x, @y = x, y
24
- end
25
-
26
- # @return [Fixnum] the x co-ordinate.
27
- attr_reader :x
28
-
29
- # @return [Fixnum] the y co-ordinate.
30
- attr_reader :y
31
-
32
- # Add a point to another point by adding their co-ordinates and returning a new point.
33
- #
34
- # @param [Point] other
35
- # the other point to add.
36
- #
37
- # @return [Point]
38
- def +(other)
39
- self.class.new(self.x + other.x, self.y + other.y)
40
- end
41
-
42
- # Check if popints are equal by seeing if their co-ordinates are equal.
43
- #
44
- # @param [Point] other
45
- # the other point to compare to.
46
- #
47
- # @return [TrueClass, FalseClass]
48
- def ==(other)
49
- self.x == other.x && self.y == other.y
50
- end
51
- end
52
- end
@@ -1,86 +0,0 @@
1
- require 'just_chess/direction'
2
-
3
- module JustChess
4
-
5
- # = Vector
6
- #
7
- # An element of Vector space
8
- class Vector
9
-
10
- # New objects can be instantiated by passing in a two points with x and y co-ordinates
11
- #
12
- # @param [Point] origin
13
- # the start point.
14
- #
15
- # @param [Point] destination
16
- # the end point.
17
- #
18
- # ==== Example:
19
- # # Instantiates a new Vector
20
- # JustChess::Vector.new(
21
- # Struct.new(:x, :y).new(1, 1),
22
- # Struct.new(:x, :y).new(3, 3)
23
- # })
24
- def initialize(origin, destination)
25
- @origin, @destination = origin, destination
26
- end
27
-
28
- # @return [Object] the origin.
29
- attr_reader :origin
30
-
31
- # @return [Object] the destination.
32
- attr_reader :destination
33
-
34
- # The direction of the vector as a object
35
- #
36
- # @return [Direction]
37
- def direction
38
- Direction.new(dx, dy)
39
- end
40
-
41
- # The biggest difference between co-ordinates
42
- #
43
- # @return [Fixnum]
44
- def magnitude
45
- [dx.abs, dy.abs].max
46
- end
47
-
48
- # Is the vector orthogonal?
49
- #
50
- # @return [Boolean]
51
- def orthogonal?
52
- dx == 0 || dy == 0
53
- end
54
-
55
- # Is the vector diagonal?
56
- #
57
- # @return [Boolean]
58
- def diagonal?
59
- dx.abs == dy.abs
60
- end
61
-
62
- # Is the vector not orthogonal or diagonal?
63
- #
64
- # @return [Boolean]
65
- def not_orthogonal_or_diagonal?
66
- !(orthogonal? || diagonal?)
67
- end
68
-
69
- # Is the vector orthogonal or diagonal?
70
- #
71
- # @return [Boolean]
72
- def orthogonal_or_diagonal?
73
- orthogonal? || diagonal?
74
- end
75
-
76
- private
77
-
78
- def dx
79
- destination.x - origin.x
80
- end
81
-
82
- def dy
83
- destination.y - origin.y
84
- end
85
- end
86
- end