Class: GameState

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

Overview

The state of a game, as received from the server.

Constant Summary collapse

POINTS_PER_TILE =
5
POINTS_PER_PASSENGER =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGameState

Returns a new instance of GameState.



56
57
58
59
60
61
62
63
# File 'lib/software_challenge_client/game_state.rb', line 56

def initialize
  @current_player_color = PlayerColor::RED
  @start_player_color = PlayerColor::RED
  @board = Board.new
  @free_acceleration = true
  @free_turn = true
  @additional_free_turn_after_push = false
end

Instance Attribute Details

#additional_free_turn_after_pushBoolean Also known as: additional_free_turn_after_push?

available.

Returns:

  • (Boolean)

    True if the free turning for this turn is still



50
51
52
# File 'lib/software_challenge_client/game_state.rb', line 50

def additional_free_turn_after_push
  @additional_free_turn_after_push
end

#bluePlayer (readonly)

Returns the blue player.

Returns:

  • (Player)

    the blue player



25
26
27
# File 'lib/software_challenge_client/game_state.rb', line 25

def blue
  @blue
end

#boardBoard

Returns the game’s board.

Returns:

  • (Board)

    the game’s board



28
29
30
# File 'lib/software_challenge_client/game_state.rb', line 28

def board
  @board
end

#conditionCondition

Returns the winner and winning reason.

Returns:

  • (Condition)

    the winner and winning reason



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

def condition
  @condition
end

#current_player_colorPlayerColor

Returns the current player’s color.

Returns:



19
20
21
# File 'lib/software_challenge_client/game_state.rb', line 19

def current_player_color
  @current_player_color
end

#free_accelerationBoolean Also known as: free_acceleration?

Returns true if the free acceleration for this turn is still available.

Returns:

  • (Boolean)

    true if the free acceleration for this turn is still available.



38
39
40
# File 'lib/software_challenge_client/game_state.rb', line 38

def free_acceleration
  @free_acceleration
end

#free_turnBoolean Also known as: free_turn?

available.

Returns:

  • (Boolean)

    True if the free turning for this turn is still



44
45
46
# File 'lib/software_challenge_client/game_state.rb', line 44

def free_turn
  @free_turn
end

#lastMoveMove

Returns the last move, that was made.

Returns:

  • (Move)

    the last move, that was made



31
32
33
# File 'lib/software_challenge_client/game_state.rb', line 31

def lastMove
  @lastMove
end

#redPlayer (readonly)

Returns the red player.

Returns:



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

def red
  @red
end

#start_player_colorPlayerColor

Returns the start-player’s color.

Returns:



16
17
18
# File 'lib/software_challenge_client/game_state.rb', line 16

def start_player_color
  @start_player_color
end

#turnInteger

Returns turn number.

Returns:

  • (Integer)

    turn number



13
14
15
# File 'lib/software_challenge_client/game_state.rb', line 13

def turn
  @turn
end

Instance Method Details

#==(other) ⇒ Object

Compared with other state.



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/software_challenge_client/game_state.rb', line 160

def ==(other)
  turn == other.turn &&
    start_player_color == other.start_player_color &&
    current_player_color == other.current_player_color &&
    red == other.red &&
    blue == other.blue &&
    board == other.board &&
    lastMove == other.lastMove &&
    free_acceleration == other.free_acceleration &&
    condition == other.condition
end

#add_player(player) ⇒ Object

adds a player to the gamestate

Parameters:

  • player (Player)

    the player, that will be added



68
69
70
71
72
73
74
# File 'lib/software_challenge_client/game_state.rb', line 68

def add_player(player)
  if player.color == PlayerColor::RED
    @red = player
  elsif player.color == PlayerColor::BLUE
    @blue = player
  end
end

#current_playerPlayer

gets the current player

Returns:

  • (Player)

    the current player



79
80
81
82
83
84
# File 'lib/software_challenge_client/game_state.rb', line 79

def current_player
  if current_player_color == PlayerColor::RED
  then red
  else blue
  end
end

#deep_cloneObject

Create a deep copy of the gamestate. Can be used to perform moves on without changing the original gamestate.



174
175
176
# File 'lib/software_challenge_client/game_state.rb', line 174

def deep_clone
  Marshal.load(Marshal.dump(self))
end

#game_ended?Boolean

has the game ended?

Returns:

  • (Boolean)

    true, if the game has allready ended



124
125
126
# File 'lib/software_challenge_client/game_state.rb', line 124

def game_ended?
  !condition.nil?
end

#occupied_by_other_player?(field) ⇒ Boolean

Returns true if the given field is occupied by the other (not current) player.

Returns:

  • (Boolean)

    true if the given field is occupied by the other (not current) player.



155
156
157
# File 'lib/software_challenge_client/game_state.rb', line 155

def occupied_by_other_player?(field)
  field.x == other_player.x && field.y == other_player.y
end

#other_playerPlayer

gets the other (not the current) player

Returns:

  • (Player)

    the other (not the current) player



89
90
91
92
93
94
95
# File 'lib/software_challenge_client/game_state.rb', line 89

def other_player
  if current_player_color == PlayerColor::RED
    return blue
  else
    return red
  end
end

#other_player_colorPlayerColor

gets the other (not the current) player’s color

Returns:

  • (PlayerColor)

    the other (not the current) player’s color



100
101
102
# File 'lib/software_challenge_client/game_state.rb', line 100

def other_player_color
  PlayerColor.opponent_color(current_player_color)
end

#perform!(move, player) ⇒ Object

performs a move on the gamestate

Parameters:

  • move (Move)

    the move, that will be performed

  • player (Player)

    the player, who makes the move



115
116
117
118
119
# File 'lib/software_challenge_client/game_state.rb', line 115

def perform!(move, player)
  move.actions.each do |action|
    action.perform!(self, player)
  end
end

#points_for_player(player) ⇒ Integer

calculates a player’s points based on the current gamestate

Parameters:

  • player (Player)

    the player, whos point will be calculated

Returns:

  • (Integer)

    the points of the player



146
147
148
149
150
151
# File 'lib/software_challenge_client/game_state.rb', line 146

def points_for_player(player)
  player_field = board.field(player.x, player.y)
  POINTS_PER_TILE * player_field.index +
    POINTS_PER_PASSENGER * player.passengers +
    player_field.points
end

#roundInteger

gets the current round

Returns:

  • (Integer)

    the current round



107
108
109
# File 'lib/software_challenge_client/game_state.rb', line 107

def round
  turn / 2
end

#winnerPlayer

gets the game’s winner

Returns:

  • (Player)

    the game’s winner



131
132
133
# File 'lib/software_challenge_client/game_state.rb', line 131

def winner
  condition.nil? ? nil : condition.winner
end

#winning_reasonString

gets the winning reason

Returns:

  • (String)

    the winning reason



138
139
140
# File 'lib/software_challenge_client/game_state.rb', line 138

def winning_reason
  condition.nil? ? nil : condition.reason
end