Class: GameState
- Inherits:
-
Object
- Object
- GameState
- Defined in:
- lib/software_challenge_client/game_state.rb
Overview
The state of a game
Instance Attribute Summary collapse
-
#blue ⇒ Player
readonly
The blue player.
-
#board ⇒ Board
The game’s board.
-
#condition ⇒ Condition
The winner and winning reason.
-
#currentPlayerColor ⇒ PlayerColor
The current player’s color.
-
#lastMove ⇒ Move
The last move, that was made.
-
#red ⇒ Player
readonly
The red player.
-
#startPlayerColor ⇒ PlayerColor
The start-player’s color.
-
#turn ⇒ Integer
Turn number.
Instance Method Summary collapse
-
#addPlayer(player) ⇒ Object
adds a player to the gamestate.
-
#bottomMostFieldInCircuit(circuit) ⇒ Object
the following functions are helpers for the points calculation.
- #circuit(circuit, visited) ⇒ Object
-
#currentPlayer ⇒ Player
gets the current player.
-
#endGame(winner, reason) ⇒ Object
sets the game-ended condition.
-
#gameEnded? ⇒ Boolean
has the game ended?.
-
#gameStats ⇒ Array<Integer>
gets the players’ statistics.
-
#getPossibleMoves ⇒ Array<Move>
gets all possible moves.
-
#initialize ⇒ GameState
constructor
A new instance of GameState.
- #leftMostFieldInCircuit(circuit) ⇒ Object
-
#otherPlayer ⇒ Player
gets the other (not the current) player.
-
#otherPlayerColor ⇒ PlayerColor
gets the other (not the current) player’s color.
-
#perform(move, player) ⇒ Object
performs a move on the gamestate.
-
#playerNames ⇒ Array<String>
get the players’ names.
-
#playerStats(playerColor) ⇒ Integer
gets a player’s points by the player’s color.
-
#pointsForPlayer(player) ⇒ Integer
calculates a player’s points.
-
#prepareNextTurn(lastMove) ⇒ Object
prepares next turn and sets the last move.
- #rightMostFieldInCircuit(circuit) ⇒ Object
-
#round ⇒ Integer
gets the current round.
-
#startPlayer ⇒ Player
gets the start player.
-
#switchCurrentPlayer ⇒ Object
switches current player.
- #topMostFieldInCircuit(circuit) ⇒ Object
-
#winner ⇒ Player
gets the game’s winner.
-
#winningReason ⇒ String
gets the winning reason.
Constructor Details
#initialize ⇒ GameState
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
#blue ⇒ Player (readonly)
Returns the blue player.
27 28 29 |
# File 'lib/software_challenge_client/game_state.rb', line 27 def blue @blue end |
#board ⇒ Board
Returns the game’s board.
30 31 32 |
# File 'lib/software_challenge_client/game_state.rb', line 30 def board @board end |
#condition ⇒ Condition
Returns the winner and winning reason.
36 37 38 |
# File 'lib/software_challenge_client/game_state.rb', line 36 def condition @condition end |
#currentPlayerColor ⇒ PlayerColor
Returns the current player’s color.
21 22 23 |
# File 'lib/software_challenge_client/game_state.rb', line 21 def currentPlayerColor @currentPlayerColor end |
#lastMove ⇒ Move
Returns the last move, that was made.
33 34 35 |
# File 'lib/software_challenge_client/game_state.rb', line 33 def lastMove @lastMove end |
#red ⇒ Player (readonly)
Returns the red player.
24 25 26 |
# File 'lib/software_challenge_client/game_state.rb', line 24 def red @red end |
#startPlayerColor ⇒ PlayerColor
Returns the start-player’s color.
18 19 20 |
# File 'lib/software_challenge_client/game_state.rb', line 18 def startPlayerColor @startPlayerColor end |
#turn ⇒ Integer
Returns 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
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 |
#currentPlayer ⇒ Player
gets 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
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?
211 212 213 |
# File 'lib/software_challenge_client/game_state.rb', line 211 def gameEnded? return !self.condition.nil? end |
#gameStats ⇒ Array<Integer>
gets the players’ statistics
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 |
#getPossibleMoves ⇒ Array<Move>
gets 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 |
#otherPlayer ⇒ Player
gets 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 |
#otherPlayerColor ⇒ PlayerColor
gets 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
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 |
#playerNames ⇒ Array<String>
get the players’ names
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
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
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
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 |
#round ⇒ Integer
gets the current round
117 118 119 |
# File 'lib/software_challenge_client/game_state.rb', line 117 def round return self.turn / 2 end |
#startPlayer ⇒ Player
gets the start player
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 |
#switchCurrentPlayer ⇒ Object
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 |
#winner ⇒ Player
gets 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 |
#winningReason ⇒ String
gets 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 |