Class: CubeCoordinates

Inherits:
Object
  • Object
show all
Defined in:
lib/software_challenge_client/cube_coordinates.rb

Overview

CubeCoordinates erleichtern viele Berechnungen auf einem hexagonalen Spielfeld. Siehe www.redblobgames.com/grids/hexagons/#coordinates-cube

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, z = nil) ⇒ CubeCoordinates

Returns a new instance of CubeCoordinates.



7
8
9
10
11
12
# File 'lib/software_challenge_client/cube_coordinates.rb', line 7

def initialize(x, y, z = nil)
  @x = x
  @y = y
  @z = z.nil? ? -x - y : z
  throw InvalidArgumentException("sum of coordinates #{@x}, #{@y}, #{@z} have to be equal 0") if @x + @y + @z != 0
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



5
6
7
# File 'lib/software_challenge_client/cube_coordinates.rb', line 5

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



5
6
7
# File 'lib/software_challenge_client/cube_coordinates.rb', line 5

def y
  @y
end

#zObject (readonly)

Returns the value of attribute z.



5
6
7
# File 'lib/software_challenge_client/cube_coordinates.rb', line 5

def z
  @z
end

Instance Method Details

#==(other) ⇒ Object



14
15
16
# File 'lib/software_challenge_client/cube_coordinates.rb', line 14

def ==(other)
  x == other.x && y == other.y && z == other.z
end

#inspectObject



22
23
24
# File 'lib/software_challenge_client/cube_coordinates.rb', line 22

def inspect
  to_s
end

#to_sObject



18
19
20
# File 'lib/software_challenge_client/cube_coordinates.rb', line 18

def to_s
  "(#{x}, #{y}, #{z})"
end