Add Main.java, View.java, Window.java, Terminal.java and Game.java

This commit is contained in:
Quentin Legot 2021-03-27 11:12:20 +01:00
parent 609b2b821a
commit 8524f83864
8 changed files with 119 additions and 5 deletions

60
src/battleship/Main.java Normal file
View File

@ -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 <Human/Random> <Human/Random> [nogui]");
System.out.println("<param>: 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<Pair<String, Class<? extends Player>>> 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<String, Class<? extends Player>> 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");
}
}

View File

@ -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;
}
}

View File

@ -5,7 +5,7 @@ import battleship.utils.Triplet;
public class Human extends Player { public class Human extends Player {
@Override @Override
public Triplet chooseMove() { public Triplet<Integer,Integer,Boolean> chooseMove() {
return null; return null;
} }
} }

View File

@ -4,6 +4,7 @@ import battleship.model.Ship;
import battleship.utils.Triplet; import battleship.utils.Triplet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
public abstract class Player { public abstract class Player {
@ -14,12 +15,19 @@ public abstract class Player {
setShips(); setShips();
} }
public void setShips(){ public void setShips(Ship... ships){
this.ships.addAll(Arrays.asList(ships));
} }
public void addMove(Triplet<Integer,Integer,Boolean> 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<Integer,Integer,Boolean> move){
moves.add(move); moves.add(move);
return this;
} }
public abstract Triplet<Integer,Integer,Boolean> chooseMove(); public abstract Triplet<Integer,Integer,Boolean> chooseMove();
} }

View File

@ -4,7 +4,7 @@ import battleship.utils.Triplet;
public class Random extends Player{ public class Random extends Player{
@Override @Override
public Triplet chooseMove() { public Triplet<Integer,Integer,Boolean> chooseMove() {
return null; return null;
} }
} }

View File

@ -0,0 +1,11 @@
package battleship.view;
import battleship.model.Game;
public class Terminal extends View {
public Terminal(Game game) {
super(game);
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,10 @@
package battleship.view;
import battleship.model.Game;
public class Window extends View {
public Window(Game game) {
super(game);
}
}