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,15 +37,18 @@ 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 += "Player " + (u + 1) + " :\n";
chain += "+ - - - - - - - - - - +\n";
for(int i = 0; i < 10;++i) {
chain += "|"; chain += "|";
for(AtomicInteger y = new AtomicInteger(0);y.get() < 10; y.incrementAndGet()) { for(int y = 0;y < 10; ++y) {
if(!moves.isEmpty()) { if(!moves.isEmpty()) {
Triplet<Integer, Integer, Boolean> move = moves.stream().filter(p -> p.getLeft() == i.get() && p.getMiddle() == y.get()).findFirst().orElse(null); for(Triplet<Integer, Integer, Boolean> move : moves) {
if(move != null) { if(move.getLeft() == i && move.getMiddle() == y) {
if (move.getRight()) if (move.getRight())
chain += " !"; chain += " !";
else else
@ -53,6 +56,8 @@ public abstract class View {
} else { } else {
chain += " _"; chain += " _";
} }
break;
}
} else { } else {
chain += " _"; chain += " _";
} }
@ -60,6 +65,8 @@ public abstract class View {
chain += " |\n"; chain += " |\n";
} }
chain += "+ - - - - - - - - - - +\n"; chain += "+ - - - - - - - - - - +\n";
}
return chain; return chain;
} }