From bf9d29158b59d10926524b26b4be7ec27f2831d0 Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 15 Feb 2022 09:52:34 +0100 Subject: [PATCH] Refactoring --- src/main/java/Gameboard.java | 40 +++++++++++++++++--------------- src/test/java/GameboardTest.java | 7 +----- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/main/java/Gameboard.java b/src/main/java/Gameboard.java index ce17235..8c33339 100644 --- a/src/main/java/Gameboard.java +++ b/src/main/java/Gameboard.java @@ -29,8 +29,7 @@ public class Gameboard { public String printBoard(Game g) { - String[] f; - f = getFiguresOnBoard(g); + String[] f = getFiguresOnBoard(g); return "" + BLUE +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ YELLOW +"+---+ +---+\n" + BLUE +"| "+ f[60] + BLUE +" | | "+ f[61] + BLUE +" |"+ RESET +" | "+ f[18] +" | | "+ f[19] +" | | "+ f[20] +" | "+ YELLOW +"| "+ f[64] +" | | "+ f[65] +" |\n" + @@ -75,29 +74,32 @@ public class Gameboard { public String[] getFiguresOnBoard(Game g) { String[] res = new String[72]; Arrays.fill(res, " "); - Iterator pIt = g.players.iterator(); - while(pIt.hasNext()) { - Player p = pIt.next(); - String color = new String(); + for (Player p : g.players) { + String color = ""; int start = 0; - if(p.name.equals("Rot")) { - color = RED; - start = 56; - } else if (p.name.equals("Blau")) { - color = BLUE; - start = 60; - } else if (p.name.equals("Gelb")) { - color = YELLOW; - start = 64; - } else if (p.name.equals("Grün")) { - color = GREEN; - start = 68; + switch (p.name) { + case "Rot" -> { + color = RED; + start = 56; + } + case "Blau" -> { + color = BLUE; + start = 60; + } + case "Gelb" -> { + color = YELLOW; + start = 64; + } + case "Grün" -> { + color = GREEN; + start = 68; + } } Iterator
figureIt = p.figures.iterator(); int i = 1; while (figureIt.hasNext()) { Figure f = figureIt.next(); - if(f.getPosition() == -1) { + if (f.getPosition() == -1) { res[start++] = color + i + RESET; i++; } else { diff --git a/src/test/java/GameboardTest.java b/src/test/java/GameboardTest.java index f13ce0c..0bacb50 100644 --- a/src/test/java/GameboardTest.java +++ b/src/test/java/GameboardTest.java @@ -34,9 +34,6 @@ public class GameboardTest { @Test void checkGameboardFilled() { int[] expectedGameboard = new int[56]; - for(int i = 0; i < expectedGameboard.length; i++) { - expectedGameboard[i] = 0; - } int [] givenGameboard = gb.board; assertThat(givenGameboard).describedAs("Initial Gameboard").isEqualTo(expectedGameboard); @@ -73,9 +70,7 @@ public class GameboardTest { Iterator posIt = positions.iterator(); while(pIt.hasNext()) { Player p = pIt.next(); - Iterator
figureIt = p.figures.iterator(); - while (figureIt.hasNext()) { - Figure f = figureIt.next(); + for (Figure f : p.figures) { f.setPosition(posIt.next()); } }