class JustChess::Square

Square

A Square on a checker board

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 [String] 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
JustChess::Square.new({
  id: 'a1',
  x: 1,
  y: 0,
  piece: { id: 1, player_number: 1, type: 'pawn' }
})
# File lib/just_chess/square.rb, line 33
def initialize(id: , x: , y: , piece: nil)
  @id = id
  @x = x
  @y = y
  @piece = PieceFactory.new(piece).build
end

Public Instance Methods

last_rank(player_number) click to toggle source

Is the square the last rank for the specified player?

@return [Boolean]

# File lib/just_chess/square.rb, line 54
def last_rank(player_number)
  rank_number(player_number) == 8
end
rank_number(player_number) click to toggle source

returns the rank number of the square for the specified player

@return [Fixnum]

# File lib/just_chess/square.rb, line 43
def rank_number(player_number)
  if player_number == 1
    8 - @y
  else
    @y + 1
  end
end