Class: JustChess::PieceFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/just_chess/piece_factory.rb

Overview

Piece Factory

Generates pieces from a hash of arguments

Constant Summary collapse

CLASSES =

A mapping of type descriptions to classes

{
  'pawn' => Pawn,
  'rook' => Rook,
  'knight' => Knight,
  'bishop' => Bishop,
  'queen' => Queen,
  'king' => King
}

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ PieceFactory

New objects can be instantiated by passing in a hash or the piece.

Example:

# Instantiates a new PieceFactory
JustChess::PieceFactory.new({
  type: 'pawn',
  id: 1,
  player_number: 2
})

Parameters:

  • args (Hash, Piece)

    the initial attributes of the piece, hash requires type key



37
38
39
# File 'lib/just_chess/piece_factory.rb', line 37

def initialize(args)
  @args = args
end

Instance Method Details

#buildPiece

Returns a piece based on the initial arguments.

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/just_chess/piece_factory.rb', line 44

def build
  case @args
  when Hash
    build_from_hash
  when Piece
    @args
  when nil
    nil
  else
    raise ArgumentError, "piece must be Hash or NilClass"
  end
end