Class: BoardGameGrid::SquareSet
- Inherits:
-
Object
- Object
- BoardGameGrid::SquareSet
- Extended by:
- Forwardable
- Defined in:
- lib/board_game_grid/square_set.rb
Overview
Square Set
A collection of Squares with useful filtering functions
Instance Attribute Summary collapse
-
#squares ⇒ Array<Square>
readonly
The squares in the set.
Instance Method Summary collapse
-
#&(other) ⇒ SquareSet
Find the intersection of Squares between sets.
-
#+(other) ⇒ SquareSet
Concat two SquareSets together.
-
#-(other) ⇒ SquareSet
Remove squares from one SquareSet from another.
-
#<<(square) ⇒ SquareSet
Push a Square onto a SquareSet.
-
#as_json ⇒ Hash
serializes the squares as a hash.
-
#at_range(origin, distance) ⇒ SquareSet
Find all squares at distance of square.
-
#between(origin, destination) ⇒ SquareSet
Returns squares between a and b.
-
#diagonal(origin) ⇒ SquareSet
Find all squares diagonal from square.
-
#files_away(origin, distance) ⇒ SquareSet
Find all squares a certain number of files away .
-
#find_by_id(id) ⇒ Square
Find the square with the matching unique identifier.
-
#find_by_x_and_y(x, y) ⇒ Square
Find the square with the matching x and y co-ordinates.
-
#in_range(origin, distance) ⇒ SquareSet
Find all squares within distance of square.
-
#initialize(squares: []) ⇒ SquareSet
constructor
New objects can be instantiated by passing in a hash with squares.
-
#not_orthogonal_or_diagonal(origin) ⇒ SquareSet
Find all squares not orthogonal or diagonal from square.
-
#orthogonal(origin) ⇒ SquareSet
Find all squares orthogonal from square.
-
#orthogonal_or_diagonal(origin) ⇒ SquareSet
Find all squares orthogonal or diagonal from square.
-
#ranks_away(origin, distance) ⇒ SquareSet
Find all squares a certain number of ranks away .
-
#select(&block) ⇒ SquareSet
Filter the squares with a block and behaves like Enumerable#select.
-
#unblocked(origin, square_set) ⇒ SquareSet
Returns destination from the origin that have a clear path.
-
#uniq ⇒ SquareSet
Get unique elements in the set.
-
#unoccupied ⇒ SquareSet
Find all squares without pieces on them.
-
#where(hash) ⇒ SquareSet
Filter the squares with a hash of attribute and matching values.
-
#|(other) ⇒ SquareSet
Find the union of Squares between sets.
Constructor Details
#initialize(squares: []) ⇒ SquareSet
New objects can be instantiated by passing in a hash with squares. They can be square objects or hashes.
Example:
# Instantiates a new Square Set
BoardGameGrid::SquareSet.new({
squares: [
{ x: 1, y: 0, piece: { player_number: 1, king: false }}
]
})
26 27 28 29 30 31 32 33 34 |
# File 'lib/board_game_grid/square_set.rb', line 26 def initialize(squares: []) @squares = if squares.all? { |element| element.instance_of?(Hash) } squares.map { |args| BoardGameGrid::Square.new(**args) } elsif squares.all? { |element| element.instance_of?(BoardGameGrid::Square) } squares else raise ArgumentError, "all squares must have the same class" end end |
Instance Attribute Details
#squares ⇒ Array<Square> (readonly)
Returns The squares in the set.
37 38 39 |
# File 'lib/board_game_grid/square_set.rb', line 37 def squares @squares end |
Instance Method Details
#&(other) ⇒ SquareSet
Find the intersection of Squares between sets
Example:
# Find the intersection of Squares
square_set & other
104 105 106 107 108 |
# File 'lib/board_game_grid/square_set.rb', line 104 def &(other) _squares = self.squares & other.squares self.class.new(squares: _squares) end |
#+(other) ⇒ SquareSet
Concat two SquareSets together
Example:
# Concat two SquareSets together
square_set + other
59 60 61 62 63 |
# File 'lib/board_game_grid/square_set.rb', line 59 def +(other) _squares = squares + other.squares self.class.new(squares: _squares) end |
#-(other) ⇒ SquareSet
Remove squares from one SquareSet from another
Example:
# Remove squares from one SquareSet
square_set - other
74 75 76 77 78 |
# File 'lib/board_game_grid/square_set.rb', line 74 def -(other) _squares = squares - other.squares self.class.new(squares: _squares) end |
#<<(square) ⇒ SquareSet
Push a Square onto a SquareSet
Example:
# Push a Square onto a SquareSet
square_set << square
89 90 91 92 93 |
# File 'lib/board_game_grid/square_set.rb', line 89 def <<(square) _squares = squares << square self.class.new(squares: _squares) end |
#as_json ⇒ Hash
serializes the squares as a hash
367 368 369 |
# File 'lib/board_game_grid/square_set.rb', line 367 def as_json squares.map(&:as_json) end |
#at_range(origin, distance) ⇒ SquareSet
Find all squares at distance of square
Example:
# Get all squares at 2 squares from square_a
square_set.at_range(square_a, 2)
219 220 221 |
# File 'lib/board_game_grid/square_set.rb', line 219 def at_range(origin, distance) select { |square| Vector.new(origin, square).magnitude == distance } end |
#between(origin, destination) ⇒ SquareSet
Returns squares between a and b. Only squares that are in the same diagonal will return squares.
Example:
# Get all squares between square_a and square_b
square_set.between(square_a, square_b)
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/board_game_grid/square_set.rb', line 342 def between(origin, destination) vector = Vector.new(origin, destination) if vector.diagonal? || vector.orthogonal? point_counter = origin.point direction = vector.direction _squares = [] while point_counter != destination.point point_counter = point_counter + direction square = find_by_x_and_y(point_counter.x, point_counter.y) if square && square.point != destination.point _squares.push(square) end end else _squares = [] end self.class.new(squares: _squares) end |
#diagonal(origin) ⇒ SquareSet
Find all squares diagonal from square
Example:
# Get all squares diagonal from square_a
square_set.diagonal(square_a)
278 279 280 |
# File 'lib/board_game_grid/square_set.rb', line 278 def diagonal(origin) select { |square| Vector.new(origin, square).diagonal? } end |
#files_away(origin, distance) ⇒ SquareSet
Find all squares a certain number of files away
Example:
# Get all squares at 2 ranks away from square_a
square_set.files_away(square_a, 2)
252 253 254 |
# File 'lib/board_game_grid/square_set.rb', line 252 def files_away(origin, distance) select { |square| Vector.new(origin, square).dx.abs == distance } end |
#find_by_id(id) ⇒ Square
Find the square with the matching unique identifier
Example:
# Find the square with id 4
square_set.find_by_id(4)
171 172 173 |
# File 'lib/board_game_grid/square_set.rb', line 171 def find_by_id(id) select { |square| square.id == id }.first end |
#find_by_x_and_y(x, y) ⇒ Square
Find the square with the matching x and y co-ordinates
Example:
# Find the square at 4,2
square_set.find_by_x_and_y(4, 2)
187 188 189 |
# File 'lib/board_game_grid/square_set.rb', line 187 def find_by_x_and_y(x, y) select { |square| square.x == x && square.y == y }.first end |
#in_range(origin, distance) ⇒ SquareSet
Find all squares within distance of square
Example:
# Get all squares within 2 squares of square_a
square_set.in_range(square_a, 2)
203 204 205 |
# File 'lib/board_game_grid/square_set.rb', line 203 def in_range(origin, distance) select { |square| Vector.new(origin, square).magnitude <= distance } end |
#not_orthogonal_or_diagonal(origin) ⇒ SquareSet
Find all squares not orthogonal or diagonal from square
Example:
# Get all squares not orthogonal or diagonal from square_a
square_set.not_orthogonal_or_diagonal(square_a)
304 305 306 |
# File 'lib/board_game_grid/square_set.rb', line 304 def not_orthogonal_or_diagonal(origin) select { |square| Vector.new(origin, square).not_orthogonal_or_diagonal? } end |
#orthogonal(origin) ⇒ SquareSet
Find all squares orthogonal from square
Example:
# Get all squares orthogonal from square_a
square_set.orthogonal(square_a)
265 266 267 |
# File 'lib/board_game_grid/square_set.rb', line 265 def orthogonal(origin) select { |square| Vector.new(origin, square).orthogonal? } end |
#orthogonal_or_diagonal(origin) ⇒ SquareSet
Find all squares orthogonal or diagonal from square
Example:
# Get all squares orthogonal or diagonal from square_a
square_set.orthogonal_or_diagonal(square_a)
291 292 293 |
# File 'lib/board_game_grid/square_set.rb', line 291 def orthogonal_or_diagonal(origin) select { |square| Vector.new(origin, square).orthogonal_or_diagonal? } end |
#ranks_away(origin, distance) ⇒ SquareSet
Find all squares a certain number of ranks away
Example:
# Get all squares at 2 ranks away from square_a
square_set.ranks_away(square_a, 2)
236 237 238 |
# File 'lib/board_game_grid/square_set.rb', line 236 def ranks_away(origin, distance) select { |square| Vector.new(origin, square).dy.abs == distance } end |
#select(&block) ⇒ SquareSet
Filter the squares with a block and behaves like Enumerable#select. It returns a SquareSet with the filtered squares.
129 130 131 132 133 |
# File 'lib/board_game_grid/square_set.rb', line 129 def select(&block) _squares = squares.select(&block) self.class.new(squares: _squares) end |
#unblocked(origin, square_set) ⇒ SquareSet
Returns destination from the origin that have a clear path
324 325 326 |
# File 'lib/board_game_grid/square_set.rb', line 324 def unblocked(origin, square_set) select { |destination| square_set.between(origin, destination).all?(&:unoccupied?) } end |
#uniq ⇒ SquareSet
Get unique elements in the set. Behaves like Enumerable#uniq. It returns a SquareSet with the unique squares.
139 140 141 142 143 |
# File 'lib/board_game_grid/square_set.rb', line 139 def uniq _squares = squares.uniq self.class.new(squares: _squares) end |
#unoccupied ⇒ SquareSet
Find all squares without pieces on them.
311 312 313 |
# File 'lib/board_game_grid/square_set.rb', line 311 def unoccupied select(&:unoccupied?) end |
#where(hash) ⇒ SquareSet
Filter the squares with a hash of attribute and matching values.
Example:
# Find all squares where piece is nil
square_set.where(piece: nil)
155 156 157 158 159 160 |
# File 'lib/board_game_grid/square_set.rb', line 155 def where(hash) res = hash.inject(squares) do |memo, (attribute, value)| memo.select { |square| square.attribute_match?(attribute, value) } end self.class.new(squares: res) end |
#|(other) ⇒ SquareSet
Find the union of Squares between sets
Example:
# Find the union of Squares
square_set | other
119 120 121 122 123 |
# File 'lib/board_game_grid/square_set.rb', line 119 def |(other) _squares = self.squares | other.squares self.class.new(squares: _squares) end |