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) {
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());

View File

@ -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<Ship> 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<Integer, Integer> 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";

View File

@ -91,7 +91,7 @@ public class Terminal extends AbstractView {
*/
@Override
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) {
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<Integer, Integer, Boolean> move : player.getMoves()) {