implement Game#play

This commit is contained in:
Quentin Legot 2021-11-27 17:47:05 +01:00
parent 42b0c656f8
commit 9cfa56be8d
9 changed files with 90 additions and 22 deletions

View File

@ -35,10 +35,13 @@ public class App extends Application {
} }
} }
public static Game startGame() throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, public static void startGame() throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
InstantiationException, IllegalAccessException { InstantiationException, IllegalAccessException {
List<Player> players = parsePlayers(); List<Player> players = parsePlayers();
return new Game(new Grid(12, 12, players), players); for (Player player : game.getPlayers()) {
playerList.put(player, new ClientPlayer(player, new Terminal(game, player)));
}
game = new Game(new Grid(12, 12, players), players);
} }
public static void updateView() { public static void updateView() {
@ -48,28 +51,23 @@ public class App extends Application {
@Override @Override
public void start(Stage stage) { public void start(Stage stage) {
try { try {
game = startGame(); startGame();
} catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException } catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException
| IllegalAccessException e) { | IllegalAccessException e) {
throw new CrashException(e.getMessage(), e); throw new CrashException(e.getMessage(), e);
} }
for (Player player : game.getPlayers()) { new Thread(() -> game.play());
playerList.put(player, new ClientPlayer(player, new Window(stage, game, player)));
}
updateView(); updateView();
} }
public static void launchTerminal() { public static void launchTerminal() {
try { try {
game = startGame(); startGame();
} catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException } catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException
| IllegalAccessException e) { | IllegalAccessException e) {
throw new CrashException(e.getMessage(), e); throw new CrashException(e.getMessage(), e);
} }
new Thread(() -> game.play());
for (Player player : game.getPlayers()) {
playerList.put(player, new ClientPlayer(player, new Terminal(game, player)));
}
updateView(); updateView();
} }

View File

@ -1,8 +1,10 @@
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.listener.AbstractModelListening;
import fr.lnl.game.server.listener.ModelListener;
public class ButtonListener extends AbstractModelListening{ public class ButtonListener extends AbstractModelListening {
@Override @Override

View File

@ -1,6 +1,6 @@
package fr.lnl.game.client.view; package fr.lnl.game.client.view;
import fr.lnl.game.client.listener.ModelListener; import fr.lnl.game.server.listener.ModelListener;
import fr.lnl.game.client.listener.ButtonListener; import fr.lnl.game.client.listener.ButtonListener;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;

View File

@ -3,16 +3,22 @@ package fr.lnl.game.server.games;
import fr.lnl.game.server.games.action.*; import fr.lnl.game.server.games.action.*;
import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.Grid;
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 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.stream.Stream;
public class Game { public class Game {
Grid grid; private final Grid grid;
Player currentPlayer; private final List<Player> players;
List<Player> players; private Player currentPlayer;
private InterfaceAction interfaceAction;
private Action selectedAction = null;
private final Object lock = new Object();
public Game(Grid grid, List<Player> players) throws IllegalArgumentException { public Game(Grid grid, List<Player> players) throws IllegalArgumentException {
if(players.size() < 2) if(players.size() < 2)
@ -22,8 +28,37 @@ public class Game {
this.grid = grid; this.grid = grid;
} }
public void play() { /**
*
* @return game winner
*/
public Player play() {
while(!isOver()) {
ModelListener awakeEvent = new AwakeGame(this);
generateAndGetPlayerActions(currentPlayer);
interfaceAction = InterfaceAction.SELECT_ACTION;
waitForInterfaceEvent();
selectedAction.doAction();
nextCurrentPlayer();
}
return getWinner();
}
private void waitForInterfaceEvent() {
synchronized (lock){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void resumeThread() {
synchronized (lock) {
lock.notifyAll();
}
} }
protected List<Action> generateAndGetPlayerActions(Player player) { protected List<Action> generateAndGetPlayerActions(Player player) {
@ -46,13 +81,17 @@ public class Game {
return actions; return actions;
} }
public Stream<Player> getPlayersAlive() {
return players.parallelStream().filter(Player::isAlive);
}
public boolean isOver() { public boolean isOver() {
return players.parallelStream().filter(player -> !player.isAlive()).count() == 1; return getPlayersAlive().count() <= 1;
} }
public Player getWinner() { public Player getWinner() {
// On part du principe que isOver est forcément appelé avant d'appeler getWinner // On part du principe que isOver est forcément appelé avant d'appeler getWinner
return players.parallelStream().filter(player -> !player.isAlive()).findFirst().orElse(null); return getPlayersAlive().findFirst().orElse(null);
} }
public Player getCurrentPlayer() { public Player getCurrentPlayer() {
@ -86,4 +125,8 @@ public class Game {
public List<Player> getPlayers() { public List<Player> getPlayers() {
return players; return players;
} }
public InterfaceAction getInterfaceAction() {
return interfaceAction;
}
} }

View File

@ -0,0 +1,7 @@
package fr.lnl.game.server.games;
public enum InterfaceAction {
SELECT_ACTION
}

View File

@ -1,9 +1,9 @@
package fr.lnl.game.client.listener; package fr.lnl.game.server.listener;
import java.util.List; import java.util.List;
public abstract class AbstractModelListening implements ModelListener { public abstract class AbstractModelListening implements ModelListener {
List<ModelListener> listeners; protected List<ModelListener> listeners;
public void addListener(ModelListener e) { public void addListener(ModelListener e) {
this.listeners.add(e); this.listeners.add(e);

View File

@ -0,0 +1,17 @@
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();
}
}

View File

@ -1,4 +1,4 @@
package fr.lnl.game.client.listener; package fr.lnl.game.server.listener;
public interface ModelListener { public interface ModelListener {
//à peut-être revoir //à peut-être revoir

View File

@ -6,4 +6,5 @@ module server {
exports fr.lnl.game.server.games.weapon; exports fr.lnl.game.server.games.weapon;
exports fr.lnl.game.server.games.action; exports fr.lnl.game.server.games.action;
exports fr.lnl.game.server.utils; exports fr.lnl.game.server.utils;
exports fr.lnl.game.server.listener;
} }