class JustChess::SquareSet

Square Set

A collection of Squares with useful filtering functions

Public Class Methods

new(squares: ) click to toggle source

New objects can be instantiated by passing in a hash with squares. They can be square objects or hashes.

@param [Array<Square,Hash>] squares

An array of squares, each with x and y co-ordinates and a piece.

Example:

# Instantiates a new Square Set
JustChess::SquareSet.new({
  squares: [
    { x: 1, y: 0, piece: { player_number: 1, direction: 1, king: false }}
  ]
})
# 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

Public Instance Methods

find_king_for_player(player_number) click to toggle source

Find the square occupied by the player's king

@param [Fixnum] player_number

the number of the player

@return [Square]

Example:

# Find the square occupied by player 2's king
square_set.find_king_for_player(2)
# File lib/just_chess/square_set.rb, line 43
def find_king_for_player(player_number)
  find { |s| s.piece && s.piece.is_a?(JustChess::King) && s.occupied_by_player?(player_number) }
end
threatened_by(player_number, game_state) click to toggle source

Returns all squares threatened by the specified player

@param [Fixnum] player_number

the player's number.

@param [GameState] game_state

the current game state.

@return [SquareSet]

# File lib/just_chess/square_set.rb, line 63
def threatened_by(player_number, game_state)
  _squares = occupied_by_player(player_number).map do |s|
    s.piece.potential_capture_squares(s, game_state).squares
  end.flatten.uniq

  self.class.new(squares: _squares)
end
unmoved() click to toggle source

Returns all squares with pieces that haven't moved

@return [SquareSet]

# File lib/just_chess/square_set.rb, line 50
def unmoved
  select { |s| s.piece && s.piece.has_not_moved? }
end