From 239782ec6068ce2eb7dd6f35d4c6eed84d37479d Mon Sep 17 00:00:00 2001 From: Nathan Date: Mon, 24 Jun 2024 10:36:49 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add:=20log=20for=20endgame?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewModel/Game/PlayingGameVM.swift | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/DouShouQi_App/DouShouQi_App/ViewModel/Game/PlayingGameVM.swift b/DouShouQi_App/DouShouQi_App/ViewModel/Game/PlayingGameVM.swift index 3cc6dcb..104303a 100644 --- a/DouShouQi_App/DouShouQi_App/ViewModel/Game/PlayingGameVM.swift +++ b/DouShouQi_App/DouShouQi_App/ViewModel/Game/PlayingGameVM.swift @@ -38,6 +38,21 @@ class PlayingGameVM: ObservableObject { return game.rules.getNextPlayer() == .player1 ? Colors.MeepleP1! : Colors.MeepleP2! } + public var winner: String { + guard self.game.isOver else { + return "" + } + + guard let lastMove: Move = self.game.rules.historic.last else { + return "" + } + + guard let winner: DouShouQiModel.Player = game.players[lastMove.owner] else { + return "" + } + return winner.name + } + // Inits init(withGame game: Game) { self.game = game @@ -57,6 +72,34 @@ class PlayingGameVM: ObservableObject { } + self.game.addGameOverListener { board, result, player in + switch(result){ + case .notFinished: + print("⏳ Game is not over yet!") + case .winner(winner: let o, reason: let r): + print(board) + print("**************************************") + print("Game Over!!!") + print("🥇🏆 and the winner is... \(o == .player1 ? "🟡" : "🔴") \(player?.name ?? "")!") + switch(r){ + case .denReached: + print("🪺 the opponent's den has been reached.") + case .noMorePieces: + print("🐭🐱🐯🦁🐘 all the opponent's animals have been eaten...") + case .noMovesLeft: + print("⛔️ the opponent can not move any piece!") + case .tooManyOccurences: + print("🔄 the opponent seem to like this situation... but enough is enough. Sorry...") + default: + print("Reason unknown...") + } + print("**************************************") + default: + break + } + + } + } catch { print("Error") return nil @@ -79,6 +122,7 @@ class PlayingGameVM: ObservableObject { self.objectWillChange.send() } else { sender.cellPosition = start + sender.cellPosition = start } }