add dialog when game finish (#1)

This commit is contained in:
Quentin Legot 2021-12-07 16:02:20 +01:00 committed by GitHub
parent db2e2e1f7a
commit c07266a763
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 83 additions and 21 deletions

View File

@ -1,5 +1,6 @@
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;
@ -22,8 +23,8 @@ public class App extends Application {
private static LinkedList<String> argsList;
public static HashMap<Player, ClientPlayer> playerList = new HashMap<>();
public static Game game;
public static ViewManager viewManager;
private static Game game;
private static ViewManager viewManager;
public static void main(String[] args) {
argsList = new LinkedList<>(Arrays.asList(args));
@ -46,7 +47,7 @@ public class App extends Application {
List<Player> players = parsePlayers();
Grid grid = new Grid(12, 12, players);
BuildStrategy buildStrategy = new LockStrategy(grid, 0.80F, 0.95F);
game = new Game(buildStrategy, players, new GameFinishEvent());
game = new Game(buildStrategy, players, new DisplayWinnerEvent());
for (Player player : game.getPlayers()) {
playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player)));
}
@ -135,4 +136,12 @@ public class App extends Application {
}
return clazz;
}
public static ViewManager getViewManager() {
return viewManager;
}
public static Game getGame() {
return game;
}
}

View File

@ -31,11 +31,10 @@ public class ButtonListener extends AbstractModelListening {
public void updateModel(Object event) {
Player player = game.getCurrentPlayer();
game.play();
/*
Alert alert = new Alert(Alert.AlertType.INFORMATION);
/* 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());*/
alert.setContentText("Il a joué l'action: " + game.getSelectedAction()); */
App.viewManager.updateView();
//alert.showAndWait();
}

View File

@ -0,0 +1,12 @@
package fr.lnl.game.client.listener;
import fr.lnl.game.client.App;
import fr.lnl.game.server.listener.AbstractModelListening;
public class DisplayWinnerEvent extends AbstractModelListening {
@Override
public void updateModel(Object obj) {
App.getViewManager().displayWinner(App.getGame().getWinner());
}
}

View File

@ -10,8 +10,12 @@ public class Terminal extends AbstractView {
}
public void show() {
// TODO: 26/10/2021
System.out.println(game.getGrid().toString());
}
@Override
public void displayWinner(Player winner) {
System.out.println("Le joueur " + winner + " a gagné la partie");
}
}

View File

@ -1,6 +1,10 @@
package fr.lnl.game.client.view;
import fr.lnl.game.server.games.player.Player;
public interface View {
void show();
void displayWinner(Player winner);
}

View File

@ -35,4 +35,8 @@ public class ViewManager {
System.out.println("Il a joué l'action: " + game.getSelectedAction());
}
}
public void displayWinner(Player winner) {
players.get(game.getCurrentPlayer()).getView().displayWinner(winner);
}
}

View File

@ -1,5 +1,6 @@
package fr.lnl.game.client.view;
import fr.lnl.game.client.App;
import fr.lnl.game.client.listener.ButtonListener;
import fr.lnl.game.client.listener.ClientEventHandler;
import fr.lnl.game.server.games.Game;
@ -10,6 +11,7 @@ import fr.lnl.game.server.utils.Pair;
import fr.lnl.game.server.utils.Point;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
@ -42,6 +44,16 @@ public class Window extends AbstractView {
stage.show();
}
@Override
public void displayWinner(Player winner) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Fin du jeu");
alert.setHeaderText("La partie est terminé");
alert.setContentText("Un joueur " + winner + " a gagné");
App.getViewManager().updateView();
alert.showAndWait();
}
private Parent createContent() {
Pane principalPane = new Pane();
principalPane.setPrefSize(width * cellSize, height * cellSize); // TODO: 04/12/2021 A corriger -> doit plutôt s'adapter à la taille de la grid (grid.getRow() et grid.getColumn())
@ -55,12 +67,12 @@ public class Window extends AbstractView {
for (int i = 0; i < grid.getRow(); i++) {
for (int j = 0; j < grid.getColumn(); j++) {
Pair<Player, Box> value = grid.getBoard().get(new Point(i, j));
if (value.getB() instanceof Wall || value.getB() instanceof EnergyBall || value.getB() instanceof Explosive) {
addToPrincipalPanel(value.getB(), principalPane, i, j);
}
if (value.getA() != null) {
addToPrincipalPanel(value.getA(), principalPane, i, j);
}
if (value.getB() instanceof Wall || value.getB() instanceof EnergyBall || value.getB() instanceof Mine || value.getB() instanceof Bomb) {
addToPrincipalPanel(value.getB(), principalPane, i, j);
}
}
}
putStatePlayerPane(principalPane);

View File

@ -6,6 +6,8 @@ import fr.lnl.game.server.games.grid.build.BuildStrategy;
import fr.lnl.game.server.games.grid.elements.CountdownBox;
import fr.lnl.game.server.games.player.ComputerPlayer;
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;
@ -19,10 +21,11 @@ public class Game {
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(BuildStrategy buildStrategy, List<Player> players, ModelListener gameFinishEvent) throws IllegalArgumentException {
public Game(BuildStrategy buildStrategy, List<Player> players, AbstractModelListening displayWinnerEvent) throws IllegalArgumentException {
this.grid = buildStrategy.getGrid();
if(players.size() < 2)
throw new IllegalArgumentException("The game need 2 or more player to start");
@ -32,7 +35,8 @@ public class Game {
this.buildStrategy = buildStrategy;
this.players = players;
this.currentPlayer = players.get(0);
this.gameFinishEvent = gameFinishEvent;
this.gameFinishEvent = new GameFinishEvent(this);
this.displayWinnerEvent = displayWinnerEvent;
initGame();
}
@ -122,4 +126,8 @@ public class Game {
public Action getSelectedAction() {
return selectedAction;
}
public AbstractModelListening getDisplayWinnerEvent() {
return displayWinnerEvent;
}
}

View File

@ -1,5 +1,6 @@
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;
@ -7,17 +8,16 @@ 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) {
synchronized (lock) {
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
} catch (InterruptedException e) {
throw new CrashException(e.getMessage(), e);
}
}
game.getDisplayWinnerEvent().updateModel(null);
System.exit(0);
}
}

View File

@ -1,6 +1,6 @@
package fr.lnl.game.server.listener;
public interface ModelListener {
//à peut-être revoir
void updateModel(Object obj);
}

View File

@ -23,7 +23,7 @@ public class Mock {
public Mock(List<Player> players) {
this.buildStrategy = new LockStrategy(new Grid(16,16, players),0.80F, 0.95F);
game = new Game(buildStrategy, players, new GameFinishEvent());
game = new Game(buildStrategy, players, new MockDisplayWinner());
this.grid = buildStrategy.getGrid();
}

View File

@ -0,0 +1,10 @@
package fr.lnl.game.server;
import fr.lnl.game.server.listener.AbstractModelListening;
public class MockDisplayWinner extends AbstractModelListening {
@Override
public void updateModel(Object obj) {
}
}