Class: GameState

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

Overview

The state of a game

Author:

  • Ralf-Tobias Diekert

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGameState

Returns a new instance of GameState.



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

def initialize
  self.currentPlayerColor = PlayerColor::RED
  self.startPlayerColor = PlayerColor::RED
  self.board = Board.new(true)
end

Instance Attribute Details

#bluePlayer (readonly)

Returns the blue player.

Returns:

  • (Player)

    the blue player



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

def blue
  @blue
end

#boardBoard

Returns the game’s board.

Returns:

  • (Board)

    the game’s board



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

def board
  @board
end

#conditionCondition

Returns the winner and winning reason.

Returns:

  • (Condition)

    the winner and winning reason



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

def condition
  @condition
end

#currentPlayerColorPlayerColor

Returns the current player’s color.

Returns:



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

def currentPlayerColor
  @currentPlayerColor
end

#lastMoveMove

Returns the last move, that was made.

Returns:

  • (Move)

    the last move, that was made



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

def lastMove
  @lastMove
end

#redPlayer (readonly)

Returns the red player.

Returns:



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

def red
  @red
end

#startPlayerColorPlayerColor

Returns the start-player’s color.

Returns:



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

def startPlayerColor
  @startPlayerColor
end

#turnInteger

Returns turn number.

Returns:

  • (Integer)

    turn number



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

def turn
  @turn
end

Instance Method Details

#addPlayer(player) ⇒ Object

adds a player to the gamestate

Parameters:

  • player (Player)

    the player, that will be added



47
48
49
50
51
52
53
54
# File 'lib/software_challenge_client/game_state.rb', line 47

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

#bottomMostFieldInCircuit(circuit) ⇒ Object

the following functions are helpers for the points calculation



278
279
280
281
282
283
284
285
286
# File 'lib/software_challenge_client/game_state.rb', line 278

def bottomMostFieldInCircuit(circuit)
  bottomMostField = circuit[0]
  for f in circuit
    if f.y < bottomMostField.y
      bottomMostField = f
    end
  end
  return bottomMostField
end

#circuit(circuit, visited) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/software_challenge_client/game_state.rb', line 318

def circuit(circuit, visited)
  changed = false;
  toBeAddedFields = Array.new
  for f in circuit
    if !visited.include?(f)
      changed = true
      visited.push(f)
      for c in self.board.getConnections(f.x,f.y)
        if !circuit.include?(self.board.fields[c.x2][c.y2])
          toBeAddedFields.push(self.board.fields[c.x2][c.y2])
        end
        if !circuit.include?(self.board.fields[c.x1][c.y1])
          toBeAddedFields.push(self.board.fields[c.x1][c.y1])
        end
      end
    end
  end
  circuit.push(*toBeAddedFields)
  if changed
    return self.circuit(circuit, visited)
  else 
    return circuit
  end
end

#currentPlayerPlayer

gets the current player

Returns:

  • (Player)

    the current player



59
60
61
62
63
64
65
# File 'lib/software_challenge_client/game_state.rb', line 59

def currentPlayer
  if self.currentPlayerColor == PlayerColor::RED
    return self.red
  else
    return self.blue
  end
end

#endGame(winner, reason) ⇒ Object

sets the game-ended condition

Parameters:

  • winner (Player)

    the winner of the game

  • reason (String)

    the winning reason



202
203
204
205
206
# File 'lib/software_challenge_client/game_state.rb', line 202

def endGame(winner, reason)
  if condition.nil?
    @condition = Condition.new(winner, reason)
  end
end

#gameEnded?Boolean

has the game ended?

Returns:

  • (Boolean)

    true, if the game has allready ended



211
212
213
# File 'lib/software_challenge_client/game_state.rb', line 211

def gameEnded?
  return !self.condition.nil?
end

#gameStatsArray<Integer>

gets the players’ statistics

Returns:

  • (Array<Integer>)

    the points for both players



182
183
184
185
186
187
188
189
# File 'lib/software_challenge_client/game_state.rb', line 182

def gameStats
  stats = Array.new(2, Array.new(1))

  stats[0][0] = self.red.points
  stats[1][0] = self.blue.points
  
  return stats
end

#getPossibleMovesArray<Move>

gets all possible moves

Returns:

  • (Array<Move>)

    a list of all possible moves



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/software_challenge_client/game_state.rb', line 124

def getPossibleMoves
  enemyFieldType = currentPlayer.color == PlayerColor::RED ? FieldType::BLUE : FieldType::RED
  moves = Array.new
  for x in 0..(Constants::SIZE-1)
    for y in 0..(Constants::SIZE-1)
      if (self.board.fields[x][y].ownerColor == PlayerColor::NONE &&
            self.board.fields[x][y].type != FieldType::SWAMP &&
            self.board.fields[x][y].type != enemyFieldType)
        moves.push(Move.new(x, y))
      end
    end
  end
  return moves
end

#leftMostFieldInCircuit(circuit) ⇒ Object



298
299
300
301
302
303
304
305
306
# File 'lib/software_challenge_client/game_state.rb', line 298

def leftMostFieldInCircuit(circuit)
  leftMostField = circuit[0]
  for f in circuit
    if f.x < leftMostField.x
      leftMostField = f
    end
  end
  return leftMostField
end

#otherPlayerPlayer

gets the other (not the current) player

Returns:

  • (Player)

    the other (not the current) player



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

def otherPlayer
  if self.currentPlayerColor == PlayerColor::RED
    return self.blue 
  else
    return self.red
  end
