diff --git a/src/battleship/Main.java b/src/battleship/Main.java new file mode 100644 index 0000000..20ae9ee --- /dev/null +++ b/src/battleship/Main.java @@ -0,0 +1,60 @@ +package battleship; + +import battleship.model.Game; +import battleship.model.player.Human; +import battleship.model.player.Player; +import battleship.model.player.Random; +import battleship.utils.Pair; +import battleship.view.Terminal; +import battleship.view.View; +import battleship.view.Window; + +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.NoSuchElementException; + +public class Main { + + public static View view; + public static Game game; + + public static void main(String[] args) { + try { + parseArgs(args); + } catch (NoSuchElementException e) { + System.out.println("Pas assez d'arguments, veuillez donner des arguments de cette forme:"); + System.out.println("java -jar battleship [nogui]"); + System.out.println(": paramètre obligatoire"); + System.out.println("[param]: paramètre optionnel"); + System.exit(2); + } catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { + e.printStackTrace(); + System.exit(1); + } + } + + private static void parseArgs(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { + Player[] players = new Player[2]; + ArrayList>> playerClass = new ArrayList<>(2); + playerClass.add(new Pair<>("human", Human.class)); + playerClass.add(new Pair<>("random", Random.class)); + if(args.length >= 2) { + for(int i = 0; i < 2; ++i) { + for (Pair> pair : playerClass) { + if(args[i].equalsIgnoreCase(pair.getLeft())) { + players[i] = pair.getRight().getDeclaredConstructor().newInstance(); + } + } + } + game = new Game(players); + if(args.length >= 3) { + // arguments > 3 ignorés + if(args[2].equalsIgnoreCase("nogui")) + view = new Terminal(game); + else + view = new Window(game); + } + } else + throw new NoSuchElementException("Pas assez d'arguments"); + } +} diff --git a/src/battleship/model/Game.java b/src/battleship/model/Game.java new file mode 100644 index 0000000..51a036f --- /dev/null +++ b/src/battleship/model/Game.java @@ -0,0 +1,12 @@ +package battleship.model; + +import battleship.model.player.Player; + +public class Game { + + public Player[] players; + + public Game(Player[] players) { + this.players = players; + } +} diff --git a/src/battleship/model/player/Human.java b/src/battleship/model/player/Human.java index 904ec3e..b3fd5f6 100644 --- a/src/battleship/model/player/Human.java +++ b/src/battleship/model/player/Human.java @@ -5,7 +5,7 @@ import battleship.utils.Triplet; public class Human extends Player { @Override - public Triplet chooseMove() { + public Triplet chooseMove() { return null; } } diff --git a/src/battleship/model/player/Player.java b/src/battleship/model/player/Player.java index 1de5ceb..f2f2925 100644 --- a/src/battleship/model/player/Player.java +++ b/src/battleship/model/player/Player.java @@ -4,6 +4,7 @@ import battleship.model.Ship; import battleship.utils.Triplet; import java.util.ArrayList; +import java.util.Arrays; public abstract class Player { @@ -14,12 +15,19 @@ public abstract class Player { setShips(); } - public void setShips(){ - + public void setShips(Ship... ships){ + this.ships.addAll(Arrays.asList(ships)); } - public void addMove(Triplet move){ + /** + * La methode retourne son objet afin d'avoir la possibilité de faire Player.addMove().addMove().etc... + * @param move + * @return Player + */ + public Player addMove(Triplet move){ moves.add(move); + return this; } + public abstract Triplet chooseMove(); } diff --git a/src/battleship/model/player/Random.java b/src/battleship/model/player/Random.java index 7324d47..d8651c8 100644 --- a/src/battleship/model/player/Random.java +++ b/src/battleship/model/player/Random.java @@ -4,7 +4,7 @@ import battleship.utils.Triplet; public class Random extends Player{ @Override - public Triplet chooseMove() { + public Triplet chooseMove() { return null; } } diff --git a/src/battleship/view/Terminal.java b/src/battleship/view/Terminal.java new file mode 100644 index 0000000..db2adff --- /dev/null +++ b/src/battleship/view/Terminal.java @@ -0,0 +1,11 @@ +package battleship.view; + +import battleship.model.Game; + +public class Terminal extends View { + + + public Terminal(Game game) { + super(game); + } +} diff --git a/src/battleship/view/View.java b/src/battleship/view/View.java new file mode 100644 index 0000000..760acca --- /dev/null +++ b/src/battleship/view/View.java @@ -0,0 +1,13 @@ +package battleship.view; + +import battleship.model.Game; + +public abstract class View { + + + private final Game game; + + public View(Game game) { + this.game = game; + } +} diff --git a/src/battleship/view/Window.java b/src/battleship/view/Window.java new file mode 100644 index 0000000..d0ff33f --- /dev/null +++ b/src/battleship/view/Window.java @@ -0,0 +1,10 @@ +package battleship.view; + +import battleship.model.Game; + +public class Window extends View { + + public Window(Game game) { + super(game); + } +}