From e9dcf4a162af6af254f43e2f9896bbb11c09adc1 Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Sun, 13 Feb 2022 22:31:16 +0100 Subject: [PATCH 01/10] Add values and colors to board-String --- src/main/java/Gameboard.java | 62 ++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/src/main/java/Gameboard.java b/src/main/java/Gameboard.java index 5bb535b..d737a1d 100644 --- a/src/main/java/Gameboard.java +++ b/src/main/java/Gameboard.java @@ -2,13 +2,18 @@ import java.util.Arrays; public class Gameboard { int[] board; + static String RED = "\u001b[31;1m"; + static String BLUE = "\u001b[34;1m"; + static String YELLOW = "\u001b[33;1m"; + static String GREEN = "\u001b[32;1m"; + static String RESET = "\u001b[0m"; public Gameboard() { board = new int[56]; } public void initGameboard (){ - for(int i = 0; i < 40; i++) { + for(int i = 0; i < 56; i++) { if ( i % 10 == 0) { board[i] = 1; } @@ -21,9 +26,60 @@ public class Gameboard { } } - @Override + public String toString() { - return Arrays.toString(board); + String[] f = new String[72]; + Arrays.fill(f, " "); + return "" + + BLUE +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ YELLOW +"+---+ +---+\n" + + BLUE +"| "+ f[60] +" | | "+ f[61] +" |"+ 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 + + " +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" + + " | "+ f[16] +" | "+ YELLOW +" | "+ f[49] +" | "+ RESET +" | "+ f[22] +" |\n" + + " +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" + + " +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" + + " | "+ f[15] +" | "+ YELLOW +" | "+ f[50] +" | "+ RESET +" | "+ f[23] +" |\n" + + "+---+ +---+ +---+ +---+ +---+ "+ YELLOW +" +---+ "+ RESET +" +---+ +---+ +---+ +---+ +---+ \n" + + "| "+ f[10] +" | | "+ f[11] +" | | "+ f[12] +" | | "+ f[13] +" | | "+ f[14] +" | "+ YELLOW +" | "+ f[51] +" | "+ RESET +" | "+ f[24] +" | | "+ f[25] +" | | "+ f[26] +" | | "+ f[27] +" | | "+ f[28] +" | \n" + + "+---+ +---+ +---+ +---+ +---+ "+ YELLOW +" +---+ "+ RESET +" +---+ +---+ +---+ +---+ +---+ \n" + + "+---+ "+ BLUE +" +---+ +---+ +---+ +---+ "+ GREEN +"+---+ +---+ +---+ +---+ "+ RESET +" +---+ \n" + + "| "+ f[9] +" | "+ BLUE +" | "+ f[44] +" | | "+ f[45] +" | | "+ f[46] +" | | "+ f[47] +" | "+ GREEN +" | "+ f[55] +" | | "+ f[54] +" | | "+ f[53] +" | | "+ f[52] +" | "+ RESET +" | "+ f[29] +" | \n" + + "+---+ "+ BLUE +" +---+ +---+ +---+ +---+ "+ GREEN +"+---+ +---+ +---+ +---+ "+ RESET +" +---+ \n" + + "+---+ +---+ +---+ +---+ +---+ "+ RED +" +---+ "+ RESET +" +---+ +---+ +---+ +---+ +---+ \n" + + "| "+ f[8] +" | | "+ f[7] +" | | "+ f[6] +" | | "+ f[5] +" | | "+ f[4] +" | "+ RED +" | "+ f[43] +" | "+ RESET +" | "+ f[34] +" | | "+ f[33] +" | | "+ f[32] +" | | "+ f[31] +" | | "+ f[30] +" | \n" + + "+---+ +---+ +---+ +---+ +---+ "+ RED +" +---+ "+ RESET +" +---+ +---+ +---+ +---+ +---+ \n" + + " | "+ f[3] +" | "+ RED +" | "+ f[42] +" | "+ RESET +" | "+ f[35] +" |\n" + + " +---+ "+ RED +" +---+ "+ RESET +" +---+\n" + + " +---+ "+ RED +" +---+ "+ RESET +" +---+\n" + + " | "+ f[2] +" | "+ RED +" | "+ f[41] +" | "+ RESET +" | "+ f[36] +" |\n" + + " +---+ "+ RED +" +---+ "+ RESET +" +---+\n" + + RED +"+---+ +---+"+ RESET +" +---+ "+ RED +" +---+ "+ RESET +" +---+ "+ GREEN +"+---+ +---+\n" + + RED +"| "+ f[56] +" | | "+ f[57] +" |"+ RESET +" | "+ f[1] +" | "+ RED +" | "+ f[40] +" | "+ RESET +" | "+ f[37] +" | "+ GREEN +"| "+ f[68] +" | | "+ f[69] +" |\n" + + RED +"+---+ +---+"+ RESET +" +---+ "+ RED +" +---+ "+ RESET +" +---+ "+ GREEN +"+---+ +---+\n" + + RED +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ GREEN +"+---+ +---+\n" + + RED +"| "+ f[58] +" | | "+ f[59] +" |"+ RESET +" | "+ f[0] +" | | "+ f[39] +" | | "+ f[38] +" | "+ GREEN +"| "+ f[70] +" | | "+ f[71] +" |\n" + + RED +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ GREEN +"+---+ +---+\n" + RESET + + "Color Test\n" + + "" + RED + " RED " + BLUE + " BLUE " + GREEN + " GREEN " + YELLOW + " YELLOW " + RESET + " WHITE"; } + public static void main(String[] args) { + Game g = new Game(); + System.out.println(g.gb.toString()); + } } + +/* + 0-39 Spielfelder + Rot Ziel: 40,41,42,43 + Blau Ziel: 44,45,46,47 + Gelb Ziel: 48,49,50,51 + Grün Ziel: 52.53.54.55 + Rot Daneben: 56,57,58,59 + Blau Daneben: 60,61,62,63 + Gelb Daneben: 64.65.66.67 + Grün Daneben: 68.69,70,71 + */ \ No newline at end of file From 4139925d6327d14d7fc5f719970413cd58394c26 Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Sun, 13 Feb 2022 22:31:29 +0100 Subject: [PATCH 02/10] Refactor Method call --- src/main/java/Game.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/Game.java b/src/main/java/Game.java index 504800c..80abd17 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -13,6 +13,7 @@ public class Game { Player winner; while(true){ for (Player p : g.players) { + System.out.println(g.toString()); int c = 0; int dice; System.out.println("Spieler " + p.name + " an der Reihe."); @@ -35,6 +36,7 @@ public class Game { System.out.println("Spieler " + winner.name + " gewinnt!"); exit(42); } + //clear; } } } @@ -49,6 +51,11 @@ public class Game { players.add(new Player("Grün",30, 52, 29)); } + @Override + public String toString() { + return gb.toString(); + } + public boolean checkDice(int dice, Player p, int countRolls) { int figuresInBase = p.checkFigureInBase(p.figures); if(figuresInBase == 4) { From 2d27095edfee3c7fe068f1d2b346b539848a2593 Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 15 Feb 2022 09:50:49 +0100 Subject: [PATCH 03/10] 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"} + ) + ); + } + + } From bf9d29158b59d10926524b26b4be7ec27f2831d0 Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 15 Feb 2022 09:52:34 +0100 Subject: [PATCH 04/10] 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()); } } From 36596f67165003496a3a4e07cde32e01a6052d10 Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 15 Feb 2022 10:11:25 +0100 Subject: [PATCH 05/10] Fix Board-String --- src/main/java/Gameboard.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/Gameboard.java b/src/main/java/Gameboard.java index 8c33339..97dc8ac 100644 --- a/src/main/java/Gameboard.java +++ b/src/main/java/Gameboard.java @@ -42,6 +42,7 @@ public class Gameboard { " +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" + " +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" + " | "+ f[15] +" | "+ YELLOW +" | "+ f[50] +" | "+ RESET +" | "+ f[23] +" |\n" + + " +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" + "+---+ +---+ +---+ +---+ +---+ "+ YELLOW +" +---+ "+ RESET +" +---+ +---+ +---+ +---+ +---+ \n" + "| "+ f[10] +" | | "+ f[11] +" | | "+ f[12] +" | | "+ f[13] +" | | "+ f[14] +" | "+ YELLOW +" | "+ f[51] +" | "+ RESET +" | "+ f[24] +" | | "+ f[25] +" | | "+ f[26] +" | | "+ f[27] +" | | "+ f[28] +" | \n" + "+---+ +---+ +---+ +---+ +---+ "+ YELLOW +" +---+ "+ RESET +" +---+ +---+ +---+ +---+ +---+ \n" + @@ -51,6 +52,7 @@ public class Gameboard { "+---+ +---+ +---+ +---+ +---+ "+ RED +" +---+ "+ RESET +" +---+ +---+ +---+ +---+ +---+ \n" + "| "+ f[8] +" | | "+ f[7] +" | | "+ f[6] +" | | "+ f[5] +" | | "+ f[4] +" | "+ RED +" | "+ f[43] +" | "+ RESET +" | "+ f[34] +" | | "+ f[33] +" | | "+ f[32] +" | | "+ f[31] +" | | "+ f[30] +" | \n" + "+---+ +---+ +---+ +---+ +---+ "+ RED +" +---+ "+ RESET +" +---+ +---+ +---+ +---+ +---+ \n" + + " +---+ "+ RED +" +---+ "+ RESET +" +---+\n" + " | "+ f[3] +" | "+ RED +" | "+ f[42] +" | "+ RESET +" | "+ f[35] +" |\n" + " +---+ "+ RED +" +---+ "+ RESET +" +---+\n" + " +---+ "+ RED +" +---+ "+ RESET +" +---+\n" + From f2ef9a7c81341d943f0923590408cd45db3fce42 Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 15 Feb 2022 10:20:31 +0100 Subject: [PATCH 06/10] Remove Color Test --- src/main/java/Gameboard.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/Gameboard.java b/src/main/java/Gameboard.java index 97dc8ac..429d0f7 100644 --- a/src/main/java/Gameboard.java +++ b/src/main/java/Gameboard.java @@ -63,9 +63,7 @@ public class Gameboard { RED +"+---+ +---+"+ RESET +" +---+ "+ RED +" +---+ "+ RESET +" +---+ "+ GREEN +"+---+ +---+\n" + RED +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ GREEN +"+---+ +---+\n" + RED +"| "+ f[58] +" | | "+ f[59] +" |"+ RESET +" | "+ f[0] +" | | "+ f[39] +" | | "+ f[38] +" | "+ GREEN +"| "+ f[70] +" | | "+ f[71] +" |\n" + - RED +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ GREEN +"+---+ +---+\n" + RESET + - "Color Test\n" + - "" + RED + " RED " + BLUE + " BLUE " + GREEN + " GREEN " + YELLOW + " YELLOW " + RESET + " WHITE"; + RED +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ GREEN +"+---+ +---+\n" + RESET; } public static void main(String[] args) { From 6f7e2cfe59a48d4b6687b1f24b1983ecf0b67a8b Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 15 Feb 2022 10:37:48 +0100 Subject: [PATCH 07/10] Edit Board String and add player --- src/main/java/Gameboard.java | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/main/java/Gameboard.java b/src/main/java/Gameboard.java index 429d0f7..4f571f1 100644 --- a/src/main/java/Gameboard.java +++ b/src/main/java/Gameboard.java @@ -28,47 +28,56 @@ public class Gameboard { } - public String printBoard(Game g) { + public String printBoard(Game g, Player p) { String[] f = getFiguresOnBoard(g); + String color = ""; + switch (p.name) { + case "Rot" -> color = RED; + case "Blau" -> color = BLUE; + case "Gelb" -> color = YELLOW; + case "Grün" -> color = GREEN; + } 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" + + BLUE +"| "+ f[60] + BLUE +" | | "+ f[61] + BLUE +" |"+ RESET +" | "+ f[18] +" | | "+ f[19] +" | | "+ f[20] +" | "+ YELLOW +"| "+ f[64] + YELLOW + " | | "+ f[65] + YELLOW +" |\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 +"| "+ f[62] + BLUE + " | | "+ f[63] + BLUE +" |"+ RESET +" | "+ f[17] +" | "+ YELLOW +" | "+ f[48] + YELLOW +" | "+ RESET +" | "+ f[21] +" | "+ YELLOW +"| "+ f[66] + YELLOW +" | | "+ f[67] + YELLOW +" |\n" + BLUE +"+---+ +---+"+ RESET +" +---+ "+ YELLOW +"+---+"+ RESET +" +---+ "+ YELLOW +"+---+ +---+\n" + RESET + " +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" + - " | "+ f[16] +" | "+ YELLOW +" | "+ f[49] +" | "+ RESET +" | "+ f[22] +" |\n" + + " | "+ f[16] +" | "+ YELLOW +" | "+ f[49] + YELLOW +" | "+ RESET +" | "+ f[22] +" |\n" + " +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" + " +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" + - " | "+ f[15] +" | "+ YELLOW +" | "+ f[50] +" | "+ RESET +" | "+ f[23] +" |\n" + + " | "+ f[15] +" | "+ YELLOW +" | "+ f[50] + YELLOW +" | "+ RESET +" | "+ f[23] +" |\n" + " +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" + "+---+ +---+ +---+ +---+ +---+ "+ YELLOW +" +---+ "+ RESET +" +---+ +---+ +---+ +---+ +---+ \n" + "| "+ f[10] +" | | "+ f[11] +" | | "+ f[12] +" | | "+ f[13] +" | | "+ f[14] +" | "+ YELLOW +" | "+ f[51] +" | "+ RESET +" | "+ f[24] +" | | "+ f[25] +" | | "+ f[26] +" | | "+ f[27] +" | | "+ f[28] +" | \n" + "+---+ +---+ +---+ +---+ +---+ "+ YELLOW +" +---+ "+ RESET +" +---+ +---+ +---+ +---+ +---+ \n" + "+---+ "+ BLUE +" +---+ +---+ +---+ +---+ "+ GREEN +"+---+ +---+ +---+ +---+ "+ RESET +" +---+ \n" + - "| "+ f[9] +" | "+ BLUE +" | "+ f[44] +" | | "+ f[45] +" | | "+ f[46] +" | | "+ f[47] +" | "+ GREEN +" | "+ f[55] +" | | "+ f[54] +" | | "+ f[53] +" | | "+ f[52] +" | "+ RESET +" | "+ f[29] +" | \n" + + "| "+ f[9] +" | "+ BLUE +" | "+ f[44] + BLUE +" | | "+ f[45] + BLUE + " | | "+ f[46] + BLUE + " | | "+ f[47] + BLUE +" | "+ GREEN +" | "+ f[55] + GREEN + " | | "+ f[54] + GREEN +" | | "+ f[53] + GREEN +" | | "+ f[52] + GREEN +" | "+ RESET +" | "+ f[29] +" | \n" + "+---+ "+ BLUE +" +---+ +---+ +---+ +---+ "+ GREEN +"+---+ +---+ +---+ +---+ "+ RESET +" +---+ \n" + "+---+ +---+ +---+ +---+ +---+ "+ RED +" +---+ "+ RESET +" +---+ +---+ +---+ +---+ +---+ \n" + - "| "+ f[8] +" | | "+ f[7] +" | | "+ f[6] +" | | "+ f[5] +" | | "+ f[4] +" | "+ RED +" | "+ f[43] +" | "+ RESET +" | "+ f[34] +" | | "+ f[33] +" | | "+ f[32] +" | | "+ f[31] +" | | "+ f[30] +" | \n" + + "| "+ f[8] +" | | "+ f[7] +" | | "+ f[6] +" | | "+ f[5] +" | | "+ f[4] +" | "+ RED +" | "+ f[43] + RED +" | "+ RESET +" | "+ f[34] +" | | "+ f[33] +" | | "+ f[32] +" | | "+ f[31] +" | | "+ f[30] +" | \n" + "+---+ +---+ +---+ +---+ +---+ "+ RED +" +---+ "+ RESET +" +---+ +---+ +---+ +---+ +---+ \n" + " +---+ "+ RED +" +---+ "+ RESET +" +---+\n" + - " | "+ f[3] +" | "+ RED +" | "+ f[42] +" | "+ RESET +" | "+ f[35] +" |\n" + + " | "+ f[3] +" | "+ RED +" | "+ f[42] + RED +" | "+ RESET +" | "+ f[35] +" |\n" + " +---+ "+ RED +" +---+ "+ RESET +" +---+\n" + " +---+ "+ RED +" +---+ "+ RESET +" +---+\n" + - " | "+ f[2] +" | "+ RED +" | "+ f[41] +" | "+ RESET +" | "+ f[36] +" |\n" + + " | "+ f[2] +" | "+ RED +" | "+ f[41] + RED +" | "+ RESET +" | "+ f[36] +" |\n" + " +---+ "+ RED +" +---+ "+ RESET +" +---+\n" + RED +"+---+ +---+"+ RESET +" +---+ "+ RED +" +---+ "+ RESET +" +---+ "+ GREEN +"+---+ +---+\n" + - RED +"| "+ f[56] +" | | "+ f[57] +" |"+ RESET +" | "+ f[1] +" | "+ RED +" | "+ f[40] +" | "+ RESET +" | "+ f[37] +" | "+ GREEN +"| "+ f[68] +" | | "+ f[69] +" |\n" + + RED +"| "+ f[56] + RED +" | | "+ f[57] + RED +" |"+ RESET +" | "+ f[1] +" | "+ RED +" | "+ f[40] + RED +" | "+ RESET +" | "+ f[37] +" | "+ GREEN +"| "+ f[68] + GREEN +" | | "+ f[69] + GREEN +" |\n" + RED +"+---+ +---+"+ RESET +" +---+ "+ RED +" +---+ "+ RESET +" +---+ "+ GREEN +"+---+ +---+\n" + RED +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ GREEN +"+---+ +---+\n" + - RED +"| "+ f[58] +" | | "+ f[59] +" |"+ RESET +" | "+ f[0] +" | | "+ f[39] +" | | "+ f[38] +" | "+ GREEN +"| "+ f[70] +" | | "+ f[71] +" |\n" + - RED +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ GREEN +"+---+ +---+\n" + RESET; + RED +"| "+ f[58] + RED +" | | "+ f[59] + RED +" |"+ RESET +" | "+ f[0] +" | | "+ f[39] +" | | "+ f[38] +" | "+ GREEN +"| "+ f[70] + GREEN +" | | "+ f[71] + GREEN +" |\n" + + RED +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ GREEN +"+---+ +---+\n" + RESET + + "===========================================================================\n" + + "Spieler " + color + p.name + RESET + " ist an der Reihe"; } public static void main(String[] args) { Game g = new Game(); - System.out.println(g.gb.printBoard(g)); + System.out.println(g.gb.printBoard(g, g.players.get(0))); } public String[] getFiguresOnBoard(Game g) { From 2f8f41b04b230d327ec7caf65c88335579f9110b Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 15 Feb 2022 10:41:45 +0100 Subject: [PATCH 08/10] Implement printGameboard into Gameloop --- src/main/java/Game.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/Game.java b/src/main/java/Game.java index 80abd17..3ebb4cb 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -1,8 +1,10 @@ +import java.io.IOException; import java.util.ArrayList; import java.util.concurrent.TimeUnit; import static java.lang.System.exit; + public class Game { Gameboard gb; @@ -13,10 +15,9 @@ public class Game { Player winner; while(true){ for (Player p : g.players) { - System.out.println(g.toString()); int c = 0; int dice; - System.out.println("Spieler " + p.name + " an der Reihe."); + System.out.println(g.printGameboard(g,p)); do { int figId; dice = p.rollDice(); @@ -29,6 +30,8 @@ public class Game { figId = p.choose(usableFigures); } while(figId == -1); g.setFigure(figId, dice, p, g); + clearScreen(); + System.out.println(g.printGameboard(g,p)); } } while (g.checkDice(dice, p, c)); if(p.checkGameWin(p.figures)) { @@ -36,7 +39,7 @@ public class Game { System.out.println("Spieler " + winner.name + " gewinnt!"); exit(42); } - //clear; + clearScreen(); } } } @@ -51,9 +54,13 @@ public class Game { players.add(new Player("Grün",30, 52, 29)); } - @Override - public String toString() { - return gb.toString(); + public String printGameboard(Game g, Player p) { + return gb.printBoard(g, p); + } + + public static void clearScreen() { + System.out.print("\033[H\033[2J"); + System.out.flush(); } public boolean checkDice(int dice, Player p, int countRolls) { From 63ec77cf5c94070b91053cebf6f1e1f1c0890c09 Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 15 Feb 2022 11:00:12 +0100 Subject: [PATCH 09/10] Change Output of choose method to match gameboard --- src/main/java/Player.java | 19 ++++++++++++++----- src/test/java/PlayerTest.java | 20 ++++++++++---------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/main/java/Player.java b/src/main/java/Player.java index fd04ee1..647ac5e 100644 --- a/src/main/java/Player.java +++ b/src/main/java/Player.java @@ -50,19 +50,28 @@ public class Player { } public int choose(ArrayList usableFigures) { + StringBuilder out = new StringBuilder("["); + Iterator it = usableFigures.iterator(); + for(int i = 0; i Collections.max(usableFigures) || input < Collections.min(usableFigures)) { + if (input > Collections.max(usableFigures) + 1 || input < Collections.min(usableFigures) + 1) { System.out.println("Die eingegebene Zahl war zu groß oder zu klein.\n" + - "Bitte nur Zahlen von " + Collections.min(usableFigures) + " bis " + Collections.max(usableFigures) + " eingeben."); + "Bitte nur Zahlen von " + (Collections.min(usableFigures) + 1) + " bis " + (Collections.max(usableFigures) + 1) + " eingeben."); return -1; } - return input; + return input - 1; } catch (Exception e) { System.out.println("Die Eingabe hat keine Zahl bekommen.\n" + - "Bitte nur Zahlen von " + Collections.min(usableFigures) + " bis " + Collections.max(usableFigures) + " eingeben."); + "Bitte nur Zahlen von " + (Collections.min(usableFigures) + 1) + " bis " + (Collections.max(usableFigures) + 1)+ " eingeben."); return -1; } } diff --git a/src/test/java/PlayerTest.java b/src/test/java/PlayerTest.java index 0a38a78..69a6892 100644 --- a/src/test/java/PlayerTest.java +++ b/src/test/java/PlayerTest.java @@ -174,16 +174,16 @@ public class PlayerTest { static Stream checkChooseMethodData() { return Stream.of( - Arguments.of("Figure 1 choosen", "1\n", new ArrayList<>(Arrays.asList(1,2,3,4)), 1), - Arguments.of("Figure 2 choosen", "2\n", new ArrayList<>(Arrays.asList(1,2,3,4)), 2), - Arguments.of("Figure 3 choosen", "3\n", new ArrayList<>(Arrays.asList(1,2,3,4)),3), - Arguments.of("Figure 4 choosen", "4\n", new ArrayList<>(Arrays.asList(1,2,3,4)),4), - Arguments.of("Index out of bounds choosen", "16\n",new ArrayList<>(Arrays.asList(1,2,3,4)), -1), - Arguments.of("Index out of bounds choosen", "-2\n",new ArrayList<>(Arrays.asList(1,2,3,4)), -1), - Arguments.of("Input a letter or char", "g\n",new ArrayList<>(Arrays.asList(1,2,3,4)), -1), - Arguments.of("Input a empty string", "\n",new ArrayList<>(Arrays.asList(1,2,3,4)), -1), - Arguments.of("Input a special character", "&\n",new ArrayList<>(Arrays.asList(1,2,3,4)), -1), - Arguments.of("Only one Figure available", "1\n", new ArrayList<>(List.of(1)),1), + Arguments.of("Figure 1 choosen", "1\n", new ArrayList<>(Arrays.asList(0,1,2,3)), 0), + Arguments.of("Figure 2 choosen", "2\n", new ArrayList<>(Arrays.asList(0,1,2,3)), 1), + Arguments.of("Figure 3 choosen", "3\n", new ArrayList<>(Arrays.asList(0,1,2,3)),2), + Arguments.of("Figure 4 choosen", "4\n", new ArrayList<>(Arrays.asList(0,1,2,3)),3), + Arguments.of("Index out of bounds choosen", "16\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1), + Arguments.of("Index out of bounds choosen", "-2\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1), + Arguments.of("Input a letter or char", "g\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1), + Arguments.of("Input a empty string", "\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1), + Arguments.of("Input a special character", "&\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1), + Arguments.of("Only one Figure available", "1\n", new ArrayList<>(List.of(0)), 0), Arguments.of("Figure not available", "4\n", new ArrayList<>(Arrays.asList(1,2)),-1) ); } From d2d5cfc13509d40413fef9cd837ceb57888ff3f0 Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 15 Feb 2022 11:12:53 +0100 Subject: [PATCH 10/10] Add exit to end mid-game --- src/main/java/Player.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/Player.java b/src/main/java/Player.java index 647ac5e..20357a0 100644 --- a/src/main/java/Player.java +++ b/src/main/java/Player.java @@ -63,6 +63,9 @@ public class Player { System.out.print("Wählen Sie eine Figur " + out + ": "); try{ int input = scanner.nextInt(); + if(input == -1) { + System.exit(0); + } if (input > Collections.max(usableFigures) + 1 || input < Collections.min(usableFigures) + 1) { System.out.println("Die eingegebene Zahl war zu groß oder zu klein.\n" + "Bitte nur Zahlen von " + (Collections.min(usableFigures) + 1) + " bis " + (Collections.max(usableFigures) + 1) + " eingeben.");