From 4e505be8a80638fbb812f25d83fd5efb01812131 Mon Sep 17 00:00:00 2001 From: Alexis Drai Date: Thu, 9 Feb 2023 15:32:56 +0100 Subject: [PATCH] :rotating_light: :recycle: Clean up Game --- .../Sources/connect4_lib/games/Game.swift | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Connect4/connect4_lib/Sources/connect4_lib/games/Game.swift b/Connect4/connect4_lib/Sources/connect4_lib/games/Game.swift index ec722d0..b55ef30 100644 --- a/Connect4/connect4_lib/Sources/connect4_lib/games/Game.swift +++ b/Connect4/connect4_lib/Sources/connect4_lib/games/Game.swift @@ -31,14 +31,12 @@ public class Game { public var gameOverString: String { var string = "Game over" - switch(rules.isGameOver(byPlayer: getCurrentPlayerId(), - onGrid: board.grid).result) { - case .won(let playerId, let victoryTiles): + let gameOverResult = rules.isGameOver(byPlayer: getCurrentPlayerId(), + onGrid: board.grid).result + if case .won(let playerId, let victoryTiles) = gameOverResult { string.append("\nPlayer \(playerId) won!\n") string.append(board.displayVictory(fromTiles: victoryTiles)) - default: break; // nothing } - return string } @@ -47,7 +45,11 @@ public class Game { let currentPlayer = { if(rules.getNextPlayer(fromGrid: board.grid, withPlayer1Id: player1.id, - withPlayer2Id: player2.id) == player1.id) { return player1 } else { return player2 } + withPlayer2Id: player2.id) == player1.id) { + return player1 + } else { + return player2 + } }() if let chosenCol = currentPlayer.chooseColumn(inBoard: board, @@ -60,7 +62,10 @@ public class Game { private func getCurrentPlayerId() -> Int { if(rules.getNextPlayer(fromGrid: board.grid, withPlayer1Id: player1.id, - withPlayer2Id: player2.id) == player1.id) { return player2.id } else { return player1.id } + withPlayer2Id: player2.id) == player1.id) { + return player2.id + } else { + return player1.id + } } - }