board_game_grid 0.1.3 → 0.1.4

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
  SHA256:
3
- metadata.gz: 30b06334909c284fa4df43311e971bad13b2d4b03ad0c95e09e5f05ec2240d7b
4
- data.tar.gz: f9821c6b664ab4eb5cd0c766e2e4b556eab40067517c9fea3abc38bf53d36205
3
+ metadata.gz: fd36b2b988acedde04d34d32c977268766edbe370565d7e4afc1af072e0bd49d
4
+ data.tar.gz: e47c5ab5280c737da98d3757ac35864aaab0cb10d435a25ec6e8ee53db11c63d
5
5
  SHA512:
6
- metadata.gz: c290da6cb71e2ef9b9eb2c841f2e86a6f165ebcf1d9fbc433f40d98f9e2117a4b3f5b1bd4bf47662489fc883a0841b67386d6b85d9eaec9d687b99c920b29cd9
7
- data.tar.gz: 7be305c897cfb2290e574a6d403f71c60499f7a6315c015e4a3c1822a1f512ee19443424dde9f3e9569917885434923fc78343ae5abea11300c203b7e8305a01
6
+ metadata.gz: 7b9d4d05e209bad2899a5b721c457272d23e6a1f6242fa8015c3f7aac4f86bb490de63d195012f42df157d6e35bcf91ac3f8f601006511739c521f34c2e0d1e3
7
+ data.tar.gz: ecb84eb4ed4e7f076a37aed8203409ee8e5275560d6b5368188ee865bcaa5911c090ae7e6034b54dd44d9ec00e6e595df00597d2471ea547ec959dbdbdbd1a1e
@@ -48,6 +48,18 @@ module BoardGameGrid
48
48
  # @return [Hash,NilClass] The piece on the square if any.
49
49
  attr_accessor :piece
50
50
 
51
+ # A serialized version of the square as a hash
52
+ #
53
+ # @return [Hash]
54
+ def as_json
55
+ {
56
+ id: id,
57
+ x: x,
58
+ y: y,
59
+ piece: piece && piece.as_json
60
+ }
61
+ end
62
+
51
63
  # checks if the square matches the attributes passed.
52
64
  #
53
65
  # @param [Symbol] attribute
@@ -86,6 +98,20 @@ module BoardGameGrid
86
98
  !piece
87
99
  end
88
100
 
101
+ # Is the square occupied by the specified player?
102
+ #
103
+ # @return [Boolean]
104
+ def occupied_by_player?(player_number)
105
+ piece && piece.player_number == player_number
106
+ end
107
+
108
+ # Is the square occupied by the specified player?
109
+ #
110
+ # @return [Boolean]
111
+ def occupied_by_opponent?(player_number)
112
+ piece && piece.player_number != player_number
113
+ end
114
+
89
115
  # Is the square the same as another one?
90
116
  #
91
117
  # @return [Boolean]
@@ -100,11 +126,5 @@ module BoardGameGrid
100
126
  Point.new(x, y)
101
127
  end
102
128
 
103
- # A serialized version of the square as a hash
104
- #
105
- # @return [Hash]
106
- def as_json
107
- { id: id, x: x, y: y, piece: piece }
108
- end
109
129
  end
110
130
  end
@@ -47,6 +47,13 @@ module BoardGameGrid
47
47
  def_delegator :squares, :each
48
48
  def_delegator :squares, :empty?
49
49
 
50
+ # serializes the squares as a hash
51
+ #
52
+ # @return [Hash]
53
+ def as_json
54
+ squares.map(&:as_json)
55
+ end
56
+
50
57
  # Concat two SquareSets together
51
58
  #
52
59
  # @param [SquareSet] other
@@ -188,6 +195,35 @@ module BoardGameGrid
188
195
  select { |square| square.x == x && square.y == y }.first
189
196
  end
190
197
 
198
+ # Find the square with the matching piece identifier
199
+ #
200
+ # @param [Fixnum] piece_id
201
+ # the unique identifier of the piece.
202
+ #
203
+ # @return [Square]
204
+ # ==== example:
205
+ # # Find the square with a piece with id 4
206
+ # square_set.find_by_piece_id(4)
207
+ def find_by_piece_id(piece_id)
208
+ find { |s| s.piece && s.piece.id == piece_id }
209
+ end
210
+
211
+ # Find all squares in the y direction of square
212
+ #
213
+ # @param [Square] square
214
+ # the originating square
215
+ #
216
+ # @param [Fixnum] direction_y
217
+ # the direction, either up (-1) or down (1)
218
+ #
219
+ # @return [SquareSet]
220
+ # ==== Example:
221
+ # # Get all squares up from square_a
222
+ # square_set.in_direction(square_a, -1)
223
+ def in_direction(square, direction_y)
224
+ select { |s| BoardGameGrid::Vector.new(square, s).direction.y == direction_y }
225
+ end
226
+
191
227
  # Find all squares within distance of square
192
228
  #
193
229
  # @param [Square] square
@@ -305,6 +341,56 @@ module BoardGameGrid
305
341
  select { |square| Vector.new(origin, square).not_orthogonal_or_diagonal? }
306
342
  end
307
343
 
344
+ # Takes a player number and returns all squares occupied by the player
345
+ #
346
+ # @param [Fixnum] player_number
347
+ # the player's number.
348
+ #
349
+ # @return [SquareSet]
350
+ def occupied_by_player(player_number)
351
+ select { |s| s.occupied_by_player?(player_number) }
352
+ end
353
+
354
+ # Takes a player number and returns all squares occupied by the opponent of the player
355
+ #
356
+ # @param [Fixnum] player_number
357
+ # the player's number.
358
+ #
359
+ # @return [SquareSet]
360
+ def occupied_by_opponent(player_number)
361
+ select { |s| s.occupied_by_opponent?(player_number) }
362
+ end
363
+
364
+ # Takes a player number and returns all squares unoccupied or occupied by the opponent of the player
365
+ #
366
+ # @param [Fixnum] player_number
367
+ # the player's number.
368
+ #
369
+ # @return [SquareSet]
370
+ def unoccupied_or_occupied_by_opponent(player_number)
371
+ select { |s| s.unoccupied? || s.occupied_by_opponent?(player_number) }
372
+ end
373
+
374
+ # Find all squares occupied by a piece of a particular type
375
+ #
376
+ # @param [Class] piece_type
377
+ # the class of the piece.
378
+ #
379
+ # @return [SquareSet]
380
+ def occupied_by_piece(piece_type)
381
+ select { |s| s.piece && s.piece.is_a?(piece_type) }
382
+ end
383
+
384
+ # Find all squares occupied by a piece not of a particular type
385
+ #
386
+ # @param [Class] piece_type
387
+ # the class of the piece.
388
+ #
389
+ # @return [SquareSet]
390
+ def excluding_piece(piece_type)
391
+ select { |s| s.piece && !s.piece.is_a?(piece_type) }
392
+ end
393
+
308
394
  # Find all squares without pieces on them.
309
395
  #
310
396
  # @return [SquareSet]
@@ -360,12 +446,5 @@ module BoardGameGrid
360
446
 
361
447
  self.class.new(squares: _squares)
362
448
  end
363
-
364
- # serializes the squares as a hash
365
- #
366
- # @return [Hash]
367
- def as_json
368
- squares.map(&:as_json)
369
- end
370
449
  end
371
450
  end
@@ -1,3 +1,3 @@
1
1
  module BoardGameGrid
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: board_game_grid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Humphreys
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-26 00:00:00.000000000 Z
11
+ date: 2020-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler