Class: JustChess::Square

Inherits:
BoardGameGrid::Square
  • Object
show all
Defined in:
lib/just_chess/square.rb

Overview

Square

A Square on a checker board

Instance Method Summary collapse

Constructor Details

#initialize(id:, x:, y:, piece: nil) ⇒ Square

New objects can be instantiated by passing in a hash with

Example:

# Instantiates a new Square
JustChess::Square.new({
  id: 'a1',
  x: 1,
  y: 0,
  piece: { id: 1, player_number: 1, type: 'pawn' }
})

Parameters:

  • id (String)

    the unique identifier of the square.

  • x (Fixnum)

    the x co-ordinate of the square.

  • y (Fixnum)

    the y co-ordinate of the square.

  • [Piece,Hash,NilClass] (Hash)

    a customizable set of options



33
34
35
36
37
38
# 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

Instance Method Details

#last_rank(player_number) ⇒ Boolean

Is the square the last rank for the specified player?

Returns:

  • (Boolean)


54
55
56
# File 'lib/just_chess/square.rb', line 54

def last_rank(player_number)
  rank_number(player_number) == 8
end

#rank_number(player_number) ⇒ Fixnum

returns the rank number of the square for the specified player

Returns:

  • (Fixnum)


43
44
45
46
47
48
49
# File 'lib/just_chess/square.rb', line 43

def rank_number(player_number)
  if player_number == 1
    8 - @y
  else
    @y + 1
  end
end