2021-03-27 11:12:20 +01:00
|
|
|
package battleship.model;
|
|
|
|
|
|
|
|
import battleship.model.player.Player;
|
2021-03-27 19:43:36 +01:00
|
|
|
import battleship.utils.Pair;
|
|
|
|
import battleship.utils.Triplet;
|
|
|
|
import battleship.view.View;
|
2021-03-27 11:12:20 +01:00
|
|
|
|
2021-03-28 13:55:59 +02:00
|
|
|
import java.util.Scanner;
|
|
|
|
|
2021-03-27 11:12:20 +01:00
|
|
|
public class Game {
|
|
|
|
|
|
|
|
public Player[] players;
|
2021-03-27 13:12:33 +01:00
|
|
|
public Player currentPlayer;
|
2021-03-28 14:35:10 +02:00
|
|
|
private final int[] ships = { 5, 4, 3, 3, 2};
|
2021-03-27 11:12:20 +01:00
|
|
|
|
2021-03-27 14:56:01 +01:00
|
|
|
public Game(Player[] players) {
|
2021-03-27 11:12:20 +01:00
|
|
|
this.players = players;
|
2021-03-27 19:43:36 +01:00
|
|
|
this.currentPlayer = players[0];
|
2021-03-28 13:55:59 +02:00
|
|
|
players[0].setId(1);
|
|
|
|
players[1].setId(2);
|
2021-03-27 11:12:20 +01:00
|
|
|
}
|
2021-03-27 13:12:33 +01:00
|
|
|
|
2021-03-28 13:55:59 +02:00
|
|
|
|
2021-03-27 13:12:33 +01:00
|
|
|
public Player getCurrentPlayer(){
|
|
|
|
return this.currentPlayer;
|
|
|
|
}
|
2021-03-28 14:35:10 +02:00
|
|
|
|
2021-03-27 13:12:33 +01:00
|
|
|
public void changeCurrentPlayer(){
|
|
|
|
currentPlayer = (currentPlayer == players[1])? players[0] : players[1];
|
|
|
|
}
|
2021-03-28 14:35:10 +02:00
|
|
|
|
2021-03-27 13:12:33 +01:00
|
|
|
public void checkDrownedShips(){
|
2021-03-27 14:56:01 +01:00
|
|
|
Player otherPlayer = (currentPlayer == players[1])? players[0] : players[1];
|
2021-03-27 13:12:33 +01:00
|
|
|
for(Ship ship : currentPlayer.getShips()){
|
2021-03-28 14:35:10 +02:00
|
|
|
if(!ship.isDrown())
|
2021-03-27 13:12:33 +01:00
|
|
|
otherPlayer.isItDrown(ship);
|
|
|
|
}
|
|
|
|
}
|
2021-03-28 14:35:10 +02:00
|
|
|
|
2021-03-27 19:43:36 +01:00
|
|
|
public Player getWinner(){
|
|
|
|
int cpt = 0;
|
|
|
|
for(Ship ship : players[0].getShips()){
|
2021-03-28 14:35:10 +02:00
|
|
|
if(!ship.isDrown())
|
2021-03-27 19:43:36 +01:00
|
|
|
break;
|
|
|
|
else
|
|
|
|
cpt ++;
|
|
|
|
}
|
|
|
|
if(cpt == 5)
|
|
|
|
return players[1];
|
|
|
|
cpt = 0;
|
|
|
|
for(Ship ship : players[1].getShips()){
|
2021-03-28 14:35:10 +02:00
|
|
|
if(!ship.isDrown())
|
2021-03-27 19:43:36 +01:00
|
|
|
break;
|
|
|
|
else
|
|
|
|
cpt ++;
|
|
|
|
}
|
|
|
|
if(cpt == 5)
|
|
|
|
return players[0];
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
public void move(Pair<Integer,Integer> move){
|
|
|
|
boolean bool = false;
|
|
|
|
Player player = (currentPlayer == players[1])? players[0] : players[1];
|
|
|
|
for (Ship ship : player.getShips()) {
|
|
|
|
for(Pair<Integer,Integer> pair : ship.getCoordsArray()){
|
|
|
|
if ((pair.getRight().equals(move.getRight())) && (pair.getLeft().equals(move.getLeft()))) {
|
|
|
|
bool = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
currentPlayer.addMove(new Triplet<>(move.getLeft(),move.getRight(),bool));
|
|
|
|
|
|
|
|
}
|
|
|
|
public void setShips(Player player){
|
2021-03-28 13:55:59 +02:00
|
|
|
Scanner scanner = new Scanner(System.in);
|
2021-03-28 14:35:10 +02:00
|
|
|
int x, y;
|
|
|
|
String dir;
|
2021-03-28 13:55:59 +02:00
|
|
|
for(int i : ships){
|
2021-03-28 14:35:10 +02:00
|
|
|
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());
|
2021-03-28 13:55:59 +02:00
|
|
|
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();
|
2021-03-28 14:35:10 +02:00
|
|
|
ship.setCoords(new Pair<>(x, y));
|
|
|
|
boolean validDirection = false;
|
|
|
|
while(!validDirection){
|
2021-03-28 13:55:59 +02:00
|
|
|
System.out.println("Veuillez indiquer la direction de placement de votre bateau (d droite, h haut, b bas, g gauche)");
|
2021-03-28 14:35:10 +02:00
|
|
|
dir = scanner.next().toUpperCase();
|
2021-03-28 13:55:59 +02:00
|
|
|
System.out.println(dir);
|
2021-03-28 14:35:10 +02:00
|
|
|
for(Direction direction : Direction.values()) {
|
|
|
|
if(direction.getKeyword() != null && direction.getKeyword().equals(dir)) {
|
|
|
|
ship.setDirection(direction);
|
|
|
|
validDirection = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-03-28 13:55:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-27 19:43:36 +01:00
|
|
|
}
|
|
|
|
public Player Play(View view){
|
|
|
|
setShips(players[0]);
|
|
|
|
setShips(players[1]);
|
|
|
|
while(getWinner() == null){
|
|
|
|
System.out.println(view);
|
|
|
|
Pair<Integer,Integer> move = currentPlayer.chooseMove();
|
|
|
|
move(move);
|
|
|
|
changeCurrentPlayer();
|
|
|
|
checkDrownedShips();
|
|
|
|
}
|
|
|
|
return getWinner();
|
|
|
|
}
|
2021-03-27 13:12:33 +01:00
|
|
|
|
|
|
|
|
2021-03-27 11:12:20 +01:00
|
|
|
}
|