fix NullPointerException error when using Terminal view, fix MVC model not respected to display winner

This commit is contained in:
Quentin Legot 2021-12-08 11:32:16 +01:00
parent 6e13a3124e
commit 74b8489854
7 changed files with 40 additions and 62 deletions

View File

@ -1,4 +1,4 @@
Projet de Conception Logiciel concu par : Projet de Conception Logiciel conçu par :
LUCAS Valentin LUCAS Valentin
LEGOT Quentin LEGOT Quentin

View File

@ -1,12 +1,10 @@
package fr.lnl.game.client; package fr.lnl.game.client;
import fr.lnl.game.client.listener.DisplayWinnerEvent;
import fr.lnl.game.client.view.AbstractView; import fr.lnl.game.client.view.AbstractView;
import fr.lnl.game.client.view.Terminal; import fr.lnl.game.client.view.Terminal;
import fr.lnl.game.client.view.ViewManager; import fr.lnl.game.client.view.ViewManager;
import fr.lnl.game.client.view.Window; import fr.lnl.game.client.view.Window;
import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.grid.build.GridFactoryBuilder; import fr.lnl.game.server.games.grid.build.GridFactoryBuilder;
import fr.lnl.game.server.games.grid.build.LockGridFactoryBuilder; import fr.lnl.game.server.games.grid.build.LockGridFactoryBuilder;
import fr.lnl.game.server.games.player.*; import fr.lnl.game.server.games.player.*;
@ -44,9 +42,8 @@ public class App extends Application {
public static void startGame(ViewLambda lambda) throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, public static void startGame(ViewLambda lambda) throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
InstantiationException, IllegalAccessException { InstantiationException, IllegalAccessException {
List<Player> players = parsePlayers(); List<Player> players = parsePlayers();
Grid grid = new Grid(12, 12, players);
GridFactoryBuilder builder = LockGridFactoryBuilder.create().energyProbability(0.95F).wallProbability(0.80F).playersList(players).gridDimensions(12, 12); GridFactoryBuilder builder = LockGridFactoryBuilder.create().energyProbability(0.95F).wallProbability(0.80F).playersList(players).gridDimensions(12, 12);
game = new Game(builder, players, new DisplayWinnerEvent()); game = new Game(builder, players);
for (Player player : game.getPlayers()) { for (Player player : game.getPlayers()) {
playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player))); playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player)));
} }
@ -61,6 +58,7 @@ public class App extends Application {
throw new CrashException(e.getMessage(), e); throw new CrashException(e.getMessage(), e);
} }
viewManager = new ViewManager(playerList, game, Window.class); viewManager = new ViewManager(playerList, game, Window.class);
viewManager.run();
} }
public static void launchTerminal() { public static void launchTerminal() {
@ -71,6 +69,7 @@ public class App extends Application {
throw new CrashException(e.getMessage(), e); throw new CrashException(e.getMessage(), e);
} }
viewManager = new ViewManager(playerList, game, Terminal.class); viewManager = new ViewManager(playerList, game, Terminal.class);
viewManager.run();
} }
public static List<Player> parsePlayers() throws IllegalArgumentException, NoSuchMethodException, public static List<Player> parsePlayers() throws IllegalArgumentException, NoSuchMethodException,

View File

@ -11,9 +11,11 @@ public class ButtonListener extends AbstractModelListening {
private final Game game; private final Game game;
private final DisplayWinnerEvent displayWinnerEvent;
public ButtonListener(Game game) { public ButtonListener(Game game) {
this.game = game; this.game = game;
this.displayWinnerEvent = new DisplayWinnerEvent();
} }
@Override @Override
@ -30,12 +32,16 @@ public class ButtonListener extends AbstractModelListening {
@Override @Override
public void updateModel(Object event) { public void updateModel(Object event) {
Player player = game.getCurrentPlayer(); Player player = game.getCurrentPlayer();
game.play(); boolean isOver = game.play();
/* Alert alert = new Alert(Alert.AlertType.INFORMATION); /* Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Un joueur ordinateur a joué"); alert.setTitle("Un joueur ordinateur a joué");
alert.setHeaderText("Le joueur ordinateur numéro" + player.getId() + " a joué"); alert.setHeaderText("Le joueur ordinateur numéro" + player.getId() + " a joué");
alert.setContentText("Il a joué l'action: " + game.getSelectedAction()); */ alert.setContentText("Il a joué l'action: " + game.getSelectedAction()); */
App.getViewManager().updateView(); App.getViewManager().updateView();
if(isOver) {
displayWinnerEvent.updateModel(game.getWinner());
System.exit(0);
}
//alert.showAndWait(); //alert.showAndWait();
} }
} }

View File

