2021-03-23 10:37:51 +01:00
|
|
|
package battleship.model;
|
|
|
|
|
2021-04-10 15:05:02 +02:00
|
|
|
import battleship.model.player.Player;
|
2021-03-28 15:09:44 +02:00
|
|
|
import battleship.utils.Pair;
|
2021-04-10 15:05:02 +02:00
|
|
|
import battleship.utils.Triplet;
|
|
|
|
|
2021-03-23 10:37:51 +01:00
|
|
|
public class Ship {
|
|
|
|
|
2021-03-28 13:55:59 +02:00
|
|
|
private Pair<Integer, Integer> coords;
|
2021-03-27 13:12:33 +01:00
|
|
|
private final int size;
|
2021-04-10 15:05:02 +02:00
|
|
|
Pair<Integer,Integer>[] fullCoords;
|
2021-03-28 14:35:10 +02:00
|
|
|
private Direction direction; // (0,-1) bas // (0,1) haut // (1,0) droite // (-1,0) gauche
|
2021-03-27 14:56:01 +01:00
|
|
|
private boolean isDrown;
|
2021-03-23 10:37:51 +01:00
|
|
|
|
2021-03-28 14:35:10 +02:00
|
|
|
public Ship(Pair<Integer, Integer> coords, int size, Direction direction) {
|
2021-03-23 10:37:51 +01:00
|
|
|
this.coords = coords;
|
|
|
|
this.size = size;
|
2021-04-10 15:05:02 +02:00
|
|
|
this.fullCoords = new Pair[this.size];
|
2021-03-27 13:12:33 +01:00
|
|
|
this.direction = direction;
|
2021-03-27 14:56:01 +01:00
|
|
|
isDrown = false;
|
2021-04-10 15:05:02 +02:00
|
|
|
recalculateFullCoords();
|
2021-03-27 13:12:33 +01:00
|
|
|
}
|
2021-04-10 15:05:02 +02:00
|
|
|
|
2021-03-28 14:35:10 +02:00
|
|
|
public void setDirection(Direction d){
|
2021-03-28 13:55:59 +02:00
|
|
|
this.direction = d;
|
|
|
|
}
|
2021-04-10 15:05:02 +02:00
|
|
|
|
2021-03-28 13:55:59 +02:00
|
|
|
public void setCoords(Pair<Integer,Integer> c){
|
|
|
|
this.coords = c;
|
|
|
|
}
|
|
|
|
|
2021-03-27 14:56:01 +01:00
|
|
|
public void setDrown(){
|
|
|
|
isDrown = true;
|
2021-03-27 13:12:33 +01:00
|
|
|
}
|
2021-03-28 14:35:10 +02:00
|
|
|
|
2021-04-10 15:05:02 +02:00
|
|
|
public boolean isDrown(){
|
2021-03-27 13:12:33 +01:00
|
|
|
return isDrown;
|
|
|
|
}
|
2021-03-28 14:35:10 +02:00
|
|
|
|
2021-03-27 13:12:33 +01:00
|
|
|
public int getSize(){
|
|
|
|
return this.size;
|
|
|
|
}
|
2021-03-28 14:35:10 +02:00
|
|
|
|
|
|
|
public Direction getDirection(){
|
2021-03-27 13:12:33 +01:00
|
|
|
return this.direction;
|
|
|
|
}
|
2021-03-28 14:35:10 +02:00
|
|
|
|
2021-03-27 13:12:33 +01:00
|
|
|
public Pair<Integer,Integer> getCoords(){
|
|
|
|
return this.coords;
|
2021-03-23 10:37:51 +01:00
|
|
|
}
|
2021-03-28 14:35:10 +02:00
|
|
|
|
2021-04-10 15:05:02 +02:00
|
|
|
public void recalculateFullCoords() {
|
|
|
|
for(int i = 0; i < size; ++i){
|
|
|
|
fullCoords[i] = new Pair<>(coords.getLeft() + i * direction.getDirection().getLeft(),coords.getRight() + i * direction.getDirection().getRight());
|
2021-03-27 19:43:36 +01:00
|
|
|
}
|
2021-04-10 15:05:02 +02:00
|
|
|
}
|
2021-03-27 19:43:36 +01:00
|
|
|
|
2021-04-10 15:05:02 +02:00
|
|
|
public Pair<Integer, Integer>[] getFullCoords(){
|
|
|
|
return fullCoords;
|
2021-03-27 19:43:36 +01:00
|
|
|
}
|
2021-03-23 10:37:51 +01:00
|
|
|
|
2021-04-07 15:31:57 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return super.toString() + ", coords=" + coords.toString() + ", size=" + size + ", direction=" + direction.toString();
|
|
|
|
}
|
2021-04-10 15:05:02 +02:00
|
|
|
|
|
|
|
public void updateIsDrown(Player player) {
|
|
|
|
int cpt = 0;
|
|
|
|
for(Pair<Integer, Integer> coords : getFullCoords()) {
|
|
|
|
for(Triplet<Integer,Integer,Boolean> move : player.getMoves()) {
|
|
|
|
if(move.getRight() && move.getLeft().equals(coords.getLeft()) && move.getMiddle().equals(coords.getRight())){
|
|
|
|
++cpt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(cpt == getSize()) {
|
|
|
|
setDrown();
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 10:37:51 +01:00
|
|
|
}
|