Add Direction enum + fix Game.setShips(Player)
This commit is contained in:
parent
be296d3946
commit
19bd363503
28
src/battleship/model/Direction.java
Normal file
28
src/battleship/model/Direction.java
Normal 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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
int x, y;
|
||||
String dir;
|
||||
for(int i : ships){
|
||||
while(!player.setShips(ship)){
|
||||
System.out.println("Placement du bateau de longueur "+i +" :\n");
|
||||
boolean valid = false;
|
||||
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");
|
||||
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")){
|
||||
ship.setCoords(new Pair<>(x, y));
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -12,15 +12,12 @@ 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();
|
||||
if(x > 9 ||x<0||y>9||y<0)
|
||||
int x, y;
|
||||
for(int i = 0; i < ship.getSize(); i++){
|
||||
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;
|
||||
}
|
||||
this.ships.add(ship);
|
||||
@ -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();
|
||||
for(int i = 1; i <= ship.getSize(); i++){
|
||||
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,20 +48,21 @@ 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;
|
||||
}
|
||||
|
||||
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<>();
|
||||
for(Integer i = 0; i<10;i++){
|
||||
for(Integer y = 0;y<10;y++){
|
||||
@ -75,7 +74,7 @@ public abstract class Player {
|
||||
return validMovesList;
|
||||
|
||||
}
|
||||
public void setId(int i ){
|
||||
public void setId(int i){
|
||||
id = i;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user