Improved argument parser and fix a bug with grid not initialize on new instance

This commit is contained in:
Quentin Legot 2021-10-26 16:04:12 +02:00
parent ad9ac682bb
commit 2e86f71e94
15 changed files with 71 additions and 80 deletions

View File

@ -5,6 +5,7 @@ import fr.lnl.game.client.view.Window;
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.*;
import fr.lnl.game.server.utils.CrashException;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import javafx.application.Application; import javafx.application.Application;
import javafx.stage.Stage; import javafx.stage.Stage;
@ -20,47 +21,56 @@ public class App extends Application {
public static void main(String[] args) { public static void main(String[] args) {
argsList = new LinkedList<>(Arrays.asList(args)); argsList = new LinkedList<>(Arrays.asList(args));
Class<? extends AbstractView> clazz = parseView(); argsList.removeIf(s -> s.startsWith("-D") || s.equals("fr.lnl.game.client.App")); // remove given parameters from gradle
if(clazz.equals(Terminal.class)) { Class<? extends AbstractView> clazz;
try { try {
launchTerminal(); clazz = parseView();
} catch (InvocationTargetException | IllegalAccessException | InstantiationException | } catch (IllegalArgumentException e) {
NoSuchMethodException e) { throw new CrashException(e.getMessage(), e);
throw new CrashException(e.getCause());
} }
if(clazz.equals(Terminal.class)) {
launchTerminal();
} else { } else {
launch(); launch();
} }
} }
public static Game startGame() public static Game startGame() throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { InstantiationException, IllegalAccessException {
List<Player> players = parsePlayers(); List<Player> players = parsePlayers();
System.out.println(players);
return new Game(new Grid(12, 12, players), players); return new Game(new Grid(12, 12, players), players);
} }
@Override @Override
public void start(Stage stage) throws Exception { public void start(Stage stage) {
try {
game = startGame(); game = startGame();
} catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException
| IllegalAccessException e) {
throw new CrashException(e.getMessage(), e);
}
for (Player player : game.getPlayers()) { for (Player player : game.getPlayers()) {
playerList.put(player, new ClientPlayer(player, new Window(stage, game, player))); playerList.put(player, new ClientPlayer(player, new Window(stage, game, player)));
} }
playerList.get(game.getCurrentPlayer()).getView().show(); playerList.get(game.getCurrentPlayer()).getView().show();
} }
public static void launchTerminal() public static void launchTerminal() {
throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { try {
game = startGame(); game = startGame();
} catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException
| IllegalAccessException e) {
throw new CrashException(e.getMessage(), e);
}
for (Player player : game.getPlayers()) { for (Player player : game.getPlayers()) {
playerList.put(player, new ClientPlayer(player, new Terminal(game, player))); playerList.put(player, new ClientPlayer(player, new Terminal(game, player)));
} }
playerList.get(game.getCurrentPlayer()).getView().show(); playerList.get(game.getCurrentPlayer()).getView().show();
} }
// TODO: 23/10/2021 nécessite un rework -> faire une view par joueur public static List<Player> parsePlayers() throws IllegalArgumentException, NoSuchMethodException,
public static List<Player> parsePlayers() InvocationTargetException, InstantiationException, IllegalAccessException {
throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
List<Player> playerList = new ArrayList<>(); List<Player> playerList = new ArrayList<>();
Class<? extends AbstractPlayer> playerClass = null; Class<? extends AbstractPlayer> playerClass = null;
ClassPlayer classPlayer = null; ClassPlayer classPlayer = null;
@ -85,6 +95,7 @@ public class App extends Application {
playerClass = ComputerPlayer.class; playerClass = ComputerPlayer.class;
} }
case "default" -> classPlayer = ClassPlayer.DEFAULT; case "default" -> classPlayer = ClassPlayer.DEFAULT;
default -> throw new IllegalArgumentException("Unknown argument: " + str);
} }
} }
if(playerClass != null) if(playerClass != null)
@ -104,14 +115,16 @@ public class App extends Application {
public static Class<? extends AbstractView> parseView() { public static Class<? extends AbstractView> parseView() {
Class<? extends AbstractView> clazz; Class<? extends AbstractView> clazz;
if(!argsList.isEmpty()) { if(!argsList.isEmpty()) {
if(argsList.get(0).equalsIgnoreCase("terminal")) { if(argsList.get(0).equals("terminal")) {
clazz = Terminal.class; clazz = Terminal.class;
} else { } else if(argsList.get(0).equals("window")){
clazz = Window.class; clazz = Window.class;
} else {
throw new IllegalArgumentException("Unknown argument: " + argsList.get(0));
} }
argsList.removeFirst(); argsList.removeFirst();
} else { } else {
clazz = Window.class; throw new IllegalArgumentException("No argument given");
} }
return clazz; return clazz;
} }

View File

@ -3,15 +3,7 @@ package fr.lnl.game.client;
import fr.lnl.game.client.view.View; import fr.lnl.game.client.view.View;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
public class ClientPlayer { public record ClientPlayer(Player serverPlayer, View view) {
private final Player serverPlayer;
private final View view;
public ClientPlayer(Player serverPlayer, View view) {
this.serverPlayer = serverPlayer;
this.view = view;
}
public Player getServerPlayer() { public Player getServerPlayer() {
return serverPlayer; return serverPlayer;

View File

@ -1,9 +0,0 @@
package fr.lnl.game.client;
public class CrashException extends RuntimeException {
public CrashException(Throwable cause) {
super(cause);
}
}

View File

@ -11,7 +11,7 @@ public class Terminal extends AbstractView {
public void show() { public void show() {
// TODO: 26/10/2021 // TODO: 26/10/2021
game.getGrid().printGrid(); System.out.println(game.getGrid().toString());
} }
} }

View File

@ -1,14 +1,5 @@
package fr.lnl.game.server; package fr.lnl.game.server;
import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.player.ComputerPlayer;
import fr.lnl.game.server.games.player.ClassPlayer;
import fr.lnl.game.server.games.player.Player;
import java.util.Arrays;
import java.util.List;
public class ServerMain { public class ServerMain {
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -11,7 +11,7 @@ public class Game {
Player currentPlayer; Player currentPlayer;
List<Player> players; List<Player> players;
public Game(Grid grid, List<Player> players) { public Game(Grid grid, List<Player> players) 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;
@ -35,11 +35,16 @@ public class Game {
return currentPlayer; return currentPlayer;
} }
/**
* Change player to the next available in the list
*/
public void nextCurrentPlayer() { public void nextCurrentPlayer() {
do {
int index = players.indexOf(currentPlayer) + 1; int index = players.indexOf(currentPlayer) + 1;
if(index == players.size()) if(index == players.size())
index = 0; index = 0;
currentPlayer = players.get(index); currentPlayer = players.get(index);
} while(!currentPlayer.isAlive()); // On arrête la boucle dès qu'on trouve un joueur en vie
} }
public Grid getGrid() { public Grid getGrid() {

View File

@ -3,7 +3,7 @@ package fr.lnl.game.server.games.action;
import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.Game;
public abstract class AbstractAction implements Action { public abstract class AbstractAction implements Action {
private Game game; private final Game game;
public AbstractAction(Game game){ public AbstractAction(Game game){
this.game = game; this.game = game;

View File

@ -2,7 +2,6 @@ package fr.lnl.game.server.games.action;
import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.grid.Bomb; import fr.lnl.game.server.games.grid.Bomb;
import fr.lnl.game.server.games.grid.Mine;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;

View File

@ -5,7 +5,6 @@ public class EnergyBall implements Box{
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; return o != null && getClass() == o.getClass();// no var to test
return true; // no var to test
} }
} }

View File

@ -10,16 +10,17 @@ import java.util.List;
public class Grid { public class Grid {
private HashMap<Point, Pair<Player, Box>> board; private final HashMap<Point, Pair<Player, Box>> board;
private int row; private final int row;
private int column; private final int column;
private List<Player> players; private final List<Player> players;
public Grid(int row, int column, List<Player> players) { public Grid(int row, int column, List<Player> players) {
this.row = row; this.row = row;
this.column = column; this.column = column;
this.players = players; this.players = players;
board = new HashMap<>(); board = new HashMap<>();
initGrid();
} }
public void initGrid(){ public void initGrid(){
@ -121,14 +122,6 @@ public class Grid {
return str.toString(); return str.toString();
} }
/**
* @deprecated modèle mvc non respecté
*/
@Deprecated
public void printGrid() {
System.out.println(this);
}
public List<Player> getPlayers() { public List<Player> getPlayers() {
return players; return players;
} }

View File

@ -6,9 +6,9 @@ import java.util.Objects;
public class Wall implements Box { public class Wall implements Box {
private Cardinal cardinal; private final Cardinal cardinal;
private int x; private final int x;
private int y; private final int y;
public Wall(Cardinal cardinal, int x, int y){ public Wall(Cardinal cardinal, int x, int y){
this.cardinal = cardinal; this.cardinal = cardinal;

View File

@ -6,13 +6,13 @@ import fr.lnl.game.server.utils.Point;
public abstract class AbstractPlayer implements Player { public abstract class AbstractPlayer implements Player {
private int id; private final int id;
private Point point; private Point point;
private int energy; private int energy;
private Weapon weapon; private Weapon weapon;
private boolean shieldDeploy; private boolean shieldDeploy;
private Action[] actions; private Action[] actions;
private ClassPlayer classPlayer; private final ClassPlayer classPlayer;
public AbstractPlayer(Integer id, Point point, boolean shieldDeploy, ClassPlayer classPlayer) { public AbstractPlayer(Integer id, Point point, boolean shieldDeploy, ClassPlayer classPlayer) {
this.id = id; this.id = id;

View File

@ -0,0 +1,9 @@
package fr.lnl.game.server.utils;
public class CrashException extends RuntimeException {
public CrashException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -18,7 +18,7 @@ public class ActionPlayerTest {
this.game = mock.game; this.game = mock.game;
} }
// TODO: 21/10/2021 Vérifier sur Move effectue la bonne action en pensant a appeller isPossible() avant et // TODO: 21/10/2021 Vérifier sur Move effectue la bonne action en pensant a appeler isPossible() avant et
// en checkant son résultat // en checkant son résultat
@Test @Test
public void moveActionTest() { public void moveActionTest() {

View File

@ -22,7 +22,6 @@ public class Mock {
List<Player> players = Arrays.asList(new ComputerPlayer(1,null, ClassPlayer.DEFAULT), List<Player> players = Arrays.asList(new ComputerPlayer(1,null, ClassPlayer.DEFAULT),
new ComputerPlayer(2,null, ClassPlayer.DEFAULT)); new ComputerPlayer(2,null, ClassPlayer.DEFAULT));
this.grid = new Grid(16,16, players); this.grid = new Grid(16,16, players);
grid.initGrid();
placePlayersBRUT(); placePlayersBRUT();
placeEnergyBallBRUT(); placeEnergyBallBRUT();
placeInternWallBRUT(); placeInternWallBRUT();