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

View File

@ -3,15 +3,7 @@ package fr.lnl.game.client;
import fr.lnl.game.client.view.View;
import fr.lnl.game.server.games.player.Player;
public class ClientPlayer {
private final Player serverPlayer;
private final View view;
public ClientPlayer(Player serverPlayer, View view) {
this.serverPlayer = serverPlayer;
this.view = view;
}
public record ClientPlayer(Player serverPlayer, View view) {
public Player getServerPlayer() {
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() {
// TODO: 26/10/2021
game.getGrid().printGrid();
System.out.println(game.getGrid().toString());
}
}

View File

@ -1,14 +1,5 @@
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 static void main(String[] args) {

View File

@ -11,7 +11,7 @@ public class Game {
Player currentPlayer;
List<Player> players;
public Game(Grid grid, List<Player> players) {
public Game(Grid grid, List<Player> players) throws IllegalArgumentException {
if(players.size() < 2)
throw new IllegalArgumentException("The game need 2 or more player to start");
this.players = players;
@ -19,7 +19,7 @@ public class Game {
this.grid = grid;
}
public void play(){
public void play() {
}
@ -27,7 +27,7 @@ public class Game {
return players.parallelStream().filter(player -> !player.isAlive()).count() == 1;
}
public Player getWinner(){
public Player getWinner() {
return players.parallelStream().filter(player -> !player.isAlive()).findFirst().orElseThrow(ArrayIndexOutOfBoundsException::new);
}
@ -35,11 +35,16 @@ public class Game {
return currentPlayer;
}
/**
* Change player to the next available in the list
*/
public void nextCurrentPlayer() {
do {
int index = players.indexOf(currentPlayer) + 1;
if(index == players.size())
index = 0;
currentPlayer = players.get(index);
} while(!currentPlayer.isAlive()); // On arrête la boucle dès qu'on trouve un joueur en vie
}
public Grid getGrid() {

View File

@ -3,7 +3,7 @@ package fr.lnl.game.server.games.action;
import fr.lnl.game.server.games.Game;
public abstract class AbstractAction implements Action {
private Game game;
private final Game game;
public AbstractAction(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.grid.Bomb;
import fr.lnl.game.server.games.grid.Mine;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Point;
@ -15,7 +14,7 @@ public class DropBomb extends DropObject {
super(game);
}
//voir pour la redondance de code au niveau de DropBomb,DropObject,DropMine
// voir pour la redondance de code au niveau de DropBomb, DropObject,DropMine
@Override
public void doAction() {
List<Point> points = getValidPoint();

View File

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

View File

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

View File

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

View File

@ -6,13 +6,13 @@ import fr.lnl.game.server.utils.Point;
public abstract class AbstractPlayer implements Player {
private int id;
private final int id;
private Point point;
private int energy;
private Weapon weapon;
private boolean shieldDeploy;
private Action[] actions;
private ClassPlayer classPlayer;
private final ClassPlayer classPlayer;
public AbstractPlayer(Integer id, Point point, boolean shieldDeploy, ClassPlayer classPlayer) {
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;
}
// 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
@Test
public void moveActionTest() {

View File

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