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 +4 -4
- data/lib/board_game_grid/square.rb +26 -6
- data/lib/board_game_grid/square_set.rb +86 -7
- data/lib/board_game_grid/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd36b2b988acedde04d34d32c977268766edbe370565d7e4afc1af072e0bd49d
|
4
|
+
data.tar.gz: e47c5ab5280c737da98d3757ac35864aaab0cb10d435a25ec6e8ee53db11c63d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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.
|
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-
|
11
|
+
date: 2020-04-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|