Improved View.toString

This commit is contained in:
Quentin Legot 2021-04-06 10:19:56 +02:00
parent f2607f1268
commit 7fc9fb462c
2 changed files with 29 additions and 21 deletions

View File

@ -75,6 +75,7 @@ public class Game {
public Player Play(View view){ public Player Play(View view){
view.setShips(players[0]); view.setShips(players[0]);
view.setShips(players[1]); view.setShips(players[1]);
System.out.println(view.toString());
while(getWinner() == null) { while(getWinner() == null) {
System.out.println(view); System.out.println(view);
Pair<Integer,Integer> move = currentPlayer.chooseMove(); Pair<Integer,Integer> move = currentPlayer.chooseMove();

View File

@ -25,7 +25,7 @@ public abstract class View {
public abstract void displayBoard(); public abstract void displayBoard();
protected void placeShipRandomly(Player player) { public void placeShipRandomly(Player player) {
Random rand = new Random(); Random rand = new Random();
for(int i : ships) { for(int i : ships) {
Ship ship = null; Ship ship = null;
@ -37,29 +37,36 @@ public abstract class View {
@Override @Override
public String toString() { public String toString() {
ArrayList<Triplet<Integer,Integer,Boolean>> moves = game.currentPlayer.getMoves(); // String chain = "A vous de joueur "+game.currentPlayer.toString()+ "\n+ - - - - - - - - - - +\n";
String chain = "A vous de joueur "+game.currentPlayer.toString()+ "\n+ - - - - - - - - - - +\n"; String chain = "";
for(int u = 0; u < 2; ++u) {
for(AtomicInteger i = new AtomicInteger(0); i.get() < 10;i.incrementAndGet()) { ArrayList<Triplet<Integer,Integer,Boolean>> moves = game.players[u].getMoves();
chain += "|"; chain += "Player " + (u + 1) + " :\n";
for(AtomicInteger y = new AtomicInteger(0);y.get() < 10; y.incrementAndGet()) { chain += "+ - - - - - - - - - - +\n";
if(!moves.isEmpty()) { for(int i = 0; i < 10;++i) {
Triplet<Integer, Integer, Boolean> move = moves.stream().filter(p -> p.getLeft() == i.get() && p.getMiddle() == y.get()).findFirst().orElse(null); chain += "|";
if(move != null) { for(int y = 0;y < 10; ++y) {
if (move.getRight()) if(!moves.isEmpty()) {
chain += " !"; for(Triplet<Integer, Integer, Boolean> move : moves) {
else if(move.getLeft() == i && move.getMiddle() == y) {
chain += " ."; if (move.getRight())
} else { chain += " !";
chain += " _"; else
} chain += " .";
}else { } else {
chain += " _"; chain += " _";
}
break;
}
} else {
chain += " _";
}
} }
chain += " |\n";
} }
chain += " |\n"; chain += "+ - - - - - - - - - - +\n";
} }
chain += "+ - - - - - - - - - - +\n";
return chain; return chain;
} }