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,14 +29,18 @@ 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;
@ -48,8 +52,10 @@ public abstract class AbstractView implements View {
chain += " X"; chain += " X";
} else if (result == 2){ } else if (result == 2){
chain += " !"; chain += " !";
} else { } else if(debug || game.getCurrentPlayer() == player) {
chain += " ."; chain += " .";
} else {
chain += " _";
} }
break; break;
} }

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,6 +242,7 @@ 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()) {
if(player == game.getCurrentPlayer() ||ship.isDrown()) {
int x1 = i == 1 ? initialWidth : initialWidth * 13; int x1 = i == 1 ? initialWidth : initialWidth * 13;
int y1 = initialHeight * 2; int y1 = initialHeight * 2;
int shipWidth = initialWidth; int shipWidth = initialWidth;
@ -275,10 +276,9 @@ public class Window extends AbstractView {
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()) {