@ -1,6 +1,7 @@
package fr.lnl.game.client.listener; package fr.lnl.game.client.listener;
import fr.lnl.game.client.App; import fr.lnl.game.client.App;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.listener.AbstractModelListening; import fr.lnl.game.server.listener.AbstractModelListening;
public class DisplayWinnerEvent extends AbstractModelListening { public class DisplayWinnerEvent extends AbstractModelListening {
@ -8,6 +9,7 @@ public class DisplayWinnerEvent extends AbstractModelListening {
// TODO: 07/12/2021 PROBLEM -> ViewManager retourne null // TODO: 07/12/2021 PROBLEM -> ViewManager retourne null
@Override @Override
public void updateModel(Object obj) { public void updateModel(Object obj) {
App.getViewManager().displayWinner(App.getGame().getWinner()); Player winner = (Player) obj;
App.getViewManager().displayWinner(winner);
} }
} }

View File

@ -1,42 +1,45 @@
package fr.lnl.game.client.view; package fr.lnl.game.client.view;
import fr.lnl.game.client.ClientPlayer; import fr.lnl.game.client.ClientPlayer;
import fr.lnl.game.client.listener.DisplayWinnerEvent;
import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import java.util.HashMap; import java.util.HashMap;
public class ViewManager { public record ViewManager(
HashMap<Player, ClientPlayer> players,
Game game, Class<? extends View> viewType) {
private final HashMap<Player, ClientPlayer> players;
private final Game game;
public ViewManager(HashMap<Player, ClientPlayer> players, Game game, Class<? extends View> viewType) {
this.players = players;
this.game = game;
if(viewType == Terminal.class) {
terminalView();
} else {
updateView();
}
}
public void updateView() { public void updateView() {
players.get(game.getCurrentPlayer()).getView().show(); players.get(game.getCurrentPlayer()).getView().show();
} }
public void terminalView() { public void terminalView() {
DisplayWinnerEvent displayWinnerEvent = new DisplayWinnerEvent();
while (true) { while (true) {
Player player = game.getCurrentPlayer(); Player player = game.getCurrentPlayer();
players.get(game.getCurrentPlayer()).getView().show(); players.get(game.getCurrentPlayer()).getView().show();
game.play(); boolean isOver = game.play();
System.out.println("Le joueur ordinateur numéro " + player.getId() + " a joué"); System.out.println("Le joueur ordinateur numéro " + player.getId() + " a joué");
System.out.println("Il a joué l'action: " + game.getSelectedAction()); System.out.println("Il a joué l'action: " + game.getSelectedAction());
if (isOver) {
displayWinnerEvent.updateModel(game.getWinner());
System.exit(0);
}
} }
} }
public void displayWinner(Player winner) { public void displayWinner(Player winner) {
players.get(game.getCurrentPlayer()).getView().displayWinner(winner); players.get(game.getCurrentPlayer()).getView().displayWinner(winner);
} }
public void run() {
if (viewType == Terminal.class) {
terminalView();
} else {
updateView();
}
}
} }

View File

@ -6,13 +6,10 @@ import fr.lnl.game.server.games.grid.build.GridFactoryBuilder;
import fr.lnl.game.server.games.grid.elements.CountdownBox; import fr.lnl.game.server.games.grid.elements.CountdownBox;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.listener.AbstractModelListening; import fr.lnl.game.server.listener.AbstractModelListening;
import fr.lnl.game.server.listener.GameFinishEvent;
import fr.lnl.game.server.listener.ModelListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import static java.util.function.Predicate.not; import static java.util.function.Predicate.not;
@ -22,12 +19,10 @@ public class Game {
private final GridFactoryBuilder buildStrategy; private final GridFactoryBuilder buildStrategy;
private final Grid grid; private final Grid grid;
private final List<Player> players; private final List<Player> players;
private final ModelListener gameFinishEvent;
private final AbstractModelListening displayWinnerEvent;
private Player currentPlayer; private Player currentPlayer;
private Action selectedAction = null; private Action selectedAction = null;
public Game(GridFactoryBuilder buildStrategy, List<Player> players, AbstractModelListening displayWinnerEvent) throws IllegalArgumentException { public Game(GridFactoryBuilder buildStrategy, List<Player> players) throws IllegalArgumentException {
this.grid = buildStrategy.build(); this.grid = buildStrategy.build();
if(players.size() < 2) if(players.size() < 2)
throw new IllegalArgumentException("The game need 2 or more player to start"); throw new IllegalArgumentException("The game need 2 or more player to start");
@ -37,8 +32,6 @@ public class Game {
this.buildStrategy = buildStrategy; this.buildStrategy = buildStrategy;
this.players = players; this.players = players;
this.currentPlayer = players.get(0); this.currentPlayer = players.get(0);
this.gameFinishEvent = new GameFinishEvent(this);
this.displayWinnerEvent = displayWinnerEvent;
initGame(); initGame();
} }
@ -47,16 +40,18 @@ public class Game {
currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer)); currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer));
} }
public void play() { /**
*
* @return true if game is over, false otherwise
*/
public boolean play() {
selectedAction = currentPlayer.choseAction(); selectedAction = currentPlayer.choseAction();
selectedAction.doAction(); selectedAction.doAction();
countdownGridElementsUpdate(); countdownGridElementsUpdate();
gridPlayersUpdate(); gridPlayersUpdate();
nextCurrentPlayer(); nextCurrentPlayer();
currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer)); currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer));
if(isOver()) { return isOver();
gameFinishEvent.updateModel(null);
}
} }
private void gridPlayersUpdate(){ private void gridPlayersUpdate(){
@ -139,8 +134,4 @@ public class Game {
public Action getSelectedAction() { public Action getSelectedAction() {
return selectedAction; return selectedAction;
} }
public AbstractModelListening getDisplayWinnerEvent() {
return displayWinnerEvent;
}
} }

View File

@ -1,23 +0,0 @@
package fr.lnl.game.server.listener;
import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.utils.CrashException;
import java.util.concurrent.TimeUnit;
public class GameFinishEvent extends AbstractModelListening {
private final Object lock = new Object();
private final Game game;
public GameFinishEvent(Game game) {
this.game = game;
}
// TODO: 02/12/2021 méthode a revoir, notamment surement mettre un dialog sur la view Window et fermer dès que l'utilisateur a cliqué sur ok
@Override
public void updateModel(Object obj) {
game.getDisplayWinnerEvent().updateModel(null);
System.exit(0);
}
}