Adding getters to Ship.class, Adding a checker in Game.class and Starting the toString() method of View.class

This commit is contained in:
Arthur 2021-03-27 13:12:33 +01:00
parent 72f268cff3
commit 33ab1c131a
7 changed files with 102 additions and 10 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> [nogui]"); System.out.println("java -jar battleship <Human/Random> <Human/Random> <Int/Taille de la grille> [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 >= 2) { if(args.length >= 3) {
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); game = new Game(players,Integer.parseInt(args[2]));
if(args.length >= 3) { if(args.length >= 4) {
// arguments > 3 ignorés // arguments > 3 ignorés
if(args[2].equalsIgnoreCase("nogui")) if(args[3].equalsIgnoreCase("nogui"))
view = new Terminal(game); view = new Terminal(game);
else else
view = new Window(game); view = new Window(game);

View File

@ -5,8 +5,29 @@ import battleship.model.player.Player;
public class Game { public class Game {
public Player[] players; public Player[] players;
public Player currentPlayer;
public int size;
public Game(Player[] players) { public Game(Player[] players,int size) {
this.players = players; this.players = players;
this.size = size;
}
public Player getCurrentPlayer(){
return this.currentPlayer;
}
public void changeCurrentPlayer(){
currentPlayer = (currentPlayer == players[1])? players[0] : players[1];
}
public void checkDrownedShips(){
changeCurrentPlayer();
Player otherPlayer = currentPlayer;
changeCurrentPlayer();
for(Ship ship : currentPlayer.getShips()){
if(!ship.hasDrown())
otherPlayer.isItDrown(ship);
} }
} }
}

View File

@ -1,15 +1,35 @@
package battleship.model; package battleship.model;
import battleship.utils.Pair; import battleship.utils.*;
public class Ship { public class Ship {
public Pair<Integer, Integer> coords; private final Pair<Integer, Integer> coords;
public int size; private final int size;
private final Pair<Integer,Integer> direction;
// (0,-1) bas // (0,1) haut // (1,0) droite // (-1,0) gauche
private Boolean isDrown;
public Ship(Pair<Integer, Integer> coords, int size) { 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.isDrown = Boolean.FALSE;
}
public void isDrown(){
isDrown = Boolean.TRUE;
}
public Boolean hasDrown(){
return isDrown;
}
public int getSize(){
return this.size;
}
public Pair<Integer, Integer> getDirection(){
return this.direction;
}
public Pair<Integer,Integer> getCoords(){
return this.coords;
} }
} }

View File

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

View File

@ -28,6 +28,32 @@ public abstract class Player {
moves.add(move); moves.add(move);
return this; return this;
} }
public Boolean isItDrown(Ship ship){
int cpt = 0;
for(Triplet<Integer,Integer,Boolean> move : moves){
for(int i =1;i<=ship.getSize();i++){
int x = ship.getCoords().getLeft()+i*ship.getDirection().getLeft();
int y = ship.getCoords().getRight()+i*ship.getDirection().getRight();
if((move.getLeft() == x) && (move.getMiddle() == y)){
cpt += 1;
break;
}
}
}
if(cpt == ship.getSize()) {
ship.isDrown();
return Boolean.TRUE;
}
return Boolean.FALSE;
}
public ArrayList<Ship> getShips(){
return this.ships;
}
public ArrayList<Triplet<Integer,Integer,Boolean>> getMoves(){
return this.moves;
}
public abstract Triplet<Integer,Integer,Boolean> chooseMove(); public abstract Triplet<Integer,Integer,Boolean> chooseMove();
} }

View File

@ -3,6 +3,7 @@ package battleship.model.player;
import battleship.utils.Triplet; import battleship.utils.Triplet;
public class Random extends Player{ public class Random extends Player{
@Override @Override
public Triplet<Integer,Integer,Boolean> chooseMove() { public Triplet<Integer,Integer,Boolean> chooseMove() {
return null; return null;

View File

@ -1,6 +1,9 @@
package battleship.view; package battleship.view;
import battleship.model.Game; import battleship.model.Game;
import battleship.utils.Triplet;
import java.util.ArrayList;
public abstract class View { public abstract class View {
@ -10,4 +13,24 @@ public abstract class View {
public View(Game game) { public View(Game game) {
this.game = game; this.game = game;
} }
@Override
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 = "";
for(int i = 0; i<game.size;i++){
for(int y = 0;y<game.size;y++){
for(Triplet<Integer, Integer, Boolean> ships1 : player1){
if(i == ships1.getLeft()&& y == ships1.getMiddle()){
//chain +=
}
}
for(Triplet<Integer, Integer, Boolean> ships2 : player2){
}
}
}
// Not finished yet
return chain;
}
} }