Class: CoordinateSet

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/software_challenge_client/coordinate_set.rb

Overview

Eine Menge aus Koordinaten

Constant Summary

Constants included from Constants

Constants::BOARD_SIZE, Constants::GAME_IDENTIFIER, Constants::ROUND_LIMIT, Constants::TOTAL_PIECE_SHAPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coordinates) ⇒ CoordinateSet

Erstellt eine neue leere Koordinaten-Menge.



12
13
14
# File 'lib/software_challenge_client/coordinate_set.rb', line 12

def initialize(coordinates)
  @coordinates = coordinates
end

Instance Attribute Details

#coordinatesArray<Coordinates> (readonly)

Returns Die enthaltenen Koordinaten.

Returns:

  • (Array<Coordinates>)

    Die enthaltenen Koordinaten.



9
10
11
# File 'lib/software_challenge_client/coordinate_set.rb', line 9

def coordinates
  @coordinates
end

Instance Method Details

#==(other) ⇒ Object



89
90
91
# File 'lib/software_challenge_client/coordinate_set.rb', line 89

def ==(other)
  coordinates.sort == other.coordinates.sort
end

#alignObject

Bewege den Bereich der enthaltenen Koordinaten zum Ursprung



44
45
46
47
48
49
50
# File 'lib/software_challenge_client/coordinate_set.rb', line 44

def align
  minX = coordinates.map(&:x).min
  minY = coordinates.map(&:y).min
  transform do |it|
    Coordinates.new(it.x - minX, it.y - minY)
  end
end

#areaObject

Gibt die Größe des kleinsten Bereichs zurück, in dem alle enthaltenen Punkte liegen



35
36
37
38
39
40
41
# File 'lib/software_challenge_client/coordinate_set.rb', line 35

def area
  minX = coordinates.map(&:x).min
  minY = coordinates.map(&:y).min
  maxX = coordinates.map(&:x).max
  maxY = coordinates.map(&:y).max
  Coordinates.new(maxX - minX + 1, maxY - minY + 1)
end

#flip(should_flip = true) ⇒ Object

Invertiert die X-Koordinate aller Koordinaten in dieser Menge



17
18
19
20
21
22
23
# File 'lib/software_challenge_client/coordinate_set.rb', line 17

def flip(should_flip = true)
  return self unless should_flip

  transform do |it|
    Coordinates.new(-it.x, it.y)
  end.align
end

#mirrorObject

Spiegle alle enthaltenen Koordinaten um beide Achsen



83
84
85
86
87
# File 'lib/software_challenge_client/coordinate_set.rb', line 83

def mirror
  transform do |it|
    Coordinates.new(-it.x, -it.y)
  end
end

#rotate(rotation) ⇒ CoordinateSet

Wende eine Rotation auf den Stein an

Parameters:

  • rotation (Rotation)

    Die anzuwendene Rotation

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/software_challenge_client/coordinate_set.rb', line 55

def rotate(rotation)
  case rotation
  when Rotation::NONE
    self
  when Rotation::RIGHT
    turn_right.align
  when Rotation::MIRROR
    mirror.align
  when Rotation::LEFT
    turn_left.align
  end
end

#transformObject

Enumeriert die enthaltenen Koordinaten



26
27
28
29
30
31
32
# File 'lib/software_challenge_client/coordinate_set.rb', line 26

def transform
  CoordinateSet.new(
    coordinates.map do |it|
      yield it
    end
  )
end

#turn_leftObject

Drehe alle enthaltenen Koordinaten um 90° nach links



76
77
78
79
80
# File 'lib/software_challenge_client/coordinate_set.rb', line 76

def turn_left
  transform do |it|
    Coordinates.new(it.y, -it.x)
  end
end

#turn_rightObject

Drehe alle enthaltenen Koordinaten um 90° nach rechts



69
70
71
72
73
# File 'lib/software_challenge_client/coordinate_set.rb', line 69

def turn_right
  transform do |it|
    Coordinates.new(-it.y, it.x)
  end
end