Fix Pair.equals & Player.setShips

This commit is contained in:
Quentin Legot 2021-04-07 15:31:57 +02:00
parent 3919fcfea8
commit f792e2ba7b
4 changed files with 13 additions and 11 deletions

View File

@ -56,4 +56,8 @@ public class Ship {
}
@Override
public String toString() {
return super.toString() + ", coords=" + coords.toString() + ", size=" + size + ", direction=" + direction.toString();
}
}

View File

@ -16,6 +16,8 @@ public abstract class Player {
protected final int[] bato = { 5, 4, 3, 3, 2};
public boolean setShips(Ship ship) {
if(ship.getDirection() == Direction.DEFAULT)
return false;
for(int i = 0; i < ship.getSize(); i++){
int x = ship.getCoords().getLeft() + i * ship.getDirection().getDirection().getLeft();
int y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight();

View File

@ -38,7 +38,7 @@ public class Pair<U, K> {
return false;
}
final Pair<?, ?> other = (Pair<?, ?>) obj;
return this.left.equals(other.getLeft()) && this.left.equals(other.getRight());
return this.left.equals(other.getLeft()) && this.right.equals(other.getRight());
}
@Override

View File

@ -32,11 +32,10 @@ public abstract class View {
ArrayList<Ship> ships = game.players[u].getShips();
chain += "Player " + (u + 1) + " :\n";
chain += "+ - - - - - - - - - - +\n";
for(int i = 0; i < 10;++i) {
for(int x = 0; x < 10; ++x) {
chain += "|";
for(int y = 0; y < 10; ++y) {
Pair<Integer, Integer> pair = new Pair<>(i, y);
if(!ships.isEmpty()) {
Pair<Integer, Integer> pair = new Pair<>(x, y);
boolean isPosition = false;
for(Ship ship : ships) {
if(isShipPosition(ship, pair)) {
@ -51,9 +50,6 @@ public abstract class View {
}
if(!isPosition)
chain += " _";
} else {
chain += " _";
}
}
chain += " |\n";
}
@ -63,11 +59,11 @@ public abstract class View {
return chain;
}
private boolean isShipPosition(Ship ship, Pair<Integer, Integer> pair) {
if(ship.getCoords().equals(pair))
private boolean isShipPosition(Ship ship, Pair<Integer, Integer> boardsCoords) {
if(ship.getCoords().equals(boardsCoords))
return true;
for(int a = 0; a < ship.getSize(); ++a) {
if(new Pair<>(ship.getCoords().getLeft() + a * ship.getDirection().getDirection().getLeft(), ship.getCoords().getRight() + a * ship.getDirection().getDirection().getRight()).equals(pair)) {
if(new Pair<>(ship.getCoords().getLeft() + a * ship.getDirection().getDirection().getLeft(), ship.getCoords().getRight() + a * ship.getDirection().getDirection().getRight()).equals(boardsCoords)) {
return true;
}
}