From 5098d46e5c4b7cd97f4d27032e10af09cbef0da5 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 30 Mar 2021 10:33:02 +0200 Subject: [PATCH] fix View.toString() --- .gitignore | 4 ++++ src/battleship/view/View.java | 36 +++++++++++++++++------------------ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 455ed4c..c3cfb23 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ out/ .idea *.iml +.classpath +.project +.settings/ +/bin/ diff --git a/src/battleship/view/View.java b/src/battleship/view/View.java index 2d10ba7..4e96a36 100644 --- a/src/battleship/view/View.java +++ b/src/battleship/view/View.java @@ -5,6 +5,7 @@ import battleship.model.player.Player; import battleship.utils.Triplet; import java.util.ArrayList; +import java.util.concurrent.atomic.AtomicInteger; public abstract class View { @@ -23,25 +24,24 @@ public abstract class View { @Override public String toString() { ArrayList> moves = game.currentPlayer.getMoves(); - String chain = "A vous de joueur "+game.currentPlayer.toString()+ "\n+ A B C D E F G H I J +\n"; - for(int i = 0; i<10;i++){ - chain += i+1; - for(int y = 0;y<10;y++){ + String chain = "A vous de joueur "+game.currentPlayer.toString()+ "\n+ - - - - - - - - - - +\n"; + + for(AtomicInteger i = new AtomicInteger(0); i.get() < 10;i.incrementAndGet()) { + chain += "|"; + for(AtomicInteger y = new AtomicInteger(0);y.get() < 10; y.incrementAndGet()) { if(!moves.isEmpty()) { - for (Triplet ships : moves) { - if (i == ships.getLeft() && y == ships.getMiddle()) { - if(ships.getRight()) - chain += " !"; - else - chain += " ."; - - - }else - chain += " _"; - - } - }else - chain += " _"; + Triplet move = moves.stream().filter(p -> p.getLeft() == i.get() && p.getMiddle() == y.get()).findFirst().orElse(null); + if(move != null) { + if (move.getRight()) + chain += " !"; + else + chain += " ."; + } else { + chain += " _"; + } + }else { + chain += " _"; + } } chain += " |\n"; }