class JustCheckers::Square
Square
¶ ↑
A Square
on a board of checkers
Public Class Methods
new(id: , x: , y: , piece: nil)
click to toggle source
New objects can be instantiated by passing in a hash with
@param [Fixnum] id
the unique identifier of the square.
@param [Fixnum] x
the x co-ordinate of the square.
@param [Fixnum] y
the y co-ordinate of the square.
@option [Piece,Hash,NilClass] piece
The piece on the square, can be a piece object or hash or nil.
Example:¶ ↑
# Instantiates a new Square JustCheckers::Square.new({ id: 1, x: 1, y: 0, piece: { player_number: 1, king: false } })
# File lib/just_checkers/square.rb, line 34 def initialize(id: , x: , y: , piece: nil) @id = id @x = x @y = y @piece = if piece.is_a?(Hash) Piece.new(**piece) else piece end end
Public Instance Methods
as_json()
click to toggle source
A serialized version of the square as a hash
@return [Hash]
# File lib/just_checkers/square.rb, line 48 def as_json { id: id, x: x, y: y, piece: piece && piece.as_json } end
possible_jumps(piece, squares)
click to toggle source
All squares that a piece on this square could jump to, given the board.
@param [Piece] piece
the piece on this square
@param [SquareSet] squares
the board
@return [SquareSet]
# File lib/just_checkers/square.rb, line 61 def possible_jumps(piece, squares) squares.at_range(self, 2).in_direction_of(piece, self).unoccupied.select do |s| squares.between(self, s).occupied_by_opponent_of(piece.player_number).any? end end
possible_moves(piece, squares)
click to toggle source
All squares that a piece on this square could move to, given the board.
@param [Piece] piece
the piece on this square
@param [SquareSet] squares
the board
@return [SquareSet]
# File lib/just_checkers/square.rb, line 76 def possible_moves(piece, squares) squares.at_range(self, 1).in_direction_of(piece, self).unoccupied end
promotable?()
click to toggle source
Checks if the piece on the square can promote.
@return [Boolean]
# File lib/just_checkers/square.rb, line 83 def promotable? case piece && piece.direction when 1 y == 7 when -1 y == 0 else false end end
promote()
click to toggle source
Promotes the piece if the piece exists.
@return [Boolean]
# File lib/just_checkers/square.rb, line 97 def promote piece && piece.promote end