Class: Pitchcar::Pieces::Piece

Inherits:
Object
  • Object
show all
Defined in:
lib/pieces/piece.rb

Direct Known Subclasses

Left, Right, Straight

Constant Summary collapse

DIRECTIONS =
{ NORTH: 0, EAST: 1, WEST: 2, SOUTH: 3 }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties) ⇒ Piece

Returns a new instance of Piece.



10
11
12
13
14
# File 'lib/pieces/piece.rb', line 10

def initialize(properties)
  self.x = properties[:x]
  self.y = properties[:y]
  self.direction = properties[:direction]
end

Instance Attribute Details

#directionObject

Returns the value of attribute direction.



8
9
10
# File 'lib/pieces/piece.rb', line 8

def direction
  @direction
end

#typeObject

Returns the value of attribute type.



8
9
10
# File 'lib/pieces/piece.rb', line 8

def type
  @type
end

#xObject

Returns the value of attribute x.



8
9
10
# File 'lib/pieces/piece.rb', line 8

def x
  @x
end

#yObject

Returns the value of attribute y.



8
9
10
# File 'lib/pieces/piece.rb', line 8

def y
  @y
end

Class Method Details

.first_from_string(piece_string) ⇒ Object



16
17
18
# File 'lib/pieces/piece.rb', line 16

def self.first_from_string(piece_string)
  Pieces::Piece.type_from_string(piece_string).new(x: 0, y: 0, direction: DIRECTIONS[:SOUTH])
end

.type_from_string(string) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/pieces/piece.rb', line 20

def self.type_from_string(string)
  case string
  when 'S'
    Straight
  when 'L'
    Left
  when 'R'
    Right
  end
end

Instance Method Details

#coordinateObject



39
40
41
# File 'lib/pieces/piece.rb', line 39

def coordinate
  { x: x, y: y }
end

#nameObject



35
36
37
# File 'lib/pieces/piece.rb', line 35

def name
  self.class.name.split('::').last
end

#to_hObject



31
32
33
# File 'lib/pieces/piece.rb', line 31

def to_h
  { x: x, y: y, type: name, direction_name: DIRECTIONS.key(direction).downcase, direction: direction }
end