From de8c258322c00d3ea5afdc95878ac929d322c736 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Mon, 29 Nov 2021 19:22:43 +0100 Subject: [PATCH] Fix game not launching + add supprot to Computer Players (AI) + fix view not updating at each Game#play 's loop --- client/build.gradle | 4 ++ .../src/main/java/fr/lnl/game/client/App.java | 9 ++-- .../game/client/listener/UpdateViewEvent.java | 12 ++++++ server/build.gradle | 4 ++ .../java/fr/lnl/game/server/games/Game.java | 43 ++++++++++++++++--- .../server/games/player/ComputerPlayer.java | 10 +++++ .../games/player/RandomComputerPlayer.java | 4 +- .../games/player/StrategyComputerPlayer.java | 4 +- .../game/server/listener/GameFinishEvent.java | 20 +++++++++ .../test/java/fr/lnl/game/server/Mock.java | 4 +- .../lnl/game/server/games/MockViewUpdate.java | 10 +++++ 11 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 client/src/main/java/fr/lnl/game/client/listener/UpdateViewEvent.java create mode 100644 server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java create mode 100644 server/src/main/java/fr/lnl/game/server/listener/GameFinishEvent.java create mode 100644 server/src/test/java/fr/lnl/game/server/games/MockViewUpdate.java diff --git a/client/build.gradle b/client/build.gradle index bc48cb6..41b271d 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -21,6 +21,10 @@ javafx { modules = [ 'javafx.controls', 'javafx.graphics' ] } +compileJava { + options.encoding = 'UTF-8' +} + application { // Define the main class for the application. mainClassName = 'fr.lnl.game.client.App' 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 cc622a0..f01c2fb 100644 --- a/client/src/main/java/fr/lnl/game/client/App.java +++ b/client/src/main/java/fr/lnl/game/client/App.java @@ -1,4 +1,6 @@ package fr.lnl.game.client; +import fr.lnl.game.server.listener.GameFinishEvent; +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.Window; @@ -38,7 +40,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); + game = new Game(new Grid(12, 12, players), players, new UpdateViewEvent(), new GameFinishEvent()); for (Player player : game.getPlayers()) { playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player))); } @@ -56,7 +58,7 @@ public class App extends Application { | IllegalAccessException e) { throw new CrashException(e.getMessage(), e); } - new Thread(() -> game.play()); + new Thread(() -> game.play()).start(); updateView(); } @@ -67,8 +69,7 @@ public class App extends Application { | IllegalAccessException e) { throw new CrashException(e.getMessage(), e); } - new Thread(() -> game.play()); - updateView(); + new Thread(() -> game.play()).start(); } public static List parsePlayers() throws IllegalArgumentException, NoSuchMethodException, 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 new file mode 100644 index 0000000..4ed69c0 --- /dev/null +++ b/client/src/main/java/fr/lnl/game/client/listener/UpdateViewEvent.java @@ -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 UpdateViewEvent extends AbstractModelListening { + + @Override + public void updateModel(Object obj) { + App.updateView(); + } +} diff --git a/server/build.gradle b/server/build.gradle index 5b194ea..31a473a 100644 --- a/server/build.gradle +++ b/server/build.gradle @@ -12,6 +12,10 @@ dependencies { testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1' } +compileJava { + options.encoding = 'UTF-8' +} + application { mainClassName = 'fr.lnl.game.server.ServerMain' } 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 706c439..0dba186 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 @@ -2,9 +2,12 @@ package fr.lnl.game.server.games; import fr.lnl.game.server.games.action.*; 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.Point; import java.util.ArrayList; import java.util.Arrays; @@ -15,28 +18,56 @@ 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) throws IllegalArgumentException { + public Game(Grid grid, List players, ModelListener viewUpdate, 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; + placePlayersBRUT(); + + } + + /** + * @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).setPoint(new Point(7, 7)); + grid.getBoard().get(new Point(7,8)).setA(grid.getPlayers().get(1)); + grid.getPlayers().get(1).setPoint(new Point(7, 8)); } public void play() { while(!isOver()) { - ModelListener awakeEvent = new AwakeGame(this); - generateAndGetPlayerActions(currentPlayer); - interfaceAction = InterfaceAction.SELECT_ACTION; - waitForInterfaceEvent(); - selectedAction.doAction(); + 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(); + } + selectedAction = null; nextCurrentPlayer(); + viewUpdateEvent.updateModel(null); + gameFinishEvent.updateModel(null); } + } private void waitForInterfaceEvent() { diff --git a/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java new file mode 100644 index 0000000..9468333 --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java @@ -0,0 +1,10 @@ +package fr.lnl.game.server.games.player; + +import fr.lnl.game.server.utils.Point; + +public abstract class ComputerPlayer extends AbstractPlayer { + + public ComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) { + super(id, point, false, classPlayer); + } +} diff --git a/server/src/main/java/fr/lnl/game/server/games/player/RandomComputerPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/RandomComputerPlayer.java index 4b44d77..f735b49 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/RandomComputerPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/RandomComputerPlayer.java @@ -6,10 +6,10 @@ import fr.lnl.game.server.utils.Point; import java.util.Random; -public class RandomComputerPlayer extends AbstractPlayer{ +public class RandomComputerPlayer extends ComputerPlayer { public RandomComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) { - super(id,point,false, classPlayer); + super(id,point, classPlayer); } @Override diff --git a/server/src/main/java/fr/lnl/game/server/games/player/StrategyComputerPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/StrategyComputerPlayer.java index c47f230..763c4f5 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/StrategyComputerPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/StrategyComputerPlayer.java @@ -3,10 +3,10 @@ package fr.lnl.game.server.games.player; import fr.lnl.game.server.games.action.Action; import fr.lnl.game.server.utils.Point; -public class StrategyComputerPlayer extends AbstractPlayer{ +public class StrategyComputerPlayer extends ComputerPlayer { public StrategyComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) { - super(id,point,false, classPlayer); + super(id,point, classPlayer); } @Override diff --git a/server/src/main/java/fr/lnl/game/server/listener/GameFinishEvent.java b/server/src/main/java/fr/lnl/game/server/listener/GameFinishEvent.java new file mode 100644 index 0000000..f09595f --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/listener/GameFinishEvent.java @@ -0,0 +1,20 @@ +package fr.lnl.game.server.listener; + +import fr.lnl.game.server.listener.AbstractModelListening; + +public class GameFinishEvent extends AbstractModelListening { + + private final Object lock = new Object(); + + @Override + public void updateModel(Object obj) { + synchronized (lock) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.exit(0); + } +} 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 2396f52..1bb7581 100644 --- a/server/src/test/java/fr/lnl/game/server/Mock.java +++ b/server/src/test/java/fr/lnl/game/server/Mock.java @@ -1,12 +1,14 @@ 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; import fr.lnl.game.server.games.player.ClassPlayer; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.RandomComputerPlayer; +import fr.lnl.game.server.listener.GameFinishEvent; import fr.lnl.game.server.utils.Cardinal; import fr.lnl.game.server.utils.Point; @@ -25,7 +27,7 @@ public class Mock { placePlayersBRUT(); placeEnergyBallBRUT(); placeInternWallBRUT(); - game = new Game(grid, players); + game = new Game(grid, players, new MockViewUpdate(), 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 new file mode 100644 index 0000000..05ec33f --- /dev/null +++ b/server/src/test/java/fr/lnl/game/server/games/MockViewUpdate.java @@ -0,0 +1,10 @@ +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 + } +}