fix NullPointerException error when using Terminal view, fix MVC model not respected to display winner
This commit is contained in:
parent
6e13a3124e
commit
74b8489854
@ -1,4 +1,4 @@
|
||||
Projet de Conception Logiciel concu par :
|
||||
Projet de Conception Logiciel conçu par :
|
||||
|
||||
LUCAS Valentin
|
||||
LEGOT Quentin
|
||||
|
@ -1,12 +1,10 @@
|
||||
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.Terminal;
|
||||
import fr.lnl.game.client.view.ViewManager;
|
||||
import fr.lnl.game.client.view.Window;
|
||||
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.LockGridFactoryBuilder;
|
||||
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,
|
||||
InstantiationException, IllegalAccessException {
|
||||
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);
|
||||
game = new Game(builder, players, new DisplayWinnerEvent());
|
||||
game = new Game(builder, players);
|
||||
for (Player player : game.getPlayers()) {
|
||||
playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player)));
|
||||
}
|
||||
@ -61,6 +58,7 @@ public class App extends Application {
|
||||
throw new CrashException(e.getMessage(), e);
|
||||
}
|
||||
viewManager = new ViewManager(playerList, game, Window.class);
|
||||
viewManager.run();
|
||||
}
|
||||
|
||||
public static void launchTerminal() {
|
||||
@ -71,6 +69,7 @@ public class App extends Application {
|
||||
throw new CrashException(e.getMessage(), e);
|
||||
}
|
||||
viewManager = new ViewManager(playerList, game, Terminal.class);
|
||||
viewManager.run();
|
||||
}
|
||||
|
||||
public static List<Player> parsePlayers() throws IllegalArgumentException, NoSuchMethodException,
|
||||
|
@ -11,9 +11,11 @@ public class ButtonListener extends AbstractModelListening {
|
||||
|
||||
|
||||
private final Game game;
|
||||
private final DisplayWinnerEvent displayWinnerEvent;
|
||||
|
||||
public ButtonListener(Game game) {
|
||||
this.game = game;
|
||||
this.displayWinnerEvent = new DisplayWinnerEvent();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -30,12 +32,16 @@ public class ButtonListener extends AbstractModelListening {
|
||||
@Override
|
||||
public void updateModel(Object event) {
|
||||
Player player = game.getCurrentPlayer();
|
||||
game.play();
|
||||
boolean isOver = game.play();
|
||||
/* Alert alert = new Alert(Alert.AlertType.INFORMATION);
|
||||
alert.setTitle("Un joueur ordinateur a joué");
|
||||
alert.setHeaderText("Le joueur ordinateur numéro" + player.getId() + " a joué");
|
||||
alert.setContentText("Il a joué l'action: " + game.getSelectedAction()); */
|
||||
App.getViewManager().updateView();
|
||||
if(isOver) {
|
||||
displayWinnerEvent.updateModel(game.getWinner());
|
||||
System.exit(0);
|
||||
}
|
||||
//alert.showAndWait();
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package fr.lnl.game.client.listener;
|
||||
|
||||
import fr.lnl.game.client.App;
|
||||
import fr.lnl.game.server.games.player.Player;
|
||||
import fr.lnl.game.server.listener.AbstractModelListening;
|
||||
|
||||
public class DisplayWinnerEvent extends AbstractModelListening {
|
||||
@ -8,6 +9,7 @@ public class DisplayWinnerEvent extends AbstractModelListening {
|
||||
// TODO: 07/12/2021 PROBLEM -> ViewManager retourne null
|
||||
@Override
|
||||
public void updateModel(Object obj) {
|
||||
App.getViewManager().displayWinner(App.getGame().getWinner());
|
||||
Player winner = (Player) obj;
|
||||
App.getViewManager().displayWinner(winner);
|
||||
}
|
||||
}
|
||||
|
@ -1,42 +1,45 @@
|
||||
package fr.lnl.game.client.view;
|
||||
|
||||
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.player.Player;
|
||||
|
||||
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() {
|
||||
players.get(game.getCurrentPlayer()).getView().show();
|
||||
}
|
||||
|
||||
public void terminalView() {
|
||||
while(true) {
|
||||
DisplayWinnerEvent displayWinnerEvent = new DisplayWinnerEvent();
|
||||
while (true) {
|
||||
Player player = game.getCurrentPlayer();
|
||||
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("Il a joué l'action: " + game.getSelectedAction());
|
||||
if (isOver) {
|
||||
displayWinnerEvent.updateModel(game.getWinner());
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void displayWinner(Player winner) {
|
||||
players.get(game.getCurrentPlayer()).getView().displayWinner(winner);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
if (viewType == Terminal.class) {
|
||||
terminalView();
|
||||
} else {
|
||||
updateView();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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.player.Player;
|
||||
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.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.function.Predicate.not;
|
||||
@ -22,12 +19,10 @@ public class Game {
|
||||
private final GridFactoryBuilder buildStrategy;
|
||||
private final Grid grid;
|
||||
private final List<Player> players;
|
||||
private final ModelListener gameFinishEvent;
|
||||
private final AbstractModelListening displayWinnerEvent;
|
||||
private Player currentPlayer;
|
||||
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();
|
||||
if(players.size() < 2)
|
||||
throw new IllegalArgumentException("The game need 2 or more player to start");
|
||||
@ -37,8 +32,6 @@ public class Game {
|
||||
this.buildStrategy = buildStrategy;
|
||||
this.players = players;
|
||||
this.currentPlayer = players.get(0);
|
||||
this.gameFinishEvent = new GameFinishEvent(this);
|
||||
this.displayWinnerEvent = displayWinnerEvent;
|
||||
initGame();
|
||||
}
|
||||
|
||||
@ -47,16 +40,18 @@ public class Game {
|
||||
currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer));
|
||||
}
|
||||
|
||||
public void play() {
|
||||
/**
|
||||
*
|
||||
* @return true if game is over, false otherwise
|
||||
*/
|
||||
public boolean play() {
|
||||
selectedAction = currentPlayer.choseAction();
|
||||
selectedAction.doAction();
|
||||
countdownGridElementsUpdate();
|
||||
gridPlayersUpdate();
|
||||
nextCurrentPlayer();
|
||||
currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer));
|
||||
if(isOver()) {
|
||||
gameFinishEvent.updateModel(null);
|
||||
}
|
||||
return isOver();
|
||||
}
|
||||
|
||||
private void gridPlayersUpdate(){
|
||||
@ -139,8 +134,4 @@ public class Game {
|
||||
public Action getSelectedAction() {
|
||||
return selectedAction;
|
||||
}
|
||||
|
||||
public AbstractModelListening getDisplayWinnerEvent() {
|
||||
return displayWinnerEvent;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user