end

#otherPlayerColorPlayerColor

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

Returns:

  • (PlayerColor)

    the other (not the current) player’s color



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

def otherPlayerColor
  return PlayerColor.opponentColor(self.currentPlayerColor)
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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/software_challenge_client/game_state.rb', line 143

def perform(move, player)
  if !move.nil?
    if move.x < Constants::SIZE && move.y < Constants::SIZE && 
        move.x >= 0 && move.y >= 0
      if self.getPossibleMoves.include?(move) 
        self.board.put(move.x, move.y, player)
        player.points = self.pointsForPlayer(player)      
      else
        raise "Der Zug ist nicht möglich, denn der Platz ist bereits besetzt oder nicht besetzbar."
      end
    else
      raise "Startkoordinaten sind nicht innerhalb des Spielfeldes."
    end
  end
end

#playerNamesArray<String>

get the players’ names

Returns:

  • (Array<String>)

    the names for both players



194
195
196
# File 'lib/software_challenge_client/game_state.rb', line 194

def playerNames
  return [red.displayName, blue.displayName]
end

#playerStats(playerColor) ⇒ Integer

gets a player’s points by the player’s color

Parameters:

  • playerColor (PlayerColor)

    the player’s color, whos statistics will be returned

Returns:

  • (Integer)

    the points of the player



163
164
165
# File 'lib/software_challenge_client/game_state.rb', line 163

def playerStats(player)
  return self.playerStats(player.color)
end

#pointsForPlayer(player) ⇒ Integer

calculates a player’s points

 @param player [Player] the player, whos point will be calculated

Returns:

  • (Integer)

    the points of the player



233
234
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
270
271
272
273
274
275
# File 'lib/software_challenge_client/game_state.rb', line 233

def pointsForPlayer(player)
  playerColor = player.color
  longestPath = 0

  outermostFieldsInCircuit = Array.new(Array.new)
  visited = Array.new(Constants::SIZE).map {|j| Array.new(Constants::SIZE).map {|i| false}} #// all by default initialized to false
  for x in 0..(Constants::SIZE-1)
    for y in 0..(Constants::SIZE-1)
      if visited[x][y] == false
        if self.board.fields[x][y].ownerColor == playerColor
          startOfCircuit = Array.new
          startOfCircuit.push(self.board.fields[x][y])
          circuit = self.circuit(startOfCircuit, Array.new)
          for f in circuit
            visited[f.x][f.y] = true
          end
          outermost = Array.new(2)
          if playerColor == PlayerColor::RED

            outermost[0] = self.bottomMostFieldInCircuit(circuit)
            outermost[1] = self.topMostFieldInCircuit(circuit)
          else
            outermost[0] = leftMostFieldInCircuit(circuit)
            outermost[1] = rightMostFieldInCircuit(circuit)
          end
          outermostFieldsInCircuit.push(outermost)

        end
        visited[x][y] = true
      end
    end
  end
  for fields in outermostFieldsInCircuit
    if (playerColor == PlayerColor::RED && fields[1].y - fields[0].y > longestPath)
      longestPath = fields[1].y - fields[0].y
    end
    if (playerColor == PlayerColor::BLUE && fields[1].x - fields[0].x > longestPath)
      longestPath = fields[1].x - fields[0].x
    end
  end
  
  return longestPath # return longestPath
end

#prepareNextTurn(lastMove) ⇒ Object

prepares next turn and sets the last move

Parameters:

  • the (Move)

    last move



108
109
110
111
112
# File 'lib/software_challenge_client/game_state.rb', line 108

def prepareNextTurn(lastMove)
  @turn++
  @lastMove = lastMove;
  self.switchCurrentPlayer()
end

#rightMostFieldInCircuit(circuit) ⇒ Object



308
309
310
311
312
313
314
315
316
# File 'lib/software_challenge_client/game_state.rb', line 308

def rightMostFieldInCircuit(circuit)
  rightMostField = circuit[0]
  for f in circuit
    if f.x > rightMostField.x
      rightMostField = f
    end
  end
  return rightMostField
end

#roundInteger

gets the current round

Returns:

  • (Integer)

    the current round



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

def round
  return self.turn / 2
end

#startPlayerPlayer

gets the start player

Returns:

  • (Player)

    the startPlayer



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

def startPlayer
  if self.startPlayer == PlayerColor::RED 
    return self.red
  else
    return self.blue
  end
end

#switchCurrentPlayerObject

switches current player



97
98
99
100
101
102
103
# File 'lib/software_challenge_client/game_state.rb', line 97

def switchCurrentPlayer
  if currentPlayer.color == PlayerColor::RED
    @currentPlayer = self.blue
  else
    @currentPlayer = self.red
  end
end

#topMostFieldInCircuit(circuit) ⇒ Object



288
289
290
291
292
293
294
295
296
# File 'lib/software_challenge_client/game_state.rb', line 288

def topMostFieldInCircuit(circuit)
  topMostField = circuit[0]
  for f in circuit
    if f.y > topMostField.y
      topMostField = f
    end
  end
  return topMostField
end

#winnerPlayer

gets the game’s winner

Returns:

  • (Player)

    the game’s winner



218
219
220
# File 'lib/software_challenge_client/game_state.rb', line 218

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

#winningReasonString

gets the winning reason

Returns:

  • (String)

    the winning reason



225
226
227
# File 'lib/software_challenge_client/game_state.rb', line 225

def winningReason
  return condition.nil? ? nil : self.condition.reason
end