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.
39 40 41 42 43 |
# File 'lib/software_challenge_client/game_state.rb', line 39 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.
28 29 30 |
# File 'lib/software_challenge_client/game_state.rb', line 28 def blue @blue end |
#board ⇒ Board
Returns the game’s board.
31 32 33 |
# File 'lib/software_challenge_client/game_state.rb', line 31 def board @board end |
#condition ⇒ Condition
Returns the winner and winning reason.
37 38 39 |
# File 'lib/software_challenge_client/game_state.rb', line 37 def condition @condition end |
#currentPlayerColor ⇒ PlayerColor
Returns the current player’s color.
22 23 24 |
# File 'lib/software_challenge_client/game_state.rb', line 22 def currentPlayerColor @currentPlayerColor end |
#lastMove ⇒ Move
Returns the last move, that was made.
34 35 36 |
# File 'lib/software_challenge_client/game_state.rb', line 34 def lastMove @lastMove end |
#red ⇒ Player (readonly)
Returns the red player.
25 26 27 |
# File 'lib/software_challenge_client/game_state.rb', line 25 def red @red end |
#startPlayerColor ⇒ PlayerColor
Returns the start-player’s color.
19 20 21 |
# File 'lib/software_challenge_client/game_state.rb', line 19 def startPlayerColor @startPlayerColor end |
#turn ⇒ Integer
Returns turn number.
16 17 18 |
# File 'lib/software_challenge_client/game_state.rb', line 16 def turn @turn end |
Instance Method Details
#addPlayer(player) ⇒ Object
adds a player to the gamestate
48 49 50 51 52 53 54 55 |
# File 'lib/software_challenge_client/game_state.rb', line 48 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
279 280 281 282 283 284 285 286 287 |
# File 'lib/software_challenge_client/game_state.rb', line 279 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
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/software_challenge_client/game_state.rb', line 319 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
60 61 62 63 64 65 66 |
# File 'lib/software_challenge_client/game_state.rb', line 60 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
203 204 205 206 207 |
# File 'lib/software_challenge_client/game_state.rb', line 203 def endGame(winner, reason) if condition.nil? @condition = Condition.new(winner, reason) end end |
#gameEnded? ⇒ Boolean
has the game ended?
212 213 214 |
# File 'lib/software_challenge_client/game_state.rb', line 212 def gameEnded? return !self.condition.nil? end |
#gameStats ⇒ Array<Integer>
gets the players’ statistics
183 184 185 186 187 188 189 190 |
# File 'lib/software_challenge_client/game_state.rb', line 183 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
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/software_challenge_client/game_state.rb', line 125 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
299 300 301 302 303 304 305 306 307 |
# File 'lib/software_challenge_client/game_state.rb', line 299 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
71 72 73 74 75 76 77 |
# File 'lib/software_challenge_client/game_state.rb', line 71 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
82 83 84 |
# File 'lib/software_challenge_client/game_state.rb', line 82 def otherPlayerColor return PlayerColor.opponentColor(self.currentPlayerColor) end |
#perform(move, player) ⇒ Object
performs a move on the gamestate
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/software_challenge_client/game_state.rb', line 144 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
195 196 197 |
# File 'lib/software_challenge_client/game_state.rb', line 195 def playerNames return [red.displayName, blue.displayName] end |
#playerStats(playerColor) ⇒ Integer
gets a player’s points by the player’s color
164 165 166 |
# File 'lib/software_challenge_client/game_state.rb', line 164 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
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 276 |
# File 'lib/software_challenge_client/game_state.rb', line 234 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
109 110 111 112 113 |
# File 'lib/software_challenge_client/game_state.rb', line 109 def prepareNextTurn(lastMove) @turn++ @lastMove = lastMove; self.switchCurrentPlayer() end |
#rightMostFieldInCircuit(circuit) ⇒ Object
309 310 311 312 313 314 315 316 317 |
# File 'lib/software_challenge_client/game_state.rb', line 309 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
118 119 120 |
# File 'lib/software_challenge_client/game_state.rb', line 118 def round return self.turn / 2 end |
#startPlayer ⇒ Player
gets the start player
89 90 91 92 93 94 95 |
# File 'lib/software_challenge_client/game_state.rb', line 89 def startPlayer if self.startPlayer == PlayerColor::RED return self.red else return self.blue end end |
#switchCurrentPlayer ⇒ Object
switches current player
98 99 100 101 102 103 104 |
# File 'lib/software_challenge_client/game_state.rb', line 98 def switchCurrentPlayer if currentPlayer.color == PlayerColor::RED @currentPlayer = self.blue else @currentPlayer = self.red end end |
#topMostFieldInCircuit(circuit) ⇒ Object
289 290 291 292 293 294 295 296 297 |
# File 'lib/software_challenge_client/game_state.rb', line 289 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
219 220 221 |
# File 'lib/software_challenge_client/game_state.rb', line 219 def winner return condition.nil? ? nil : self.condition.winner end |
#winningReason ⇒ String
gets the winning reason
226 227 228 |
# File 'lib/software_challenge_client/game_state.rb', line 226 def winningReason return condition.nil? ? nil : self.condition.reason end |