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){
view.setShips(players[0]);
view.setShips(players[1]);
System.out.println(view.toString());
while(getWinner() == null) {
System.out.println(view);
Pair<Integer,Integer> move = currentPlayer.chooseMove();

View File

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