Class: Advance

Inherits:
Action show all
Defined in:
lib/software_challenge_client/action.rb

Overview

Ein Vorwärtszug, um spezifizierte Distanz. Verbrauchte Karroten werden mit k = (distance * (distance + 1)) / 2 berechnet (Gaußsche Summe)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Action

#invalid

Constructor Details

#initialize(distance) ⇒ Advance

Returns a new instance of Advance.



36
37
38
# File 'lib/software_challenge_client/action.rb', line 36

def initialize(distance)
  @distance = distance
end

Instance Attribute Details

#distanceObject (readonly)

Returns the value of attribute distance.



34
35
36
# File 'lib/software_challenge_client/action.rb', line 34

def distance
  @distance
end

Instance Method Details

#==(other) ⇒ Object



61
62
63
# File 'lib/software_challenge_client/action.rb', line 61

def ==(other)
  other.type == type && other.distance == distance
end

#perform!(gamestate) ⇒ Object

Perform the action.

performed. Performing may change the game state. The action is performed for the current player of the game state.

Parameters:

  • gamestate (GameState)

    The game state on which the action will be



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/software_challenge_client/action.rb', line 45

def perform!(gamestate)
  valid, message = GameRules.is_valid_to_advance(gamestate, distance)
  invalid(message) unless valid
  # perform state changes
  required_carrots = distance * (distance + 1) / 2
  gamestate.current_player.carrots -= required_carrots
  gamestate.current_player.index += distance
  if gamestate.current_field.type == FieldType::HARE
    gamestate.current_player.must_play_card = true
  end
end

#typeObject



57
58
59
# File 'lib/software_challenge_client/action.rb', line 57

def type
  :advance
end