2021-03-27 11:12:20 +01:00
|
|
|
package battleship.model;
|
|
|
|
|
2021-04-10 12:42:15 +02:00
|
|
|
import battleship.model.player.AbstractPlayer;
|
2021-03-27 11:12:20 +01:00
|
|
|
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;
|
|
|
|
|
|
|
|
import java.util.Random;
|
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 12:42:15 +02:00
|
|
|
public Game(AbstractPlayer[] 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-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 12:42:15 +02:00
|
|
|
for(Ship ship : currentPlayer.ships){
|
2021-03-28 14:35:10 +02:00
|
|
|
if(!ship.isDrown())
|
2021-04-07 16:47:59 +02:00
|
|
|
otherPlayer.updateIsDrown(ship);
|
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 12:42:15 +02:00
|
|
|
Ship remainingShip = players[0].ships.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 12:42:15 +02:00
|
|
|
remainingShip = players[1].ships.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 12:42:15 +02:00
|
|
|
for (Ship ship : otherPlayer.ships) {
|
2021-03-27 19:43:36 +01:00
|
|
|
for(Pair<Integer,Integer> pair : ship.getCoordsArray()){
|
|
|
|
if ((pair.getRight().equals(move.getRight())) && (pair.getLeft().equals(move.getLeft()))) {
|
|
|
|
bool = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-10 12:42:15 +02:00
|
|
|
public void Play(AbstractView view){
|
2021-04-06 09:55:02 +02:00
|
|
|
view.setShips(players[0]);
|
|
|
|
view.setShips(players[1]);
|
2021-04-07 16:47:59 +02:00
|
|
|
Player winner = null;
|
|
|
|
while(winner == null) {
|
|
|
|
view.displayBoard();
|
|
|
|
move(currentPlayer.chooseMove());
|
2021-03-27 19:43:36 +01:00
|
|
|
changeCurrentPlayer();
|
|
|
|
checkDrownedShips();
|
2021-04-07 16:47:59 +02:00
|
|
|
winner = getWinner();
|
2021-03-27 19:43:36 +01:00
|
|
|
}
|
2021-04-07 16:47:59 +02:00
|
|
|
view.displayWinner(winner);
|
2021-03-27 19:43:36 +01:00
|
|
|
}
|
2021-03-27 13:12:33 +01:00
|
|
|
|
|
|
|
|
2021-04-10 12:42:15 +02:00
|
|
|
public void placeShipRandomly(Player player) {
|
|
|
|
Random rand = new Random();
|
|
|
|
for(int i : player.ShipSize) {
|
|
|
|
Ship ship = new Ship(new Pair<>(-1, -1), i, Direction.DEFAULT);
|
|
|
|
while(!player.setShips(ship)) {
|
|
|
|
ship = new Ship(new Pair<>(rand.nextInt(10), rand.nextInt(10)), i, Direction.values()[rand.nextInt(Direction.values().length)]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-27 11:12:20 +01:00
|
|
|
}
|