Class: Move

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

Overview

Ein Spielzug. Er ist definiert durch das Koordinatenpaar des Ausgangsfeldes (ein Fisch des Spielers, der den Zug machen will) und eine Bewegungsrichtung.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, direction) ⇒ Move

Erstellt einen neuen Zug.


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

def initialize(x, y, direction)
  @x = x
  @y = y
  @direction = direction
  @hints = []
end

Instance Attribute Details

#directionDirection (readonly)


17
18
19
# File 'lib/software_challenge_client/move.rb', line 17

def direction
  @direction
end

#hintsArray<DebugHint> (readonly)


21
22
23
# File 'lib/software_challenge_client/move.rb', line 21

def hints
  @hints
end

#xInteger (readonly)


8
9
10
# File 'lib/software_challenge_client/move.rb', line 8

def x
  @x
end

#yInteger (readonly)


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

def y
  @y
end

Instance Method Details

#==(other) ⇒ Object


39
40
41
# File 'lib/software_challenge_client/move.rb', line 39

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

#add_hint(hint) ⇒ Object


35
36
37
# File 'lib/software_challenge_client/move.rb', line 35

def add_hint(hint)
  @hints.push(hint)
end

#from_fieldCoordinates


48
49
50
# File 'lib/software_challenge_client/move.rb', line 48

def from_field
  Coordinates.new(x, y)
end

#perform!(gamestate) ⇒ Object

Führt den Zug in dem gegebenen Spielzustand aus. Sollte dabei gegen Spielregeln verstossen werden, wird eine InvalidMoveException geworfen.


61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/software_challenge_client/move.rb', line 61

def perform!(gamestate)
  if GameRuleLogic.valid_move(self, gamestate.board)
    type = gamestate.board.field(x, y).type
    gamestate.board.change_field(x, y, FieldType::EMPTY)
    target = GameRuleLogic.move_target(self, gamestate.board)
    gamestate.board.change_field(target.x, target.y, type)
  else
    raise InvalidMoveException.new('Invalid move', self)
  end
  # change the state to the next turn
  gamestate.last_move = self
  gamestate.turn += 1
  gamestate.switch_current_player
end

#target_field(speed) ⇒ Coordinates

Ermittelt die Koordinaten des Zielfeldes des Zuges mit einer gegebenen Zugweite.


80
81
82
# File 'lib/software_challenge_client/move.rb', line 80

def target_field(speed)
  direction.translate(from_field, speed)
end

#to_sObject


43
44
45
# File 'lib/software_challenge_client/move.rb', line 43

def to_s
  "Move: (#{x},#{y}) #{direction}"
end

#valid?(gamestate) ⇒ Boolean

Überprüft, ob der Zug in dem gegebenen Spielzustand regelkonform ausgeführt werden kann.


55
56
57
# File 'lib/software_challenge_client/move.rb', line 55

def valid?(gamestate)
  GameRuleLogic.valid_move(self, gamestate.board)
end