From 2d27095edfee3c7fe068f1d2b346b539848a2593 Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 15 Feb 2022 09:50:49 +0100 Subject: [PATCH] Implement method to get Figures on Gameboard --- src/main/java/Gameboard.java | 55 ++++++++++++++++++---- src/test/java/GameboardTest.java | 78 ++++++++++++++++++++++++++++---- 2 files changed, 115 insertions(+), 18 deletions(-) diff --git a/src/main/java/Gameboard.java b/src/main/java/Gameboard.java index d737a1d..ce17235 100644 --- a/src/main/java/Gameboard.java +++ b/src/main/java/Gameboard.java @@ -1,4 +1,5 @@ import java.util.Arrays; +import java.util.Iterator; public class Gameboard { int[] board; @@ -27,16 +28,16 @@ public class Gameboard { } - public String toString() { - String[] f = new String[72]; - Arrays.fill(f, " "); + public String printBoard(Game g) { + String[] f; + f = getFiguresOnBoard(g); return "" + BLUE +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ YELLOW +"+---+ +---+\n" + - BLUE +"| "+ f[60] +" | | "+ f[61] +" |"+ RESET +" | "+ f[18] +" | | "+ f[19] +" | | "+ f[20] +" | "+ YELLOW +"| "+ f[64] +" | | "+ f[65] +" |\n" + + BLUE +"| "+ f[60] + BLUE +" | | "+ f[61] + BLUE +" |"+ RESET +" | "+ f[18] +" | | "+ f[19] +" | | "+ f[20] +" | "+ YELLOW +"| "+ f[64] +" | | "+ f[65] +" |\n" + BLUE +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ YELLOW +"+---+ +---+\n" + - BLUE +"+---+ +---+"+ RESET +" +---+ "+ YELLOW +"+---+"+ RESET +" +---+ "+ YELLOW +"+---+ +---+\n" + - BLUE +"| "+ f[62] +" | | "+ f[63] +" |"+ RESET +" | "+ f[17] +" | "+ YELLOW +" | "+ f[48] +" | "+ RESET +" | "+ f[21] +" | "+ YELLOW +"| "+ f[66] +" | | "+ f[67] +" |\n" + - BLUE +"+---+ +---+"+ RESET +" +---+ "+ YELLOW +"+---+"+ RESET +" +---+ "+ YELLOW +"+---+ +---+\n" + RESET + + BLUE +"+---+ +---+"+ RESET +" +---+ "+ YELLOW +"+---+"+ RESET +" +---+ "+ YELLOW +"+---+ +---+\n" + + BLUE +"| "+ f[62] +" | | "+ f[63] +" |"+ RESET +" | "+ f[17] +" | "+ YELLOW +" | "+ f[48] +" | "+ RESET +" | "+ f[21] +" | "+ YELLOW +"| "+ f[66] +" | | "+ f[67] +" |\n" + + BLUE +"+---+ +---+"+ RESET +" +---+ "+ YELLOW +"+---+"+ RESET +" +---+ "+ YELLOW +"+---+ +---+\n" + RESET + " +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" + " | "+ f[16] +" | "+ YELLOW +" | "+ f[49] +" | "+ RESET +" | "+ f[22] +" |\n" + " +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" + @@ -68,7 +69,45 @@ public class Gameboard { public static void main(String[] args) { Game g = new Game(); - System.out.println(g.gb.toString()); + System.out.println(g.gb.printBoard(g)); + } + + 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(); + 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; + } + Iterator
figureIt = p.figures.iterator(); + int i = 1; + while (figureIt.hasNext()) { + Figure f = figureIt.next(); + if(f.getPosition() == -1) { + res[start++] = color + i + RESET; + i++; + } else { + start++; + res[f.getPosition()] = color + i + RESET; + i++; + } + } + } + return res; } } diff --git a/src/test/java/GameboardTest.java b/src/test/java/GameboardTest.java index 8d53fdb..f13ce0c 100644 --- a/src/test/java/GameboardTest.java +++ b/src/test/java/GameboardTest.java @@ -6,15 +6,21 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; import java.util.stream.Stream; public class GameboardTest { private Gameboard gb; + private Game g; @BeforeEach void setup() { gb = new Gameboard(); + g = new Game(); } @Test @@ -60,16 +66,68 @@ public class GameboardTest { ); } - @Test - void checkPrintGameboard() { - String expectedResult = "[1, 0, 0, 0, 0, 0, 0, 0, 0, 2, " + - "1, 0, 0, 0, 0, 0, 0, 0, 0, 2, " + - "1, 0, 0, 0, 0, 0, 0, 0, 0, 2, " + - "1, 0, 0, 0, 0, 0, 0, 0, 0, 2, " + - "3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]"; - gb.initGameboard(); - String calculatedResult = gb.toString(); - assertThat(calculatedResult).describedAs("Print Gameboard").isEqualTo(expectedResult); + @ParameterizedTest + @MethodSource("FiguresOnBoardData") + void checkGetFiguresOnBoard (String testname, ArrayList positions, String[] expectedResult) { + Iterator pIt = g.players.iterator(); + Iterator posIt = positions.iterator(); + while(pIt.hasNext()) { + Player p = pIt.next(); + Iterator
figureIt = p.figures.iterator(); + while (figureIt.hasNext()) { + Figure f = figureIt.next(); + f.setPosition(posIt.next()); + } + } + + String[] calculatedResult = g.gb.getFiguresOnBoard(g); + assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult); } + static Stream FiguresOnBoardData () { + return Stream.of( + Arguments.of( + "All Figures in Base", + new ArrayList<>(Arrays.asList( + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + )), + new String[] { + " "," "," "," "," "," "," "," "," "," ", + " "," "," "," "," "," "," "," "," "," ", + " "," "," "," "," "," "," "," "," "," ", + " "," "," "," "," "," "," "," "," "," ", + " "," "," "," "," "," "," "," "," "," ", + " "," "," "," "," "," ","\u001b[31;1m1\u001b[0m", "\u001b[31;1m2\u001b[0m","\u001b[31;1m3\u001b[0m","\u001b[31;1m4\u001b[0m", + "\u001b[34;1m1\u001b[0m","\u001b[34;1m2\u001b[0m","\u001b[34;1m3\u001b[0m","\u001b[34;1m4\u001b[0m","\u001b[33;1m1\u001b[0m","\u001b[33;1m2\u001b[0m","\u001b[33;1m3\u001b[0m","\u001b[33;1m4\u001b[0m","\u001b[32;1m1\u001b[0m","\u001b[32;1m2\u001b[0m", + "\u001b[32;1m3\u001b[0m","\u001b[32;1m4\u001b[0m"} + ), + Arguments.of( + "Figures on Board", + new ArrayList<>(Arrays.asList( + 0,1,3,5,-1,-1,-1,-1,10,12,16,-1,-1,-1,-1,-1 + )), + new String[] { + "\u001b[31;1m1\u001b[0m","\u001b[31;1m2\u001b[0m"," ","\u001b[31;1m3\u001b[0m", + " ","\u001b[31;1m4\u001b[0m"," "," ", + " "," ","\u001b[33;1m1\u001b[0m"," ", + "\u001b[33;1m2\u001b[0m"," "," "," ", + "\u001b[33;1m3\u001b[0m"," "," "," ", + " "," "," "," ", + " "," "," "," ", + " "," "," "," ", + " "," "," "," ", + " "," "," "," ", + " "," "," "," ", + " "," "," "," ", + " "," "," "," ", + " "," "," "," ", + " "," "," "," ", + "\u001b[34;1m1\u001b[0m","\u001b[34;1m2\u001b[0m","\u001b[34;1m3\u001b[0m","\u001b[34;1m4\u001b[0m", + " "," "," ","\u001b[33;1m4\u001b[0m", + "\u001b[32;1m1\u001b[0m","\u001b[32;1m2\u001b[0m","\u001b[32;1m3\u001b[0m","\u001b[32;1m4\u001b[0m"} + ) + ); + } + + }