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

View File

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

View File

@ -12,14 +12,11 @@ public abstract class Player {
protected ArrayList<Triplet<Integer,Integer,Boolean>> moves = new ArrayList<>();
protected int id;
public Player(){
}
public boolean setShips(Ship ship){
int x, y;
for(int i = 0; i < ship.getSize(); i++){
x = ship.getCoords().getLeft()+i* ship.getDirection().getLeft();
y = ship.getCoords().getRight()+i* ship.getDirection().getRight();
x = ship.getCoords().getLeft() + i * ship.getDirection().getDirection().getLeft();
y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight();
if(x > 9 || x < 0 || y > 9 || y < 0)
return false;
}
@ -36,12 +33,13 @@ public abstract class Player {
moves.add(move);
return this;
}
public Boolean isItDrown(Ship ship){
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();
int x = ship.getCoords().getLeft()+ i * ship.getDirection().getDirection().getLeft();
int y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight();
if((move.getLeft() == x) && (move.getMiddle() == y)){
cpt += 1;
break;
@ -50,13 +48,14 @@ public abstract class Player {
}
if(cpt == ship.getSize()) {
ship.setDrown();
return Boolean.TRUE;
return true;
}
return Boolean.FALSE;
return false;
}
public ArrayList<Ship> getShips(){
return this.ships;
}
public ArrayList<Triplet<Integer,Integer,Boolean>> getMoves(){
return this.moves;
}