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;
|
2021-04-10 12:42:15 +02:00
|
|
|
import battleship.view.AbstractView;
|
|
|
|
|
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-27 11:12:20 +01:00
|
|
|
|
2021-04-10 15:05:02 +02: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-04-06 11:41:11 +02:00
|
|
|
public Player getOtherPlayer() {
|
|
|
|
return this.currentPlayer == players[0] ? players[1] : players[0];
|
|
|
|
}
|
|
|
|
|
2021-04-29 19:34:13 +02:00
|
|
|
public Player getOtherPlayer(Player player) {
|
|
|
|
return this.currentPlayer == player ? getOtherPlayer() : currentPlayer;
|
|
|
|
}
|
|
|
|
|
2021-03-27 13:12:33 +01:00
|
|
|
public void changeCurrentPlayer(){
|
2021-04-07 16:47:59 +02:00
|
|
|
currentPlayer = getOtherPlayer();
|
2021-03-27 13:12:33 +01:00
|
|
|
}
|
2021-03-28 14:35:10 +02:00
|
|
|
|
2021-03-27 13:12:33 +01:00
|
|
|
public void checkDrownedShips(){
|
2021-04-07 16:47:59 +02:00
|
|
|
Player otherPlayer = getOtherPlayer();
|
2021-04-10 15:05:02 +02:00
|
|
|
for(Ship ship : otherPlayer.getShips()){
|
2021-03-28 14:35:10 +02:00
|
|
|
if(!ship.isDrown())
|
2021-04-10 15:05:02 +02:00
|
|
|
ship.updateIsDrown(currentPlayer);
|
2021-03-27 13:12:33 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-28 14:35:10 +02:00
|
|
|
|
2021-03-27 19:43:36 +01:00
|
|
|
public Player getWinner(){
|
2021-04-10 15:05:02 +02:00
|
|
|
Ship remainingShip = players[0].getShips().parallelStream().filter(ship -> !ship.isDrown()).findFirst().orElse(null);
|
2021-04-07 16:47:59 +02:00
|
|
|
if(remainingShip == null)
|
|
|
|
return players[1];
|
2021-04-10 15:05:02 +02:00
|
|
|
remainingShip = players[1].getShips().parallelStream().filter(ship -> !ship.isDrown()).findFirst().orElse(null);
|
2021-04-07 16:47:59 +02:00
|
|
|
if(remainingShip == null)
|
2021-03-27 19:43:36 +01:00
|
|
|
return players[1];
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
public void move(Pair<Integer,Integer> move){
|
|
|
|
boolean bool = false;
|
2021-04-07 16:47:59 +02:00
|
|
|
Player otherPlayer = getOtherPlayer();
|
2021-04-10 15:05:02 +02:00
|
|
|
for (Ship ship : otherPlayer.getShips()) {
|
|
|
|
for(Pair<Integer,Integer> coords : ship.getFullCoords()){
|
|
|
|
if ((coords.getRight().equals(move.getRight())) && (coords.getLeft().equals(move.getLeft()))) {
|
2021-03-27 19:43:36 +01:00
|
|
|
bool = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-04-10 15:05:02 +02:00
|
|
|
if(bool)
|
|
|
|
break;
|
2021-03-27 19:43:36 +01:00
|
|
|
}
|
2021-04-07 16:47:59 +02:00
|
|
|
currentPlayer.addMove(new Triplet<>(move, bool));
|
2021-03-27 19:43:36 +01:00
|
|
|
}
|
2021-03-28 13:55:59 +02:00
|
|
|
|
2021-04-29 19:34:13 +02:00
|
|
|
public void Play(AbstractView view) {
|
2021-04-27 14:50:47 +02:00
|
|
|
try {
|
|
|
|
view.setShips(players[0]);
|
|
|
|
view.setShips(players[1]);
|
2021-04-29 19:34:13 +02:00
|
|
|
Player winner = null;
|
|
|
|
while (winner == null) {
|
|
|
|
System.out.println("Au tour du joueur " + currentPlayer.getId());
|
|
|
|
view.displayBoard();
|
|
|
|
move(view.chooseMove(currentPlayer));
|
|
|
|
checkDrownedShips();
|
|
|
|
changeCurrentPlayer();
|
|
|
|
winner = getWinner();
|
|
|
|
}
|
|
|
|
view.displayWinner(winner);
|
2021-04-27 14:50:47 +02:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
System.out.println("Une erreur est survenue");
|
|
|
|
e.printStackTrace();
|
|
|
|
System.exit(1);
|
|
|
|
}
|
2021-03-27 13:12:33 +01:00
|
|
|
|
2021-04-29 19:34:13 +02:00
|
|
|
}
|
2021-03-27 11:12:20 +01:00
|
|
|
}
|