Class: Push

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

Overview

Push the opponent in #direction

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Action

#invalid

Constructor Details

#initialize(direction) ⇒ Push

Returns a new instance of Push.

Parameters:



230
231
232
# File 'lib/software_challenge_client/action.rb', line 230

def initialize(direction)
  @direction = direction
end

Instance Attribute Details

#directionDirection (readonly)

Returns the direction where to push.

Returns:

  • (Direction)

    the direction where to push.



227
228
229
# File 'lib/software_challenge_client/action.rb', line 227

def direction
  @direction
end

Instance Method Details

#==(other) ⇒ Object



275
276
277
# File 'lib/software_challenge_client/action.rb', line 275

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

#perform!(gamestate, current_player) ⇒ Object

Perform the action.

Parameters:

  • gamestate (GameState)

    The game state on which the action will be performed. Performing may change the game state.

  • current_player (Player)

    The player for which the action will be performed.



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/software_challenge_client/action.rb', line 235

def perform!(gamestate, current_player)
  if gamestate.other_player.x != current_player.x ||
     gamestate.other_player.y != current_player.y
    invalid 'Abdrängen ist nur auf dem Feld des Gegners möglich.'
  end
  other_player_field =
    gamestate.board.field(gamestate.other_player.x, gamestate.other_player.y)
  if other_player_field.type == FieldType::SANDBANK
    invalid 'Abdrängen von einer Sandbank ist nicht erlaubt.'
  end
  if direction == Direction.get_turn_direction(current_player.direction, 3)
    invalid 'Man darf nicht hinter sich abdrängen.'
  end

  target_x, target_y =
    gamestate.board.get_neighbor(
      gamestate.other_player.x,
      gamestate.other_player.y,
      direction
    )

  required_movement = 1
  if gamestate.board.field(target_x, target_y).type == FieldType::LOG
    required_movement += 1
  end
  if required_movement > current_player.movement
    invalid 'Nicht genug Bewegungspunkte zum abdrängen '\
            "(brauche #{required_movement})"
  end

  current_player.movement -= required_movement

  gamestate.other_player.x = target_x
  gamestate.other_player.y = target_y
end

#typeObject



271
272
273
# File 'lib/software_challenge_client/action.rb', line 271

def type
  :push
end