Class: JustChess::SquareSet

Inherits:
BoardGameGrid::SquareSet
  • Object
show all
Defined in:
lib/just_chess/square_set.rb

Overview

Square Set

A collection of Squares with useful filtering functions

Instance Method Summary collapse

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

Parameters:

  • squares (Array<Square,Hash>)

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



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_jsonHash

serializes the squares as a hash

Returns:

  • (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

Parameters:

  • piece_type (Class)

    the class of the piece.

Returns:



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)

Parameters:

  • piece_id (Fixnum)

    the unique identifier of the piece.

Returns:



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)

Parameters:

  • player_number (Fixnum)

    the number of the player

Returns:



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)

Parameters:

  • square (Square)

    the originating square

  • direction_y (Fixnum)

    the direction, either up (-1) or down (1)

Returns:



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

Parameters:

  • player_number (Fixnum)

    the player’s number.

Returns:



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

Parameters:

  • piece_type (Class)

    the class of the piece.

Returns:



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

Parameters:

  • player_number (Fixnum)

    the player’s number.

Returns:



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

Parameters:

  • player_number (Fixnum)

    the player’s number.

  • game_state (GameState)

    the current game state.

Returns:



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

#unmovedSquareSet

Returns all squares with pieces that haven’t moved

Returns:



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

Parameters:

  • player_number (Fixnum)

    the player’s number.

Returns:



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