board_game_grid 0.1.3 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 30b06334909c284fa4df43311e971bad13b2d4b03ad0c95e09e5f05ec2240d7b
4
- data.tar.gz: f9821c6b664ab4eb5cd0c766e2e4b556eab40067517c9fea3abc38bf53d36205
3
+ metadata.gz: 65c16ddf332a1bc7229a8b038a6d1716cec9d43bdc875c4e0b139eeb5d987534
4
+ data.tar.gz: 1ddd8f91ccf45434eb141f1b26bc6008659555164e7f445e9947986a1bb02d58
5
5
  SHA512:
6
- metadata.gz: c290da6cb71e2ef9b9eb2c841f2e86a6f165ebcf1d9fbc433f40d98f9e2117a4b3f5b1bd4bf47662489fc883a0841b67386d6b85d9eaec9d687b99c920b29cd9
7
- data.tar.gz: 7be305c897cfb2290e574a6d403f71c60499f7a6315c015e4a3c1822a1f512ee19443424dde9f3e9569917885434923fc78343ae5abea11300c203b7e8305a01
6
+ metadata.gz: 3af1ea117ae58ad996b9f97b7deca7840ae963eeff28418cccd52b3842db987efe9a1994a0c717cf69096b82bffd179b73a85a5b3f31c3b5a1a1f198b11625cc
7
+ data.tar.gz: dd44cb8eef8eedfdff3330035704ed110b713abc7389497f13bda8eba462f829c9196baac108ba1a36707704451cf4f76df084e5813595159fec4582b132e788
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ *.swp
@@ -5,4 +5,6 @@ module BoardGameGrid
5
5
 
6
6
  end
7
7
 
8
- require 'board_game_grid/square_set'
8
+ require 'board_game_grid/square_set'
9
+ require 'board_game_grid/square'
10
+ require 'board_game_grid/piece'
@@ -0,0 +1,124 @@
1
+ module BoardGameGrid
2
+
3
+ # = Piece
4
+ #
5
+ # A piece that can move on a board
6
+ class Piece
7
+ # The forwards direciton of the piece for each player
8
+ FORWARDS_DIRECTION = { 1 => -1, 2 => 1 }
9
+
10
+ def initialize(id: , player_number: , type: nil)
11
+ @id = id
12
+ @player_number = player_number
13
+ end
14
+
15
+ # @return [Fixnum] the identifier of the piece.
16
+ attr_reader :id
17
+
18
+ # @return [Fixnum] the owner of the piece.
19
+ attr_reader :player_number
20
+
21
+ # The opposing player number
22
+ #
23
+ # @return [Fixnum]
24
+ def opponent
25
+ player_number == 1 ? 2 : 1
26
+ end
27
+
28
+ # The stringified identifier of the piece type.
29
+ #
30
+ # @return [Fixnum]
31
+ def type
32
+ self.class.to_s.split('::').last.downcase
33
+ end
34
+
35
+ # Can the piece move from a square, to a square, given the game state?
36
+ #
37
+ # @param [Square] from
38
+ # the origin square.
39
+ #
40
+ # @param [Square] to
41
+ # the destination square.
42
+ #
43
+ # @param [GameState] game_state
44
+ # the current game state.
45
+ #
46
+ # @return [Boolean]
47
+ def can_move?(from, to, game_state)
48
+ destinations(from, game_state).include?(to)
49
+ end
50
+
51
+ # All the squares that the piece can move to and/or capture.
52
+ #
53
+ # @param [Square] square
54
+ # the origin square.
55
+ #
56
+ # @param [GameState] game_state
57
+ # the current game state.
58
+ #
59
+ # @return [SquareSet]
60
+ def destinations(square, game_state)
61
+ []
62
+ end
63
+
64
+ # All the squares that the piece can move to.
65
+ #
66
+ # @param [Square] square
67
+ # the origin square.
68
+ #
69
+ # @param [GameState] game_state
70
+ # the current game state.
71
+ #
72
+ # @return [SquareSet]
73
+ def move_squares(square, game_state)
74
+ destinations(square, game_state)
75
+ end
76
+
77
+ # All the squares that the piece can capture.
78
+ #
79
+ # @param [Square] square
80
+ # the origin square.
81
+ #
82
+ # @param [GameState] game_state
83
+ # the current game state.
84
+ #
85
+ # @return [SquareSet]
86
+ def capture_squares(squares, game_state)
87
+ destinations(squares, game_state)
88
+ end
89
+
90
+ # All the squares that the piece could potentially capture. (i.e. if a piece was there)
91
+ #
92
+ # @param [Square] square
93
+ # the origin square.
94
+ #
95
+ # @param [GameState] game_state
96
+ # the current game state.
97
+ #
98
+ # @return [SquareSet]
99
+ def potential_capture_squares(square, game_state)
100
+ capture_squares(square, game_state)
101
+ end
102
+
103
+ # returns a serialized piece as a hash
104
+ #
105
+ # @return [Hash]
106
+ def as_json
107
+ {
108
+ id: id,
109
+ player_number: player_number,
110
+ type: type
111
+ }
112
+ end
113
+
114
+ # returns the direction the piece moves
115
+ #
116
+ # player 1 moves up (-1)
117
+ # player 2 moves down (1)
118
+ #
119
+ # @return [Fixnum]
120
+ def forwards_direction
121
+ FORWARDS_DIRECTION[player_number]
122
+ end
123
+ end
124
+ end
@@ -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
@@ -62,8 +74,12 @@ module BoardGameGrid
62
74
  def attribute_match?(attribute, value)
63
75
  hash_obj_matcher = lambda do |obj, k, v|
64
76
  value = obj.send(k)
65
- if !value.nil? && v.is_a?(Hash)
77
+ if v.is_a?(Hash) && !value.nil?
66
78
  v.all? { |k2,v2| hash_obj_matcher.call(value, k2, v2) }
79
+ elsif v.is_a?(Array) && !value.is_a?(Array)
80
+ v.include?(value)
81
+ elsif v.is_a?(Proc)
82
+ v.call(value)
67
83
  else
68
84
  value == v
69
85
  end
@@ -86,6 +102,20 @@ module BoardGameGrid
86
102
  !piece
87
103
  end
88
104
 
105
+ # Is the square occupied by the specified player?
106
+ #
107
+ # @return [Boolean]
108
+ def occupied_by_player?(player_number)
109
+ piece && piece.player_number == player_number
110
+ end
111
+
112
+ # Is the square occupied by the specified player?
113
+ #
114
+ # @return [Boolean]
115
+ def occupied_by_opponent?(player_number)
116
+ piece && piece.player_number != player_number
117
+ end
118
+
89
119
  # Is the square the same as another one?
90
120
  #
91
121
  # @return [Boolean]
@@ -100,11 +130,5 @@ module BoardGameGrid
100
130
  Point.new(x, y)
101
131
  end
102
132
 
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
133
  end
110
134
  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.8"
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.8
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: 2021-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,6 +71,7 @@ files:
71
71
  - board_game_grid.gemspec
72
72
  - lib/board_game_grid.rb
73
73
  - lib/board_game_grid/direction.rb
74
+ - lib/board_game_grid/piece.rb
74
75
  - lib/board_game_grid/point.rb
75
76
  - lib/board_game_grid/square.rb
76
77
  - lib/board_game_grid/square_set.rb