displayBoard now hide opponents ships when playing

This commit is contained in:
Quentin Legot 2021-05-05 14:52:51 +02:00
parent 2ed5251f9b
commit 5bdc772f29
4 changed files with 61 additions and 53 deletions

View File

@ -88,7 +88,9 @@ public class Game {
public void Play(View view) { public void Play(View view) {
try { try {
view.setShips(players[0]); view.setShips(players[0]);
changeCurrentPlayer();
view.setShips(players[1]); view.setShips(players[1]);
changeCurrentPlayer();
Player winner = null; Player winner = null;
while (winner == null) { while (winner == null) {
System.out.println("Au tour du joueur " + currentPlayer.getId()); System.out.println("Au tour du joueur " + currentPlayer.getId());

View File

@ -29,38 +29,44 @@ public abstract class AbstractView implements View {
*/ */
@Override @Override
public String toString() { public String toString() {
return toString(true);
}
public String toString(boolean debug) {
String chain = ""; String chain = "";
for(int u = 0; u < 2; ++u) { for(int u = 0; u < 2; ++u) {
Player player = game.players[u]; Player player = game.players[u];
ArrayList<Ship> ships = game.players[u].getShips(); ArrayList<Ship> ships = game.players[u].getShips();
chain += "Joueur " + player.getId() + " :\n"; 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) { for(int x = 0; x < 10; ++x) {
chain += "|"; chain += x;
for(int y = 0; y < 10; ++y) { for(int y = 0; y < 10; ++y) {
Pair<Integer, Integer> pair = new Pair<>(x, y); Pair<Integer, Integer> pair = new Pair<>(x, y);
boolean isPosition = false; boolean isPosition = false;
for(Ship ship : ships) { for(Ship ship : ships) {
if(isShipPosition(ship, pair)) { if(isShipPosition(ship, pair)) {
isPosition = true; isPosition = true;
int result = isPositionDrowned(game.players[u == 0 ? 1 : 0], ship, pair); int result = isPositionDrowned(game.players[u == 0 ? 1 : 0], ship, pair);
if(result == 1) { if(result == 1) {
chain += " X"; chain += " X";
} else if (result == 2){ } else if (result == 2){
chain += " !"; chain += " !";
} else { } else if(debug || game.getCurrentPlayer() == player) {
chain += " ."; chain += " .";
}
break;
}
}
if(!isPosition) {
if(isPositionDrowned(game.players[u == 0 ? 1 : 0], pair) == 2) {
chain += " ?";
} else { } else {
chain += " _"; chain += " _";
} }
break;
} }
}
if(!isPosition) {
if(isPositionDrowned(game.players[u == 0 ? 1 : 0], pair) == 2) {
chain += " ?";
} else {
chain += " _";
}
}
} }
chain += " |\n"; chain += " |\n";

View File

@ -91,7 +91,7 @@ public class Terminal extends AbstractView {
*/ */
@Override @Override
public void displayBoard() { public void displayBoard() {
System.out.println(this); System.out.println(this.toString(false));
} }
/** /**

View File

@ -242,43 +242,43 @@ public class Window extends AbstractView {
for(int i = 1; i < 3; ++i) { for(int i = 1; i < 3; ++i) {
Player player = game.players[i-1]; Player player = game.players[i-1];
for(Ship ship : player.getShips()) { for(Ship ship : player.getShips()) {
int x1 = i == 1 ? initialWidth : initialWidth * 13; if(player == game.getCurrentPlayer() ||ship.isDrown()) {
int y1 = initialHeight * 2; int x1 = i == 1 ? initialWidth : initialWidth * 13;
int shipWidth = initialWidth; int y1 = initialHeight * 2;
int shipHeight = initialHeight; int shipWidth = initialWidth;
switch(ship.getDirection()) { int shipHeight = initialHeight;
case DOWN: switch(ship.getDirection()) {
x1 += initialWidth * ship.getCoords().getRight(); case DOWN:
y1 += initialHeight * ship.getCoords().getLeft(); x1 += initialWidth * ship.getCoords().getRight();
shipHeight = initialHeight * ship.getSize(); y1 += initialHeight * ship.getCoords().getLeft();
g.setColor(new Color(0, 255, 255)); shipHeight = initialHeight * ship.getSize();
break; g.setColor(new Color(0, 255, 255));
case UP: break;
x1 += initialWidth * ship.getCoords().getRight(); case UP:
shipHeight = initialHeight * ship.getSize(); x1 += initialWidth * ship.getCoords().getRight();
y1 += initialHeight * ship.getCoords().getLeft() - shipHeight + initialHeight; shipHeight = initialHeight * ship.getSize();
g.setColor(new Color(255, 255, 0)); y1 += initialHeight * ship.getCoords().getLeft() - shipHeight + initialHeight;
break; g.setColor(new Color(255, 255, 0));
case RIGHT: break;
x1 += initialWidth * ship.getCoords().getRight(); case RIGHT:
y1 += initialHeight * ship.getCoords().getLeft(); x1 += initialWidth * ship.getCoords().getRight();
shipWidth = initialWidth * ship.getSize(); y1 += initialHeight * ship.getCoords().getLeft();
g.setColor(new Color(0, 255, 0)); shipWidth = initialWidth * ship.getSize();
break; g.setColor(new Color(0, 255, 0));
case LEFT: break;
shipWidth = initialWidth * ship.getSize(); case LEFT:
x1 += initialWidth * ship.getCoords().getRight() - shipWidth + initialWidth; shipWidth = initialWidth * ship.getSize();
y1 += initialHeight * ship.getCoords().getLeft(); x1 += initialWidth * ship.getCoords().getRight() - shipWidth + initialWidth;
g.setColor(new Color(0, 0, 255)); y1 += initialHeight * ship.getCoords().getLeft();
break; 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) { for(int i = 1; i < 3; ++i) {
Player player = game.players[i-1]; Player player = game.players[i-1];
int halfBoxSizeWidth = initialWidth / 2;
int halfBoxSizeHeight = initialHeight / 2;
float rectangleSize = initialWidth / 4f; float rectangleSize = initialWidth / 4f;
int sqrt = (int) Math.sqrt(initialHeight * initialHeight + initialWidth * initialWidth) - 10; int sqrt = (int) Math.sqrt(initialHeight * initialHeight + initialWidth * initialWidth) - 10;
for(Triplet<Integer, Integer, Boolean> move : player.getMoves()) { for(Triplet<Integer, Integer, Boolean> move : player.getMoves()) {