diff --git a/client/src/main/java/fr/lnl/game/client/App.java b/client/src/main/java/fr/lnl/game/client/App.java index d798d90..574a030 100644 --- a/client/src/main/java/fr/lnl/game/client/App.java +++ b/client/src/main/java/fr/lnl/game/client/App.java @@ -1,14 +1,9 @@ package fr.lnl.game.client; - -import fr.lnl.game.client.listener.UpdateViewEvent; -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.client.view.*; +import fr.lnl.game.server.listener.GameFinishEvent; import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.player.*; -import fr.lnl.game.server.listener.GameFinishEvent; import fr.lnl.game.server.utils.CrashException; import fr.lnl.game.server.utils.Point; import javafx.application.Application; @@ -43,7 +38,7 @@ public class App extends Application { public static void startGame(ViewLambda lambda) throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { List players = parsePlayers(); - game = new Game(new Grid(12, 12, players), players, new UpdateViewEvent(), new GameFinishEvent()); + game = new Game(new Grid(12, 12, players), players, new GameFinishEvent()); for (Player player : game.getPlayers()) { playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player))); } @@ -58,7 +53,6 @@ public class App extends Application { throw new CrashException(e.getMessage(), e); } viewManager = new ViewManager(playerList, game); - new Thread(() -> game.play()).start(); } public static void launchTerminal() { @@ -69,7 +63,6 @@ public class App extends Application { throw new CrashException(e.getMessage(), e); } viewManager = new ViewManager(playerList, game); - new Thread(() -> game.play()).start(); } public static List parsePlayers() throws IllegalArgumentException, NoSuchMethodException, diff --git a/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java b/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java index bfcf322..4d8e7f4 100644 --- a/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java +++ b/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java @@ -1,12 +1,21 @@ package fr.lnl.game.client.listener; import fr.lnl.game.client.App; +import fr.lnl.game.server.games.Game; import fr.lnl.game.server.listener.AbstractModelListening; import fr.lnl.game.server.listener.ModelListener; +import javafx.scene.control.Alert; +import javafx.scene.control.Dialog; public class ButtonListener extends AbstractModelListening { + private final Game game; + + public ButtonListener(Game game) { + this.game = game; + } + @Override public void addListener(ModelListener e) { this.listeners.add(e); @@ -20,6 +29,12 @@ public class ButtonListener extends AbstractModelListening { @Override public void updateModel(Object event) { - App.viewManager.setNeedUpdate(true); + game.play(); + App.viewManager.updateView(); + Alert alert = new Alert(Alert.AlertType.INFORMATION); + alert.setTitle("Un joueur ordinateur a joué"); + alert.setHeaderText("Le joueur ordinateur numéro" + game.getCurrentPlayer().getId() + " a joué"); + alert.setContentText("Il a joué l'action: " + game.getSelectedAction()); + alert.showAndWait(); } } diff --git a/client/src/main/java/fr/lnl/game/client/listener/UpdateViewEvent.java b/client/src/main/java/fr/lnl/game/client/listener/UpdateViewEvent.java deleted file mode 100644 index ac6b91e..0000000 --- a/client/src/main/java/fr/lnl/game/client/listener/UpdateViewEvent.java +++ /dev/null @@ -1,12 +0,0 @@ -package fr.lnl.game.client.listener; - -import fr.lnl.game.client.App; -import fr.lnl.game.server.listener.AbstractModelListening; - -public class UpdateViewEvent extends AbstractModelListening { - - @Override - public void updateModel(Object obj) { - App.viewManager.setNeedUpdate(true); - } -} diff --git a/client/src/main/java/fr/lnl/game/client/view/Cell.java b/client/src/main/java/fr/lnl/game/client/view/Cell.java index 945b58d..2874278 100644 --- a/client/src/main/java/fr/lnl/game/client/view/Cell.java +++ b/client/src/main/java/fr/lnl/game/client/view/Cell.java @@ -4,6 +4,7 @@ import fr.lnl.game.server.games.grid.Bomb; import fr.lnl.game.server.games.grid.EnergyBall; import fr.lnl.game.server.games.grid.Mine; import fr.lnl.game.server.games.player.Player; +import fr.lnl.game.server.utils.Cardinal; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.StackPane; @@ -36,17 +37,34 @@ public class Cell extends Rectangle { if(object instanceof Player){ image = new Image("file:resources/images/player.png"); } - if(object instanceof EnergyBall){ + else if(object instanceof EnergyBall){ image = new Image("file:resources/images/energyBall.png"); } - if(object instanceof Bomb){ + else if(object instanceof Bomb){ image = new Image("file:resources/images/bomb.jpg"); } - if(object instanceof Mine){ + else if(object instanceof Mine){ image = new Image("file:resources/images/mine.webp"); } else{ - image = new Image("file:resources/images/wall.jpg"); + //test + if(((Wall)object).getCardinal()== Cardinal.NORTH){ + image = new Image("file:resources/topWall.png"); + }else if(((Wall)object).getCardinal()== Cardinal.SOUTH) { + image = new Image("file:resources/bottomWall.png"); + }else if(((Wall)object).getCardinal()== Cardinal.EAST) { + image = new Image("file:resources/rightWall.png"); + }else if(((Wall)object).getCardinal()== Cardinal.WEST) { + image = new Image("file:resources/leftWall.png"); + }else if(((Wall)object).getCardinal()== Cardinal.NORTH_EAST) { + image = new Image("file:resources/topRightWall.png"); + }else if(((Wall)object).getCardinal()== Cardinal.NORTH_WEST) { + image = new Image("file:resources/topLeftWall.png"); + }else if(((Wall)object).getCardinal()== Cardinal.SOUTH_EAST) { + image = new Image("file:resources/bottomRightWall.png"); + }else{ + image = new Image("file:resources/bottomLeftWall.png"); + } } ImageView iv = new ImageView(image); diff --git a/client/src/main/java/fr/lnl/game/client/view/GUI.java b/client/src/main/java/fr/lnl/game/client/view/GUI.java index 46ce600..2a33bb6 100644 --- a/client/src/main/java/fr/lnl/game/client/view/GUI.java +++ b/client/src/main/java/fr/lnl/game/client/view/GUI.java @@ -49,9 +49,8 @@ public class GUI { private Parent createContent() { Pane principalPane = new Pane(); principalPane.setPrefSize(width * cellSize, height * cellSize); - //PARTIE1 - //à définir avec n pour moduler la taille du plateau + //à définir avec n pour moduler la taille du plateau for (int i = 0; i < 16; i++) { for (int j = 0; j < 16; j++) { Cell cell = new Cell(i, j); @@ -59,7 +58,6 @@ public class GUI { } } - //PARTIE2 board = grid.getBoard(); for (int i = 0; i < 16; i++) { for (int j = 0; j < 16; j++) { @@ -68,11 +66,20 @@ public class GUI { addToPrincipalPanel(value.getA(), principalPane, i, j); } if (value.getB() instanceof Wall || value.getB() instanceof EnergyBall || value.getB() instanceof Mine || value.getB() instanceof Bomb) { + System.out.println(value.getB()); addToPrincipalPanel(value.getB(), principalPane, i, j); } } } - //PARTIE3 + + Rectangle shape = new Rectangle(); + shape.setX(700); + shape.setY(20); + shape.setWidth(200); + shape.setHeight(600); + shape.setFill(javafx.scene.paint.Color.WHITE); + + Button followingButton = new Button("SUIVANT"); followingButton.setLayoutX(775); followingButton.setLayoutY(600); @@ -81,6 +88,7 @@ public class GUI { //add un eventListener au button principalPane.getChildren().add(followingButton); + //pas compris le principe return principalPane; } diff --git a/client/src/main/java/fr/lnl/game/client/view/TimerService.java b/client/src/main/java/fr/lnl/game/client/view/TimerService.java deleted file mode 100644 index 936bb35..0000000 --- a/client/src/main/java/fr/lnl/game/client/view/TimerService.java +++ /dev/null @@ -1,19 +0,0 @@ -package fr.lnl.game.client.view; - -import fr.lnl.game.client.App; -import javafx.concurrent.ScheduledService; -import javafx.concurrent.Task; - -public class TimerService extends ScheduledService { - @Override - protected Task createTask() { - return new Task<>() { - protected Integer call() { - if(App.viewManager.getNeedUpdate()) { - App.viewManager.updateView(); - } - return 0; - } - }; - } -} diff --git a/client/src/main/java/fr/lnl/game/client/view/ViewManager.java b/client/src/main/java/fr/lnl/game/client/view/ViewManager.java index 50d11df..c71bfd3 100644 --- a/client/src/main/java/fr/lnl/game/client/view/ViewManager.java +++ b/client/src/main/java/fr/lnl/game/client/view/ViewManager.java @@ -12,27 +12,14 @@ public class ViewManager { private final HashMap players; private final Game game; - private Boolean needUpdate = true; public ViewManager(HashMap players, Game game) { this.players = players; this.game = game; - TimerService timer = new TimerService(); - timer.setPeriod(Duration.millis(30)); updateView(); - timer.start(); } public void updateView() { players.get(game.getCurrentPlayer()).getView().show(); - setNeedUpdate(false); - } - - public Boolean getNeedUpdate() { - return needUpdate; - } - - public void setNeedUpdate(Boolean needUpdate) { - this.needUpdate = needUpdate; } } diff --git a/client/src/main/java/fr/lnl/game/client/view/Window.java b/client/src/main/java/fr/lnl/game/client/view/Window.java index 58a6128..65b7a05 100644 --- a/client/src/main/java/fr/lnl/game/client/view/Window.java +++ b/client/src/main/java/fr/lnl/game/client/view/Window.java @@ -22,9 +22,10 @@ public class Window extends AbstractView { String javaVersion = System.getProperty("java.version"); String javafxVersion = System.getProperty("javafx.version"); Label l = new Label("Hello, JavaFX version " + javafxVersion + ", running on Java " + javaVersion + "."); - Label l2 = new Label("I'm " + player.getClass().getSimpleName() + " num " + player.getId() + " my class is " + player.getClassPlayer().toString()); - Button b = new Button("Click to see others players informations"); - b.setOnAction(new ClientEventHandler(new ButtonListener())); + Label l2 = new Label("I'm " + player.getClass().getSimpleName() + " num " + player.getId() + "" + + " my class is " + player.getClassPlayer().toString() + ", my remaining energy is " + player.getEnergy()); + Button b = new Button("Click to continue the game"); + b.setOnAction(new ClientEventHandler(new ButtonListener(game))); GridPane grid = new GridPane(); grid.add(l, 0, 0); grid.add(l2, 0, 1); diff --git a/client/src/main/resources/images/bottomLeftWall.PNG b/client/src/main/resources/images/bottomLeftWall.PNG new file mode 100644 index 0000000..af2c630 Binary files /dev/null and b/client/src/main/resources/images/bottomLeftWall.PNG differ diff --git a/client/src/main/resources/images/bottomRightWall.PNG b/client/src/main/resources/images/bottomRightWall.PNG new file mode 100644 index 0000000..3e87801 Binary files /dev/null and b/client/src/main/resources/images/bottomRightWall.PNG differ diff --git a/client/src/main/resources/images/bottomWall.PNG b/client/src/main/resources/images/bottomWall.PNG new file mode 100644 index 0000000..3287196 Binary files /dev/null and b/client/src/main/resources/images/bottomWall.PNG differ diff --git a/client/src/main/resources/images/rightWall.PNG b/client/src/main/resources/images/rightWall.PNG new file mode 100644 index 0000000..f2f5278 Binary files /dev/null and b/client/src/main/resources/images/rightWall.PNG differ diff --git a/client/src/main/resources/images/topLeftWall.PNG b/client/src/main/resources/images/topLeftWall.PNG new file mode 100644 index 0000000..73a2dd5 Binary files /dev/null and b/client/src/main/resources/images/topLeftWall.PNG differ diff --git a/client/src/main/resources/images/topRightWall.PNG b/client/src/main/resources/images/topRightWall.PNG new file mode 100644 index 0000000..74ff2dc Binary files /dev/null and b/client/src/main/resources/images/topRightWall.PNG differ diff --git a/client/src/main/resources/images/topWall.PNG b/client/src/main/resources/images/topWall.PNG new file mode 100644 index 0000000..c769e82 Binary files /dev/null and b/client/src/main/resources/images/topWall.PNG differ 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 7c16988..0966e38 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 @@ -5,92 +5,76 @@ import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.player.ComputerPlayer; import fr.lnl.game.server.games.player.HumanPlayer; import fr.lnl.game.server.games.player.Player; -import fr.lnl.game.server.listener.AwakeGame; import fr.lnl.game.server.listener.ModelListener; import fr.lnl.game.server.utils.CrashException; +import fr.lnl.game.server.utils.Point; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.concurrent.TimeUnit; import java.util.stream.Stream; public class Game { private final Grid grid; private final List players; - private final ModelListener viewUpdateEvent; private final ModelListener gameFinishEvent; private Player currentPlayer; - private InterfaceAction interfaceAction; private Action selectedAction = null; - ModelListener awakeEvent; - private final Object lock = new Object(); - public Game(Grid grid, List players, ModelListener viewUpdate, ModelListener gameFinishEvent) throws IllegalArgumentException { - if(players.size() < 2 || grid.getNumberNeutralBox() < players.size()) + public Game(Grid grid, List players, ModelListener gameFinishEvent) throws IllegalArgumentException { + if(players.size() < 2) throw new IllegalArgumentException("The game need 2 or more player to start"); this.players = players; this.currentPlayer = players.get(0); this.grid = grid; - this.viewUpdateEvent = viewUpdate; this.gameFinishEvent = gameFinishEvent; - grid.initPlacePlayers(); + placePlayersBRUT(); + currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer)); } /** * @deprecated utiliser pour le moment, nécessite une meilleure implémentation pour savoir ou placé les joueurs */ + @Deprecated + public void placePlayersBRUT(){ + grid.getBoard().get(new Point(7,7)).setA(grid.getPlayers().get(0)); + grid.getPlayers().get(0).setPosition(new Point(7, 7)); + grid.getBoard().get(new Point(7,8)).setA(grid.getPlayers().get(1)); + grid.getPlayers().get(1).setPosition(new Point(7, 8)); + } public void play() { - while(!isOver()) { - awakeEvent = new AwakeGame(this); - currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer)); - if(currentPlayer instanceof HumanPlayer) { - interfaceAction = InterfaceAction.SELECT_ACTION; - waitForInterfaceEvent(); - selectedAction.doAction(); - } else { - ComputerPlayer player = (ComputerPlayer) currentPlayer; - Action action = player.choseAction(); - action.doAction(); - waitNSeconds(2); - } - selectedAction = null; - nextCurrentPlayer(); - viewUpdateEvent.updateModel(null); + if (currentPlayer instanceof ComputerPlayer player) { + selectedAction = player.choseAction(); } - gameFinishEvent.updateModel(null); + selectedAction.doAction(); + nextCurrentPlayer(); + currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer)); + if(isOver()) { + gameFinishEvent.updateModel(null); + } + } private void waitForInterfaceEvent() { - synchronized (lock){ - try { - wait(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - } - - private void waitNSeconds(int n) { synchronized (this){ try { - wait(TimeUnit.SECONDS.toMillis(n)); + wait(); } catch (InterruptedException e) { throw new CrashException(e.getMessage(), e); } } + } public void resumeThread() { - synchronized (lock) { - lock.notifyAll(); + synchronized (this) { + notifyAll(); } } - protected List generateAndGetPlayerActions(Player player) { + public List generateAndGetPlayerActions(Player player) { List actions = new ArrayList<>(); for(Direction direction : Direction.values()) { try { @@ -155,7 +139,7 @@ public class Game { return players; } - public InterfaceAction getInterfaceAction() { - return interfaceAction; + public Action getSelectedAction() { + return selectedAction; } } \ No newline at end of file diff --git a/server/src/main/java/fr/lnl/game/server/games/InterfaceAction.java b/server/src/main/java/fr/lnl/game/server/games/InterfaceAction.java index 01591f1..2322f1f 100644 --- a/server/src/main/java/fr/lnl/game/server/games/InterfaceAction.java +++ b/server/src/main/java/fr/lnl/game/server/games/InterfaceAction.java @@ -2,6 +2,7 @@ package fr.lnl.game.server.games; public enum InterfaceAction { - SELECT_ACTION + SELECT_ACTION, + DIALOG_TO_CONTINUE_COMPUTER_IA } diff --git a/server/src/main/java/fr/lnl/game/server/listener/AwakeGame.java b/server/src/main/java/fr/lnl/game/server/listener/AwakeGame.java deleted file mode 100644 index c5bc122..0000000 --- a/server/src/main/java/fr/lnl/game/server/listener/AwakeGame.java +++ /dev/null @@ -1,17 +0,0 @@ -package fr.lnl.game.server.listener; - -import fr.lnl.game.server.games.Game; - -public class AwakeGame extends AbstractModelListening { - - private final Game game; - - public AwakeGame(Game game) { - this.game = game; - } - - @Override - public void updateModel(Object obj) { - game.resumeThread(); - } -} diff --git a/server/src/test/java/fr/lnl/game/server/Mock.java b/server/src/test/java/fr/lnl/game/server/Mock.java index 0279164..89aa984 100644 --- a/server/src/test/java/fr/lnl/game/server/Mock.java +++ b/server/src/test/java/fr/lnl/game/server/Mock.java @@ -1,7 +1,6 @@ package fr.lnl.game.server; import fr.lnl.game.server.games.Game; -import fr.lnl.game.server.games.MockViewUpdate; import fr.lnl.game.server.games.grid.EnergyBall; import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.Wall; @@ -23,8 +22,11 @@ public class Mock { public Mock() { List players = Arrays.asList(new RandomComputerPlayer(1,null, ClassPlayer.DEFAULT), new RandomComputerPlayer(2,null, ClassPlayer.DEFAULT)); - this.grid = new Grid(16,16, players, 0.80F,0.95F); - game = new Game(grid, players, new MockViewUpdate(), new GameFinishEvent()); + this.grid = new Grid(16,16, players); + placePlayersBRUT(); + placeEnergyBallBRUT(); + placeInternWallBRUT(); + game = new Game(grid, players, new GameFinishEvent()); } public void placePlayersBRUT(){ diff --git a/server/src/test/java/fr/lnl/game/server/games/MockViewUpdate.java b/server/src/test/java/fr/lnl/game/server/games/MockViewUpdate.java deleted file mode 100644 index 05ec33f..0000000 --- a/server/src/test/java/fr/lnl/game/server/games/MockViewUpdate.java +++ /dev/null @@ -1,10 +0,0 @@ -package fr.lnl.game.server.games; - -import fr.lnl.game.server.listener.AbstractModelListening; - -public class MockViewUpdate extends AbstractModelListening { - @Override - public void updateModel(Object obj) { - // no op - } -}