Class: JustXiangqi::PieceFactory

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

Overview

Piece Factory

Generates pieces from a hash of arguments

Constant Summary collapse

CLASSES =

A mapping of type descriptions to classes.

{
  'jiang' => Jiang,
  'ju' => Ju,
  'ma' => Ma,
  'pao' => Pao,
  'shi' => Shi,
  'xiang' => Xiang,
  'zu' => Zu 
}

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
JustXiangqi::PieceFactory.new({
  type: 'zu',
  id: 1,
  player_number: 2
})

Parameters:

  • args (Hash, Piece)

    the initial attributes of the piece, hash requires type key



39
40
41
# File 'lib/just_xiangqi/piece_factory.rb', line 39

def initialize(args)
  @args = args
end

Instance Method Details

#buildPiece

Returns a piece based on the initial arguments.

Returns:



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/just_xiangqi/piece_factory.rb', line 46

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