FINALLY fix the game with Window (currently not work with terminal)
This commit is contained in:
parent
2ebd51f011
commit
4c7017ff97
@ -1,7 +1,6 @@
|
|||||||
package fr.lnl.game.client;
|
package fr.lnl.game.client;
|
||||||
import fr.lnl.game.client.view.*;
|
import fr.lnl.game.client.view.*;
|
||||||
import fr.lnl.game.server.listener.GameFinishEvent;
|
import fr.lnl.game.server.listener.GameFinishEvent;
|
||||||
import fr.lnl.game.client.listener.UpdateViewEvent;
|
|
||||||
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.Grid;
|
||||||
import fr.lnl.game.server.games.player.*;
|
import fr.lnl.game.server.games.player.*;
|
||||||
@ -39,7 +38,7 @@ 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();
|
||||||
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()) {
|
for (Player player : game.getPlayers()) {
|
||||||
playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player)));
|
playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player)));
|
||||||
}
|
}
|
||||||
@ -54,7 +53,6 @@ public class App extends Application {
|
|||||||
throw new CrashException(e.getMessage(), e);
|
throw new CrashException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
viewManager = new ViewManager(playerList, game);
|
viewManager = new ViewManager(playerList, game);
|
||||||
new Thread(() -> game.play()).start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void launchTerminal() {
|
public static void launchTerminal() {
|
||||||
@ -65,7 +63,6 @@ public class App extends Application {
|
|||||||
throw new CrashException(e.getMessage(), e);
|
throw new CrashException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
viewManager = new ViewManager(playerList, game);
|
viewManager = new ViewManager(playerList, game);
|
||||||
new Thread(() -> game.play()).start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Player> parsePlayers() throws IllegalArgumentException, NoSuchMethodException,
|
public static List<Player> parsePlayers() throws IllegalArgumentException, NoSuchMethodException,
|
||||||
|
@ -1,12 +1,21 @@
|
|||||||
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.Game;
|
||||||
import fr.lnl.game.server.listener.AbstractModelListening;
|
import fr.lnl.game.server.listener.AbstractModelListening;
|
||||||
import fr.lnl.game.server.listener.ModelListener;
|
import fr.lnl.game.server.listener.ModelListener;
|
||||||
|
import javafx.scene.control.Alert;
|
||||||
|
import javafx.scene.control.Dialog;
|
||||||
|
|
||||||
public class ButtonListener extends AbstractModelListening {
|
public class ButtonListener extends AbstractModelListening {
|
||||||
|
|
||||||
|
|
||||||
|
private final Game game;
|
||||||
|
|
||||||
|
public ButtonListener(Game game) {
|
||||||
|
this.game = game;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addListener(ModelListener e) {
|
public void addListener(ModelListener e) {
|
||||||
this.listeners.add(e);
|
this.listeners.add(e);
|
||||||
@ -20,6 +29,12 @@ public class ButtonListener extends AbstractModelListening {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateModel(Object event) {
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -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<Integer> {
|
|
||||||
@Override
|
|
||||||
protected Task<Integer> createTask() {
|
|
||||||
return new Task<>() {
|
|
||||||
protected Integer call() {
|
|
||||||
if(App.viewManager.getNeedUpdate()) {
|
|
||||||
App.viewManager.updateView();
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
@ -12,27 +12,14 @@ public class ViewManager {
|
|||||||
|
|
||||||
private final HashMap<Player, ClientPlayer> players;
|
private final HashMap<Player, ClientPlayer> players;
|
||||||
private final Game game;
|
private final Game game;
|
||||||
private Boolean needUpdate = true;
|
|
||||||
|
|
||||||
public ViewManager(HashMap<Player, ClientPlayer> players, Game game) {
|
public ViewManager(HashMap<Player, ClientPlayer> players, Game game) {
|
||||||
this.players = players;
|
this.players = players;
|
||||||
this.game = game;
|
this.game = game;
|
||||||
TimerService timer = new TimerService();
|
|
||||||
timer.setPeriod(Duration.millis(30));
|
|
||||||
updateView();
|
updateView();
|
||||||
timer.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateView() {
|
public void updateView() {
|
||||||
players.get(game.getCurrentPlayer()).getView().show();
|
players.get(game.getCurrentPlayer()).getView().show();
|
||||||
setNeedUpdate(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Boolean getNeedUpdate() {
|
|
||||||
return needUpdate;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setNeedUpdate(Boolean needUpdate) {
|
|
||||||
this.needUpdate = needUpdate;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,9 +25,10 @@ public class Window extends AbstractView {
|
|||||||
String javaVersion = System.getProperty("java.version");
|
String javaVersion = System.getProperty("java.version");
|
||||||
String javafxVersion = System.getProperty("javafx.version");
|
String javafxVersion = System.getProperty("javafx.version");
|
||||||
Label l = new Label("Hello, JavaFX version " + javafxVersion + ", running on Java " + javaVersion + ".");
|
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());
|
Label l2 = new Label("I'm " + player.getClass().getSimpleName() + " num " + player.getId() + "" +
|
||||||
Button b = new Button("Click to see others players informations");
|
" my class is " + player.getClassPlayer().toString() + ", my remaining energy is " + player.getEnergy());
|
||||||
b.setOnAction(new ClientEventHandler(new ButtonListener()));
|
Button b = new Button("Click to continue the game");
|
||||||
|
b.setOnAction(new ClientEventHandler(new ButtonListener(game)));
|
||||||
GridPane grid = new GridPane();
|
GridPane grid = new GridPane();
|
||||||
grid.add(l, 0, 0);
|
grid.add(l, 0, 0);
|
||||||
grid.add(l2, 0, 1);
|
grid.add(l2, 0, 1);
|
||||||
|
@ -5,7 +5,6 @@ import fr.lnl.game.server.games.grid.Grid;
|
|||||||
import fr.lnl.game.server.games.player.ComputerPlayer;
|
import fr.lnl.game.server.games.player.ComputerPlayer;
|
||||||
import fr.lnl.game.server.games.player.HumanPlayer;
|
import fr.lnl.game.server.games.player.HumanPlayer;
|
||||||
import fr.lnl.game.server.games.player.Player;
|
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.listener.ModelListener;
|
||||||
import fr.lnl.game.server.utils.CrashException;
|
import fr.lnl.game.server.utils.CrashException;
|
||||||
import fr.lnl.game.server.utils.Point;
|
import fr.lnl.game.server.utils.Point;
|
||||||
@ -13,31 +12,25 @@ import fr.lnl.game.server.utils.Point;
|
|||||||
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.concurrent.TimeUnit;
|
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class Game {
|
public class Game {
|
||||||
|
|
||||||
private final Grid grid;
|
private final Grid grid;
|
||||||
private final List<Player> players;
|
private final List<Player> players;
|
||||||
private final ModelListener viewUpdateEvent;
|
|
||||||
private final ModelListener gameFinishEvent;
|
private final ModelListener gameFinishEvent;
|
||||||
private Player currentPlayer;
|
private Player currentPlayer;
|
||||||
private InterfaceAction interfaceAction;
|
|
||||||
private Action selectedAction = null;
|
private Action selectedAction = null;
|
||||||
ModelListener awakeEvent;
|
|
||||||
private final Object lock = new Object();
|
|
||||||
|
|
||||||
public Game(Grid grid, List<Player> players, ModelListener viewUpdate, ModelListener gameFinishEvent) throws IllegalArgumentException {
|
public Game(Grid grid, List<Player> players, ModelListener gameFinishEvent) throws IllegalArgumentException {
|
||||||
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");
|
||||||
this.players = players;
|
this.players = players;
|
||||||
this.currentPlayer = players.get(0);
|
this.currentPlayer = players.get(0);
|
||||||
this.grid = grid;
|
this.grid = grid;
|
||||||
this.viewUpdateEvent = viewUpdate;
|
|
||||||
this.gameFinishEvent = gameFinishEvent;
|
this.gameFinishEvent = gameFinishEvent;
|
||||||
placePlayersBRUT();
|
placePlayersBRUT();
|
||||||
|
currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,54 +45,36 @@ public class Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void play() {
|
public void play() {
|
||||||
while(!isOver()) {
|
if (currentPlayer instanceof ComputerPlayer player) {
|
||||||
awakeEvent = new AwakeGame(this);
|
selectedAction = player.choseAction();
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
gameFinishEvent.updateModel(null);
|
selectedAction.doAction();
|
||||||
|
nextCurrentPlayer();
|
||||||
|
currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer));
|
||||||
|
if(isOver()) {
|
||||||
|
gameFinishEvent.updateModel(null);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void waitForInterfaceEvent() {
|
private void waitForInterfaceEvent() {
|
||||||
synchronized (lock){
|
|
||||||
try {
|
|
||||||
wait();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void waitNSeconds(int n) {
|
|
||||||
synchronized (this){
|
synchronized (this){
|
||||||
try {
|
try {
|
||||||
wait(TimeUnit.SECONDS.toMillis(n));
|
wait();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
throw new CrashException(e.getMessage(), e);
|
throw new CrashException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resumeThread() {
|
public void resumeThread() {
|
||||||
synchronized (lock) {
|
synchronized (this) {
|
||||||
lock.notifyAll();
|
notifyAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected List<Action> generateAndGetPlayerActions(Player player) {
|
public List<Action> generateAndGetPlayerActions(Player player) {
|
||||||
List<Action> actions = new ArrayList<>();
|
List<Action> actions = new ArrayList<>();
|
||||||
for(Direction direction : Direction.values()) {
|
for(Direction direction : Direction.values()) {
|
||||||
try {
|
try {
|
||||||
@ -164,7 +139,7 @@ public class Game {
|
|||||||
return players;
|
return players;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InterfaceAction getInterfaceAction() {
|
public Action getSelectedAction() {
|
||||||
return interfaceAction;
|
return selectedAction;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,6 +2,7 @@ package fr.lnl.game.server.games;
|
|||||||
|
|
||||||
public enum InterfaceAction {
|
public enum InterfaceAction {
|
||||||
|
|
||||||
SELECT_ACTION
|
SELECT_ACTION,
|
||||||
|
DIALOG_TO_CONTINUE_COMPUTER_IA
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +1,5 @@
|
|||||||
package fr.lnl.game.server.listener;
|
package fr.lnl.game.server.listener;
|
||||||
|
|
||||||
import fr.lnl.game.server.listener.AbstractModelListening;
|
|
||||||
|
|
||||||
public class GameFinishEvent extends AbstractModelListening {
|
public class GameFinishEvent extends AbstractModelListening {
|
||||||
|
|
||||||
private final Object lock = new Object();
|
private final Object lock = new Object();
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package fr.lnl.game.server;
|
package fr.lnl.game.server;
|
||||||
|
|
||||||
import fr.lnl.game.server.games.Game;
|
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.EnergyBall;
|
||||||
import fr.lnl.game.server.games.grid.Grid;
|
import fr.lnl.game.server.games.grid.Grid;
|
||||||
import fr.lnl.game.server.games.grid.Wall;
|
import fr.lnl.game.server.games.grid.Wall;
|
||||||
@ -27,7 +26,7 @@ public class Mock {
|
|||||||
placePlayersBRUT();
|
placePlayersBRUT();
|
||||||
placeEnergyBallBRUT();
|
placeEnergyBallBRUT();
|
||||||
placeInternWallBRUT();
|
placeInternWallBRUT();
|
||||||
game = new Game(grid, players, new MockViewUpdate(), new GameFinishEvent());
|
game = new Game(grid, players, new GameFinishEvent());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void placePlayersBRUT(){
|
public void placePlayersBRUT(){
|
||||||
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user