Adding a few changes

This commit is contained in:
Arthur 2021-03-27 14:56:01 +01:00
parent 33ab1c131a
commit de37c534c0
7 changed files with 54 additions and 34 deletions

View File

@ -23,7 +23,7 @@ public class Main {
parseArgs(args); parseArgs(args);
} catch (NoSuchElementException e) { } catch (NoSuchElementException e) {
System.out.println("Pas assez d'arguments, veuillez donner des arguments de cette forme:"); System.out.println("Pas assez d'arguments, veuillez donner des arguments de cette forme:");
System.out.println("java -jar battleship <Human/Random> <Human/Random> <Int/Taille de la grille> [nogui]"); System.out.println("java -jar battleship <Human/Random> <Human/Random> [nogui]");
System.out.println("<param>: paramètre obligatoire"); System.out.println("<param>: paramètre obligatoire");
System.out.println("[param]: paramètre optionnel"); System.out.println("[param]: paramètre optionnel");
System.exit(2); System.exit(2);
@ -38,7 +38,7 @@ public class Main {
ArrayList<Pair<String, Class<? extends Player>>> playerClass = new ArrayList<>(2); ArrayList<Pair<String, Class<? extends Player>>> playerClass = new ArrayList<>(2);
playerClass.add(new Pair<>("human", Human.class)); playerClass.add(new Pair<>("human", Human.class));
playerClass.add(new Pair<>("random", Random.class)); playerClass.add(new Pair<>("random", Random.class));
if(args.length >= 3) { if(args.length >= 2) {
for(int i = 0; i < 2; ++i) { for(int i = 0; i < 2; ++i) {
for (Pair<String, Class<? extends Player>> pair : playerClass) { for (Pair<String, Class<? extends Player>> pair : playerClass) {
if(args[i].equalsIgnoreCase(pair.getLeft())) { if(args[i].equalsIgnoreCase(pair.getLeft())) {
@ -46,10 +46,10 @@ public class Main {
} }
} }
} }
game = new Game(players,Integer.parseInt(args[2])); game = new Game(players);
if(args.length >= 4) { if(args.length >= 3) {
// arguments > 3 ignorés // arguments > 3 ignorés
if(args[3].equalsIgnoreCase("nogui")) if(args[2].equalsIgnoreCase("nogui"))
view = new Terminal(game); view = new Terminal(game);
else else
view = new Window(game); view = new Window(game);

View File

@ -6,11 +6,9 @@ public class Game {
public Player[] players; public Player[] players;
public Player currentPlayer; public Player currentPlayer;
public int size;
public Game(Player[] players,int size) { public Game(Player[] players) {
this.players = players; this.players = players;
this.size = size;
} }
public Player getCurrentPlayer(){ public Player getCurrentPlayer(){
@ -20,9 +18,7 @@ public class Game {
currentPlayer = (currentPlayer == players[1])? players[0] : players[1]; currentPlayer = (currentPlayer == players[1])? players[0] : players[1];
} }
public void checkDrownedShips(){ public void checkDrownedShips(){
changeCurrentPlayer(); Player otherPlayer = (currentPlayer == players[1])? players[0] : players[1];
Player otherPlayer = currentPlayer;
changeCurrentPlayer();
for(Ship ship : currentPlayer.getShips()){ for(Ship ship : currentPlayer.getShips()){
if(!ship.hasDrown()) if(!ship.hasDrown())
otherPlayer.isItDrown(ship); otherPlayer.isItDrown(ship);

View File

@ -8,16 +8,16 @@ public class Ship {
private final int size; private final int size;
private final Pair<Integer,Integer> direction; private final Pair<Integer,Integer> direction;
// (0,-1) bas // (0,1) haut // (1,0) droite // (-1,0) gauche // (0,-1) bas // (0,1) haut // (1,0) droite // (-1,0) gauche
private Boolean isDrown; private boolean isDrown;
public Ship(Pair<Integer, Integer> coords, int size,Pair<Integer,Integer> direction) { public Ship(Pair<Integer, Integer> coords, int size,Pair<Integer,Integer> direction) {
this.coords = coords; this.coords = coords;
this.size = size; this.size = size;
this.direction = direction; this.direction = direction;
this.isDrown = Boolean.FALSE; isDrown = false;
} }
public void isDrown(){ public void setDrown(){
isDrown = Boolean.TRUE; isDrown = true;
} }
public Boolean hasDrown(){ public Boolean hasDrown(){
return isDrown; return isDrown;

View File

@ -1,12 +1,12 @@
package battleship.model.player; package battleship.model.player;
import battleship.utils.Triplet; import battleship.utils.Pair;
public class Human extends Player { public class Human extends Player {
@Override @Override
public Triplet<Integer,Integer,Boolean> chooseMove() { public Pair<Integer,Integer> chooseMove() {
return null; return null;
} }
} }

View File

@ -1,6 +1,7 @@
package battleship.model.player; package battleship.model.player;
import battleship.model.Ship; import battleship.model.Ship;
import battleship.utils.Pair;
import battleship.utils.Triplet; import battleship.utils.Triplet;
import java.util.ArrayList; import java.util.ArrayList;
@ -41,7 +42,7 @@ public abstract class Player {
} }
} }
if(cpt == ship.getSize()) { if(cpt == ship.getSize()) {
ship.isDrown(); ship.setDrown();
return Boolean.TRUE; return Boolean.TRUE;
} }
return Boolean.FALSE; return Boolean.FALSE;
@ -53,7 +54,20 @@ public abstract class Player {
return this.moves; return this.moves;
} }
public abstract Triplet<Integer,Integer,Boolean> chooseMove(); public abstract Pair<Integer,Integer> chooseMove();
public ArrayList<Pair<Integer,Integer>> validMoves(){
ArrayList<Pair<Integer,Integer>> validMovesList = new ArrayList<>();
for(Integer i = 0; i<10;i++){
for(Integer y = 0;y<10;y++){
if(!moves.contains(new Triplet<>(i,y,true)) ||!moves.contains(new Triplet<>(i,y,false))){
validMovesList.add(new Pair<>(i,y));
}
}
}
return null;
}
} }

View File

@ -1,11 +1,13 @@
package battleship.model.player; package battleship.model.player;
import battleship.utils.Triplet; import battleship.utils.Pair;
public class Random extends Player{ public class Random extends Player{
@Override @Override
public Triplet<Integer,Integer,Boolean> chooseMove() { public Pair<Integer,Integer> chooseMove() {
return null; Random rand = new Random();
int index = validMoves().get(rand.nextInt(validMoves().size()));
return validMoves().get(index);
} }
} }

View File

@ -15,22 +15,30 @@ public abstract class View {
} }
@Override @Override
public String toString(){ public String toString(){
ArrayList<Triplet<Integer,Integer,Boolean>> player1 = game.players[0].getMoves();
ArrayList<Triplet<Integer,Integer,Boolean>> player2 = game.players[1].getMoves();
String chain = ""; ArrayList<Triplet<Integer,Integer,Boolean>> player = game.currentPlayer.getMoves();
for(int i = 0; i<game.size;i++){ String chain = "A vous de joueur "+game.currentPlayer+"\n+ - - - - - - - - - - +";
for(int y = 0;y<game.size;y++){ for(int i = 0; i<10;i++){
for(Triplet<Integer, Integer, Boolean> ships1 : player1){ chain += "|";
if(i == ships1.getLeft()&& y == ships1.getMiddle()){ for(int y = 0;y<10;y++){
//chain += for(Triplet<Integer, Integer, Boolean> ships : player){
if(i == ships.getLeft()&& y == ships.getMiddle()){
if(ships.getRight() == true){
chain += " !";
} }
} else
for(Triplet<Integer, Integer, Boolean> ships2 : player2){ chain += " .";
} }
else
chain += " ";
} }
} }
// Not finished yet chain += " |\n";
}
chain += "+ - - - - - - - - - - +\n";
return chain; return chain;
} }
} }