Class: JustChess::SquareSet
- Inherits:
-
BoardGameGrid::SquareSet
- Object
- BoardGameGrid::SquareSet
- JustChess::SquareSet
- Defined in:
- lib/just_chess/square_set.rb
Overview
Square Set
A collection of Squares with useful filtering functions
Instance Method Summary collapse
-
#as_json ⇒ Hash
serializes the squares as a hash.
-
#excluding_piece(piece_type) ⇒ SquareSet
Find all squares occupied by a piece not of a particular type.
-
#find_by_piece_id(piece_id) ⇒ Square
Find the square with the matching piece identifier.
-
#find_king_for_player(player_number) ⇒ Square
Find the square occupied by the player’s king.
-
#in_direction(square, direction_y) ⇒ SquareSet
Find all squares in the y direction of square.
-
#initialize(squares:) ⇒ SquareSet
constructor
New objects can be instantiated by passing in a hash with squares.
-
#occupied_by_opponent(player_number) ⇒ SquareSet
Takes a player number and returns all squares occupied by the opponent of the player.
-
#occupied_by_piece(piece_type) ⇒ SquareSet
Find all squares occupied by a piece of a particular type.
-
#occupied_by_player(player_number) ⇒ SquareSet
Takes a player number and returns all squares occupied by the player.
-
#threatened_by(player_number, game_state) ⇒ SquareSet
Returns all squares threatened by the specified player.
-
#unmoved ⇒ SquareSet
Returns all squares with pieces that haven’t moved.
-
#unoccupied_or_occupied_by_opponent(player_number) ⇒ SquareSet
Takes a player number and returns all squares unoccupied or occupied by the opponent of the player.
Constructor Details
#initialize(squares:) ⇒ SquareSet
24 25 26 27 28 29 30 31 32 |
# File 'lib/just_chess/square_set.rb', line 24 def initialize(squares: ) @squares = if squares.all? { |s| s.instance_of?(Hash) } squares.map { |s| Square.new(s) } elsif squares.all? { |s| s.instance_of?(Square) } squares else raise ArgumentError, "all squares must have the same class" end end |
Instance Method Details
#as_json ⇒ Hash
serializes the squares as a hash
37 38 39 |
# File 'lib/just_chess/square_set.rb', line 37 def as_json squares.map(&:as_json) end |
#excluding_piece(piece_type) ⇒ SquareSet
Find all squares occupied by a piece not of a particular type
129 130 131 |
# File 'lib/just_chess/square_set.rb', line 129 def excluding_piece(piece_type) select { |s| s.piece && !s.piece.is_a?(piece_type) } end |
#find_by_piece_id(piece_id) ⇒ Square
Find the square with the matching piece identifier
Example:
# Find the square with a piece of id 4
square_set.find_by_piece_id(4)
50 51 52 |
# File 'lib/just_chess/square_set.rb', line 50 def find_by_piece_id(piece_id) find { |s| s.piece && s.piece.id == piece_id } end |
#find_king_for_player(player_number) ⇒ Square
Find the square occupied by the player’s king
Example:
# Find the square occupied by player 2's king
square_set.find_king_for_player(2)
63 64 65 |
# File 'lib/just_chess/square_set.rb', line 63 def find_king_for_player(player_number) find { |s| s.piece && s.piece.is_a?(JustChess::King) && s.occupied_by_player?(player_number) } end |
#in_direction(square, direction_y) ⇒ SquareSet
Find all squares in the y direction of square
Example:
# Get all squares up from square_a
square_set.in_direction(square_a, -1)
79 80 81 |
# File 'lib/just_chess/square_set.rb', line 79 def in_direction(square, direction_y) select { |s| BoardGameGrid::Vector.new(square, s).direction.y == direction_y } end |
#occupied_by_opponent(player_number) ⇒ SquareSet
Takes a player number and returns all squares occupied by the opponent of the player
99 100 101 |
# File 'lib/just_chess/square_set.rb', line 99 def occupied_by_opponent(player_number) select { |s| s.occupied_by_opponent?(player_number) } end |
#occupied_by_piece(piece_type) ⇒ SquareSet
Find all squares occupied by a piece of a particular type
119 120 121 |
# File 'lib/just_chess/square_set.rb', line 119 def occupied_by_piece(piece_type) select { |s| s.piece && s.piece.is_a?(piece_type) } end |
#occupied_by_player(player_number) ⇒ SquareSet
Takes a player number and returns all squares occupied by the player
89 90 91 |
# File 'lib/just_chess/square_set.rb', line 89 def occupied_by_player(player_number) select { |s| s.occupied_by_player?(player_number) } end |
#threatened_by(player_number, game_state) ⇒ SquareSet
Returns all squares threatened by the specified player
149 150 151 152 153 154 155 |
# File 'lib/just_chess/square_set.rb', line 149 def threatened_by(player_number, game_state) _squares = occupied_by_player(player_number).map do |s| s.piece.capture_squares(s, game_state).squares end.flatten.uniq self.class.new(squares: _squares) end |
#unmoved ⇒ SquareSet
Returns all squares with pieces that haven’t moved
136 137 138 |
# File 'lib/just_chess/square_set.rb', line 136 def unmoved select { |s| s.piece && s.piece.has_not_moved? } end |
#unoccupied_or_occupied_by_opponent(player_number) ⇒ SquareSet
Takes a player number and returns all squares unoccupied or occupied by the opponent of the player
109 110 111 |
# File 'lib/just_chess/square_set.rb', line 109 def unoccupied_or_occupied_by_opponent(player_number) select { |s| s.unoccupied? || s.occupied_by_opponent?(player_number) } end |