Add Main.java, View.java, Window.java, Terminal.java and Game.java
This commit is contained in:
parent
609b2b821a
commit
8524f83864
60
src/battleship/Main.java
Normal file
60
src/battleship/Main.java
Normal 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");
|
||||
}
|
||||
}
|
12
src/battleship/model/Game.java
Normal file
12
src/battleship/model/Game.java
Normal 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;
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ import battleship.utils.Triplet;
|
||||
public class Human extends Player {
|
||||
|
||||
@Override
|
||||
public Triplet chooseMove() {
|
||||
public Triplet<Integer,Integer,Boolean> chooseMove() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -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<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);
|
||||
return this;
|
||||
}
|
||||
|
||||
public abstract Triplet<Integer,Integer,Boolean> chooseMove();
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import battleship.utils.Triplet;
|
||||
|
||||
public class Random extends Player{
|
||||
@Override
|
||||
public Triplet chooseMove() {
|
||||
public Triplet<Integer,Integer,Boolean> chooseMove() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
11
src/battleship/view/Terminal.java
Normal file
11
src/battleship/view/Terminal.java
Normal file
@ -0,0 +1,11 @@
|
||||
package battleship.view;
|
||||
|
||||
import battleship.model.Game;
|
||||
|
||||
public class Terminal extends View {
|
||||
|
||||
|
||||
public Terminal(Game game) {
|
||||
super(game);
|
||||
}
|
||||
}
|
13
src/battleship/view/View.java
Normal file
13
src/battleship/view/View.java
Normal 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;
|
||||
}
|
||||
}
|
10
src/battleship/view/Window.java
Normal file
10
src/battleship/view/Window.java
Normal file
@ -0,0 +1,10 @@
|
||||
package battleship.view;
|
||||
|
||||
import battleship.model.Game;
|
||||
|
||||
public class Window extends View {
|
||||
|
||||
public Window(Game game) {
|
||||
super(game);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user