Add Direction enum + fix Game.setShips(Player)

This commit is contained in:
Quentin Legot 2021-03-28 14:35:10 +02:00
parent be296d3946
commit 19bd363503
4 changed files with 84 additions and 59 deletions

View File

@ -0,0 +1,28 @@
package battleship.model;
import battleship.utils.Pair;
public enum Direction {
RIGHT(new Pair<>(1, 0), "D"),
LEFT(new Pair<>(-1,0), "G"),
UP(new Pair<>(0,-1), "H"),
DOWN(new Pair<>(0,1), "B"),
DEFAULT(new Pair<>(-1,-1), null);
private final Pair<Integer, Integer> direction;
private final String keyword;
<K, U> Direction(Pair<Integer, Integer> ukPair, String keyword) {
this.direction = ukPair;
this.keyword = keyword;
}
public Pair<Integer, Integer> getDirection() {
return direction;
}
public String getKeyword() {
return keyword;
}
}

View File

@ -11,38 +11,36 @@ public class Game {
public Player[] players; public Player[] players;
public Player currentPlayer; public Player currentPlayer;
private int[] ships = new int[5]; private final int[] ships = { 5, 4, 3, 3, 2};
public Game(Player[] players) { public Game(Player[] players) {
this.players = players; this.players = players;
this.currentPlayer = players[0]; this.currentPlayer = players[0];
players[0].setId(1); players[0].setId(1);
players[1].setId(2); players[1].setId(2);
ships[0] = 5;
ships[1] = 4;
ships[2] = 3;
ships[3] = 3;
ships[4] = 2;
} }
public Player getCurrentPlayer(){ public Player getCurrentPlayer(){
return this.currentPlayer; return this.currentPlayer;
} }
public void changeCurrentPlayer(){ public void changeCurrentPlayer(){
currentPlayer = (currentPlayer == players[1])? players[0] : players[1]; currentPlayer = (currentPlayer == players[1])? players[0] : players[1];
} }
public void checkDrownedShips(){ public void checkDrownedShips(){
Player otherPlayer = (currentPlayer == players[1])? players[0] : players[1]; Player otherPlayer = (currentPlayer == players[1])? players[0] : players[1];
for(Ship ship : currentPlayer.getShips()){ for(Ship ship : currentPlayer.getShips()){
if(!ship.hasDrown()) if(!ship.isDrown())
otherPlayer.isItDrown(ship); otherPlayer.isItDrown(ship);
} }
} }
public Player getWinner(){ public Player getWinner(){
int cpt = 0; int cpt = 0;
for(Ship ship : players[0].getShips()){ for(Ship ship : players[0].getShips()){
if(!ship.hasDrown()) if(!ship.isDrown())
break; break;
else else
cpt ++; cpt ++;
@ -51,7 +49,7 @@ public class Game {
return players[1]; return players[1];
cpt = 0; cpt = 0;
for(Ship ship : players[1].getShips()){ for(Ship ship : players[1].getShips()){
if(!ship.hasDrown()) if(!ship.isDrown())
break; break;
else else
cpt ++; cpt ++;
@ -76,41 +74,36 @@ public class Game {
} }
public void setShips(Player player){ public void setShips(Player player){
Ship ship = new Ship(new Pair<>(0,0),2,new Pair<>(-1,-1));
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
int x,y; int x, y;
String dir = "AB"; String dir;
Pair pair = new Pair<>(null,null);
for(int i : ships){ for(int i : ships){
while(!player.setShips(ship)){ boolean valid = false;
System.out.println("Placement du bateau de longueur "+i +" :\n"); Ship ship = new Ship(new Pair<>(0,0), i, Direction.DEFAULT);
while(!player.setShips(ship)) {
if(valid) {
System.out.println("Erreur");
}
valid = true;
System.out.println("Placement du bateau de longueur "+ ship.getSize());
System.out.println("Veuillez indiquer la coordonée x de votre bateau"); System.out.println("Veuillez indiquer la coordonée x de votre bateau");
x = scanner.nextInt(); x = scanner.nextInt();
System.out.println("Veuillez indiquer la coordonée y de votre bateau"); System.out.println("Veuillez indiquer la coordonée y de votre bateau");
y = scanner.nextInt(); y = scanner.nextInt();
ship.setCoords(new Pair<>(x,y)); ship.setCoords(new Pair<>(x, y));
while(!dir.equals("D") || !dir.equals("H") || !dir.equals("B") || !dir.equals("G")){ boolean validDirection = false;
while(!validDirection){
System.out.println("Veuillez indiquer la direction de placement de votre bateau (d droite, h haut, b bas, g gauche)"); System.out.println("Veuillez indiquer la direction de placement de votre bateau (d droite, h haut, b bas, g gauche)");
dir = scanner.nextLine(); dir = scanner.next().toUpperCase();
System.out.println(dir); System.out.println(dir);
dir = (String.valueOf(dir.substring(0,1))).toUpperCase(); for(Direction direction : Direction.values()) {
if(direction.getKeyword() != null && direction.getKeyword().equals(dir)) {
} ship.setDirection(direction);
switch (dir){ validDirection = true;
case "D":
ship.setDirection(new Pair<>(1,0));
break;
case "H":
ship.setDirection(new Pair<>(0,1));
break;
case "B":
ship.setDirection(new Pair<>(0,-1));
break;
case "G":
ship.setDirection(new Pair<>(-1,0));
break; break;
} }
}
}
} }
} }

View File

@ -6,17 +6,16 @@ public class Ship {
private Pair<Integer, Integer> coords; private Pair<Integer, Integer> coords;
private final int size; private final int size;
private Pair<Integer,Integer> direction; private Direction 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, Direction direction) {
this.coords = coords; this.coords = coords;
this.size = size; this.size = size;
this.direction = direction; this.direction = direction;
isDrown = false; isDrown = false;
} }
public void setDirection(Pair<Integer,Integer> d){ public void setDirection(Direction d){
this.direction = d; this.direction = d;
} }
public void setCoords(Pair<Integer,Integer> c){ public void setCoords(Pair<Integer,Integer> c){
@ -27,23 +26,29 @@ public class Ship {
public void setDrown(){ public void setDrown(){
isDrown = true; isDrown = true;
} }
public Boolean hasDrown(){
public Boolean isDrown(){
return isDrown; return isDrown;
} }
public int getSize(){ public int getSize(){
return this.size; return this.size;
} }
public Pair<Integer, Integer> getDirection(){
public Direction getDirection(){
return this.direction; return this.direction;
} }
public Pair<Integer,Integer> getCoords(){ public Pair<Integer,Integer> getCoords(){
return this.coords; return this.coords;
} }
@SuppressWarnings("unchecked")
public Pair<Integer, Integer>[] getCoordsArray(){ public Pair<Integer, Integer>[] getCoordsArray(){
Pair<Integer,Integer>[] tab = new Pair[size]; Pair<Integer,Integer>[] tab = new Pair[size];
for(int i = 0; i<size;i++){ for(int i = 0; i<size;i++){
for(int y = 0;y<size;y++){ for(int y = 0;y<size;y++){
tab[i] = new Pair<>(coords.getLeft()+i* direction.getLeft(),coords.getRight()+i* direction.getRight()); tab[i] = new Pair<>(coords.getLeft()+i* direction.getDirection().getLeft(),coords.getRight()+i * direction.getDirection().getRight());
} }
} }
return tab; return tab;

View File

@ -12,15 +12,12 @@ public abstract class Player {
protected ArrayList<Triplet<Integer,Integer,Boolean>> moves = new ArrayList<>(); protected ArrayList<Triplet<Integer,Integer,Boolean>> moves = new ArrayList<>();
protected int id; protected int id;
public Player(){
}
public boolean setShips(Ship ship){ public boolean setShips(Ship ship){
int x,y; int x, y;
for(int i = 0; i<ship.getSize();i++){ for(int i = 0; i < ship.getSize(); i++){
x = ship.getCoords().getLeft()+i* ship.getDirection().getLeft(); x = ship.getCoords().getLeft() + i * ship.getDirection().getDirection().getLeft();
y = ship.getCoords().getRight()+i* ship.getDirection().getRight(); y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight();
if(x > 9 ||x<0||y>9||y<0) if(x > 9 || x < 0 || y > 9 || y < 0)
return false; return false;
} }
this.ships.add(ship); this.ships.add(ship);
@ -36,12 +33,13 @@ public abstract class Player {
moves.add(move); moves.add(move);
return this; return this;
} }
public Boolean isItDrown(Ship ship){
public boolean isItDrown(Ship ship){
int cpt = 0; int cpt = 0;
for(Triplet<Integer,Integer,Boolean> move : moves){ for(Triplet<Integer,Integer,Boolean> move : moves){
for(int i =1;i<=ship.getSize();i++){ for(int i = 1; i <= ship.getSize(); i++){
int x = ship.getCoords().getLeft()+i*ship.getDirection().getLeft(); int x = ship.getCoords().getLeft()+ i * ship.getDirection().getDirection().getLeft();
int y = ship.getCoords().getRight()+i*ship.getDirection().getRight(); int y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight();
if((move.getLeft() == x) && (move.getMiddle() == y)){ if((move.getLeft() == x) && (move.getMiddle() == y)){
cpt += 1; cpt += 1;
break; break;
@ -50,20 +48,21 @@ public abstract class Player {
} }
if(cpt == ship.getSize()) { if(cpt == ship.getSize()) {
ship.setDrown(); ship.setDrown();
return Boolean.TRUE; return true;
} }
return Boolean.FALSE; return false;
} }
public ArrayList<Ship> getShips(){ public ArrayList<Ship> getShips(){
return this.ships; return this.ships;
} }
public ArrayList<Triplet<Integer,Integer,Boolean>> getMoves(){ public ArrayList<Triplet<Integer,Integer,Boolean>> getMoves(){
return this.moves; return this.moves;
} }
public abstract Pair<Integer,Integer> chooseMove(); public abstract Pair<Integer,Integer> chooseMove();
public ArrayList<Pair<Integer,Integer>> validMoves(){ public ArrayList<Pair<Integer,Integer>> validMoves() {
ArrayList<Pair<Integer,Integer>> validMovesList = new ArrayList<>(); ArrayList<Pair<Integer,Integer>> validMovesList = new ArrayList<>();
for(Integer i = 0; i<10;i++){ for(Integer i = 0; i<10;i++){
for(Integer y = 0;y<10;y++){ for(Integer y = 0;y<10;y++){
@ -75,7 +74,7 @@ public abstract class Player {
return validMovesList; return validMovesList;
} }
public void setId(int i ){ public void setId(int i){
id = i; id = i;
} }