From f792e2ba7be772c4c4502bc6b1f4b2490f442cb8 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Wed, 7 Apr 2021 15:31:57 +0200 Subject: [PATCH] Fix Pair.equals & Player.setShips --- src/battleship/model/Ship.java | 4 ++++ src/battleship/model/player/Player.java | 2 ++ src/battleship/utils/Pair.java | 2 +- src/battleship/view/View.java | 16 ++++++---------- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/battleship/model/Ship.java b/src/battleship/model/Ship.java index ef6e7e5..45060e9 100644 --- a/src/battleship/model/Ship.java +++ b/src/battleship/model/Ship.java @@ -56,4 +56,8 @@ public class Ship { } + @Override + public String toString() { + return super.toString() + ", coords=" + coords.toString() + ", size=" + size + ", direction=" + direction.toString(); + } } diff --git a/src/battleship/model/player/Player.java b/src/battleship/model/player/Player.java index 3be161f..8cd2ca0 100644 --- a/src/battleship/model/player/Player.java +++ b/src/battleship/model/player/Player.java @@ -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(); diff --git a/src/battleship/utils/Pair.java b/src/battleship/utils/Pair.java index 73c963c..7f1e7f5 100644 --- a/src/battleship/utils/Pair.java +++ b/src/battleship/utils/Pair.java @@ -38,7 +38,7 @@ public class Pair { 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 diff --git a/src/battleship/view/View.java b/src/battleship/view/View.java index 15d46d5..c5be070 100644 --- a/src/battleship/view/View.java +++ b/src/battleship/view/View.java @@ -32,11 +32,10 @@ public abstract class View { ArrayList 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 pair = new Pair<>(i, y); - if(!ships.isEmpty()) { + for(int y = 0; y < 10; ++y) { + Pair 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 pair) { - if(ship.getCoords().equals(pair)) + private boolean isShipPosition(Ship ship, Pair 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; } }