Fix Pair.equals & Player.setShips
This commit is contained in:
parent
3919fcfea8
commit
f792e2ba7b
@ -56,4 +56,8 @@ public class Ship {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return super.toString() + ", coords=" + coords.toString() + ", size=" + size + ", direction=" + direction.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,8 @@ public abstract class Player {
|
|||||||
protected final int[] bato = { 5, 4, 3, 3, 2};
|
protected final int[] bato = { 5, 4, 3, 3, 2};
|
||||||
|
|
||||||
public boolean setShips(Ship ship) {
|
public boolean setShips(Ship ship) {
|
||||||
|
if(ship.getDirection() == Direction.DEFAULT)
|
||||||
|
return false;
|
||||||
for(int i = 0; i < ship.getSize(); i++){
|
for(int i = 0; i < ship.getSize(); i++){
|
||||||
int x = ship.getCoords().getLeft() + i * ship.getDirection().getDirection().getLeft();
|
int x = ship.getCoords().getLeft() + i * ship.getDirection().getDirection().getLeft();
|
||||||
int y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight();
|
int y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight();
|
||||||
|
@ -38,7 +38,7 @@ public class Pair<U, K> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
final Pair<?, ?> other = (Pair<?, ?>) obj;
|
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
|
@Override
|
||||||
|
@ -32,11 +32,10 @@ public abstract class View {
|
|||||||
ArrayList<Ship> ships = game.players[u].getShips();
|
ArrayList<Ship> ships = game.players[u].getShips();
|
||||||
chain += "Player " + (u + 1) + " :\n";
|
chain += "Player " + (u + 1) + " :\n";
|
||||||
chain += "+ - - - - - - - - - - +\n";
|
chain += "+ - - - - - - - - - - +\n";
|
||||||
for(int i = 0; i < 10;++i) {
|
for(int x = 0; x < 10; ++x) {
|
||||||
chain += "|";
|
chain += "|";
|
||||||
for(int y = 0; y < 10; ++y) {
|
for(int y = 0; y < 10; ++y) {
|
||||||
Pair<Integer, Integer> pair = new Pair<>(i, y);
|
Pair<Integer, Integer> pair = new Pair<>(x, y);
|
||||||
if(!ships.isEmpty()) {
|
|
||||||
boolean isPosition = false;
|
boolean isPosition = false;
|
||||||
for(Ship ship : ships) {
|
for(Ship ship : ships) {
|
||||||
if(isShipPosition(ship, pair)) {
|
if(isShipPosition(ship, pair)) {
|
||||||
@ -51,9 +50,6 @@ public abstract class View {
|
|||||||
}
|
}
|
||||||
if(!isPosition)
|
if(!isPosition)
|
||||||
chain += " _";
|
chain += " _";
|
||||||
} else {
|
|
||||||
chain += " _";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
chain += " |\n";
|
chain += " |\n";
|
||||||
}
|
}
|
||||||
@ -63,11 +59,11 @@ public abstract class View {
|
|||||||
return chain;
|
return chain;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isShipPosition(Ship ship, Pair<Integer, Integer> pair) {
|
private boolean isShipPosition(Ship ship, Pair<Integer, Integer> boardsCoords) {
|
||||||
if(ship.getCoords().equals(pair))
|
if(ship.getCoords().equals(boardsCoords))
|
||||||
return true;
|
return true;
|
||||||
for(int a = 0; a < ship.getSize(); ++a) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user