From 360a84c40321ce2f6e5a144a3794436db59399bf Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Thu, 18 Nov 2021 11:06:13 +0100 Subject: [PATCH] Fix ArrayOutOfBoundException when we didn't have a winner --- server/src/main/java/fr/lnl/game/server/games/Game.java | 2 +- server/src/test/java/fr/lnl/game/server/GridTest.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/fr/lnl/game/server/games/Game.java b/server/src/main/java/fr/lnl/game/server/games/Game.java index 1272a6a..fdd9f35 100644 --- a/server/src/main/java/fr/lnl/game/server/games/Game.java +++ b/server/src/main/java/fr/lnl/game/server/games/Game.java @@ -29,7 +29,7 @@ public class Game { public Player getWinner() { // On part du principe que isOver est forcément appelé avant d'appeler getWinner - return players.parallelStream().filter(player -> !player.isAlive()).findFirst().orElseThrow(ArrayIndexOutOfBoundsException::new); + return players.parallelStream().filter(player -> !player.isAlive()).findFirst().orElse(null); } public Player getCurrentPlayer() { diff --git a/server/src/test/java/fr/lnl/game/server/GridTest.java b/server/src/test/java/fr/lnl/game/server/GridTest.java index 9519ca9..97665ba 100644 --- a/server/src/test/java/fr/lnl/game/server/GridTest.java +++ b/server/src/test/java/fr/lnl/game/server/GridTest.java @@ -82,7 +82,8 @@ public class GridTest { game.nextCurrentPlayer(); } System.out.println(game.getGrid().toString()); - System.out.println("Le joueur gagnant : " + game.getWinner().getId()); + Player winner = game.getWinner(); + System.out.println(winner != null ? ("Le joueur gagnant : " + winner.getId()) : ("Partie nulle, aucun gagnant")); } }