From 5bdc772f292ac2dda0e94850decda196dd01228f Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Wed, 5 May 2021 14:52:51 +0200 Subject: [PATCH] displayBoard now hide opponents ships when playing --- src/battleship/model/Game.java | 2 + src/battleship/view/AbstractView.java | 46 ++++++++++--------- src/battleship/view/Terminal.java | 2 +- src/battleship/view/Window.java | 64 +++++++++++++-------------- 4 files changed, 61 insertions(+), 53 deletions(-) diff --git a/src/battleship/model/Game.java b/src/battleship/model/Game.java index 80f1f95..17091cb 100644 --- a/src/battleship/model/Game.java +++ b/src/battleship/model/Game.java @@ -88,7 +88,9 @@ public class Game { public void Play(View view) { try { view.setShips(players[0]); + changeCurrentPlayer(); view.setShips(players[1]); + changeCurrentPlayer(); Player winner = null; while (winner == null) { System.out.println("Au tour du joueur " + currentPlayer.getId()); diff --git a/src/battleship/view/AbstractView.java b/src/battleship/view/AbstractView.java index 766be35..8923055 100644 --- a/src/battleship/view/AbstractView.java +++ b/src/battleship/view/AbstractView.java @@ -29,38 +29,44 @@ public abstract class AbstractView implements View { */ @Override public String toString() { + return toString(true); + } + + public String toString(boolean debug) { String chain = ""; for(int u = 0; u < 2; ++u) { Player player = game.players[u]; ArrayList ships = game.players[u].getShips(); chain += "Joueur " + player.getId() + " :\n"; - chain += "+ - - - - - - - - - - +\n"; + chain += "+ 0 1 2 3 4 5 6 7 8 9 +\n"; for(int x = 0; x < 10; ++x) { - chain += "|"; + chain += x; for(int y = 0; y < 10; ++y) { Pair pair = new Pair<>(x, y); - boolean isPosition = false; - for(Ship ship : ships) { - if(isShipPosition(ship, pair)) { - isPosition = true; - int result = isPositionDrowned(game.players[u == 0 ? 1 : 0], ship, pair); - if(result == 1) { - chain += " X"; - } else if (result == 2){ - chain += " !"; - } else { - chain += " ."; - } - break; - } - } - if(!isPosition) { - if(isPositionDrowned(game.players[u == 0 ? 1 : 0], pair) == 2) { - chain += " ?"; + boolean isPosition = false; + for(Ship ship : ships) { + if(isShipPosition(ship, pair)) { + isPosition = true; + int result = isPositionDrowned(game.players[u == 0 ? 1 : 0], ship, pair); + if(result == 1) { + chain += " X"; + } else if (result == 2){ + chain += " !"; + } else if(debug || game.getCurrentPlayer() == player) { + chain += " ."; } else { chain += " _"; } + break; } + } + if(!isPosition) { + if(isPositionDrowned(game.players[u == 0 ? 1 : 0], pair) == 2) { + chain += " ?"; + } else { + chain += " _"; + } + } } chain += " |\n"; diff --git a/src/battleship/view/Terminal.java b/src/battleship/view/Terminal.java index c200b3d..0bd6d81 100644 --- a/src/battleship/view/Terminal.java +++ b/src/battleship/view/Terminal.java @@ -91,7 +91,7 @@ public class Terminal extends AbstractView { */ @Override public void displayBoard() { - System.out.println(this); + System.out.println(this.toString(false)); } /** diff --git a/src/battleship/view/Window.java b/src/battleship/view/Window.java index 5febc70..b3b9b75 100644 --- a/src/battleship/view/Window.java +++ b/src/battleship/view/Window.java @@ -242,43 +242,43 @@ public class Window extends AbstractView { for(int i = 1; i < 3; ++i) { Player player = game.players[i-1]; for(Ship ship : player.getShips()) { - int x1 = i == 1 ? initialWidth : initialWidth * 13; - int y1 = initialHeight * 2; - int shipWidth = initialWidth; - int shipHeight = initialHeight; - switch(ship.getDirection()) { - case DOWN: - x1 += initialWidth * ship.getCoords().getRight(); - y1 += initialHeight * ship.getCoords().getLeft(); - shipHeight = initialHeight * ship.getSize(); - g.setColor(new Color(0, 255, 255)); - break; - case UP: - x1 += initialWidth * ship.getCoords().getRight(); - shipHeight = initialHeight * ship.getSize(); - y1 += initialHeight * ship.getCoords().getLeft() - shipHeight + initialHeight; - g.setColor(new Color(255, 255, 0)); - break; - case RIGHT: - x1 += initialWidth * ship.getCoords().getRight(); - y1 += initialHeight * ship.getCoords().getLeft(); - shipWidth = initialWidth * ship.getSize(); - g.setColor(new Color(0, 255, 0)); - break; - case LEFT: - shipWidth = initialWidth * ship.getSize(); - x1 += initialWidth * ship.getCoords().getRight() - shipWidth + initialWidth; - y1 += initialHeight * ship.getCoords().getLeft(); - g.setColor(new Color(0, 0, 255)); - break; + if(player == game.getCurrentPlayer() ||ship.isDrown()) { + int x1 = i == 1 ? initialWidth : initialWidth * 13; + int y1 = initialHeight * 2; + int shipWidth = initialWidth; + int shipHeight = initialHeight; + switch(ship.getDirection()) { + case DOWN: + x1 += initialWidth * ship.getCoords().getRight(); + y1 += initialHeight * ship.getCoords().getLeft(); + shipHeight = initialHeight * ship.getSize(); + g.setColor(new Color(0, 255, 255)); + break; + case UP: + x1 += initialWidth * ship.getCoords().getRight(); + shipHeight = initialHeight * ship.getSize(); + y1 += initialHeight * ship.getCoords().getLeft() - shipHeight + initialHeight; + g.setColor(new Color(255, 255, 0)); + break; + case RIGHT: + x1 += initialWidth * ship.getCoords().getRight(); + y1 += initialHeight * ship.getCoords().getLeft(); + shipWidth = initialWidth * ship.getSize(); + g.setColor(new Color(0, 255, 0)); + break; + case LEFT: + shipWidth = initialWidth * ship.getSize(); + x1 += initialWidth * ship.getCoords().getRight() - shipWidth + initialWidth; + y1 += initialHeight * ship.getCoords().getLeft(); + g.setColor(new Color(0, 0, 255)); + break; + } + g2d.fillRoundRect(x1 + 1, y1 + 1, shipWidth - 1, shipHeight - 1, 25, 25); } - g2d.fillRoundRect(x1 + 1, y1 + 1, shipWidth - 1, shipHeight - 1, 25, 25); } } for(int i = 1; i < 3; ++i) { Player player = game.players[i-1]; - int halfBoxSizeWidth = initialWidth / 2; - int halfBoxSizeHeight = initialHeight / 2; float rectangleSize = initialWidth / 4f; int sqrt = (int) Math.sqrt(initialHeight * initialHeight + initialWidth * initialWidth) - 10; for(Triplet move : player.getMoves()) {