From 7175a51eda8ab5052cf644daed17a96f005d2179 Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Tue, 15 Feb 2022 11:58:30 +0100 Subject: [PATCH 01/10] Fix getUsableFigures for Home --- src/main/java/Game.java | 15 ++++++++++----- src/test/java/GameTest.java | 19 ++++++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/main/java/Game.java b/src/main/java/Game.java index 9e3704a..2b4b4b7 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -126,7 +126,7 @@ public class Game { if(p.checkFigureInBase(p.figures) > 0 && dice == 6){ if(g.checkFieldClear(p.startPos, p, g) == 2) { - return f.getPosition() == p.startPos; //TODO Wenn Figur auf Startpos von anderer Figur blockiert ist + return f.getPosition() == p.startPos; } return f.getPosition() == -1; } else if (f.getPosition() <= p.jumpToHome && (f.getPosition() + dice) > p.jumpToHome) { @@ -135,7 +135,9 @@ public class Game { } else { return false; } - } else if(f.getPosition() != -1) { + } else if(f.getPosition() >= p.startHome && (f.getPosition() + dice) < p.startHome + 4) { + return g.checkFieldClear(f.getPosition() + dice, p, g) == 0; + } else if(f.getPosition() != -1 && f.getPosition() < 40) { return g.checkFieldClear((f.getPosition() + dice) % 40, p, g) != 2; } return false; @@ -153,19 +155,22 @@ public class Game { int preCalculated; if(p.figures.get(figId).getPosition() == -1) { preCalculated = p.startPos; + } else if (p.figures.get(figId).getPosition() <= p.jumpToHome && (p.figures.get(figId).getPosition() + dice) > p.jumpToHome) { + System.out.println(p.startHome); + System.out.println(dice); + System.out.println(p.jumpToHome); + preCalculated = p.startHome + (dice - (p.jumpToHome - p.figures.get(figId).getPosition()) - 1); } else { preCalculated = (p.figures.get(figId).getPosition() + dice) % 40; } - int kicked = 0; for(Player currentPlayer : g.players) { for(Figure currentFigure : currentPlayer.figures) { if(currentFigure.getPosition() == preCalculated) { currentFigure.setPosition(-1); - kicked = 1; } } } p.figures.get(figId).setPosition(preCalculated); - return kicked; + return preCalculated; } } \ No newline at end of file diff --git a/src/test/java/GameTest.java b/src/test/java/GameTest.java index 27d63dc..2b0c43d 100644 --- a/src/test/java/GameTest.java +++ b/src/test/java/GameTest.java @@ -24,9 +24,6 @@ public class GameTest { g2 = new Game(); p2 = g2.players.get(0); - p2.figures.get(0).setPosition(5); - p2.figures.get(1).setPosition(10); - g2.players.get(3).figures.get(1).setPosition(14); } @ParameterizedTest @@ -162,21 +159,29 @@ public class GameTest { 6, Arrays.asList(-1, -1, -1, 0), new ArrayList<>(List.of(3)) + ), + Arguments.of( //Würfel 6 - 1 Figur auf dem Spielfeld - StartFeld besetzt + "Figur auf Startfeld", + 5, + Arrays.asList(-1, 41, -1, 0), + new ArrayList<>(List.of(3)) ) ); } @ParameterizedTest @MethodSource("setFigureData") - void setFigureTest(String testname, int figId, int dice, int expectedResult) { - int calculatedResult = g.setFigure(figId, dice, p2, g2); + void setFigureTest(String testname, int figId, int dice, int currentPosition, int expectedResult) { + p2.figures.get(figId).setPosition(currentPosition); + int calculatedResult = g2.setFigure(figId, dice, p2, g2); assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult); } static Stream setFigureData () { return Stream.of( - Arguments.of("Figur wird auf Feld gesetzt - Niemand gekicked", 0, 4, 0), - Arguments.of("Figur wird auf Feld gesetzt - Jemand gekicked", 1, 4, 1) + Arguments.of("Figur wird auf Feld gesetzt - Niemand gekicked", 0, 4, 5, 9), + Arguments.of("Figur wird auf Feld gesetzt - Jemand gekicked", 1, 4, 10, 14), + Arguments.of("Figur wird ins Haus gesetzt", 3, 4, 38, 42) ); } } From 404260a23391f4d1ad8a1ac4b8405e93f4e93073 Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Tue, 15 Feb 2022 12:05:19 +0100 Subject: [PATCH 02/10] Fix Gameboard in Gameloop --- src/main/java/Game.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/Game.java b/src/main/java/Game.java index 2b4b4b7..b9615d1 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -15,7 +15,7 @@ public class Game { for (Player p : g.players) { 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(); @@ -28,6 +28,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)) { @@ -35,6 +37,7 @@ public class Game { System.out.println("Spieler " + winner.name + " gewinnt!"); exit(42); } + clearScreen(); } } } From 975909dacba9efcac10e942372e059ec2fd08fc4 Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Tue, 15 Feb 2022 12:06:37 +0100 Subject: [PATCH 03/10] Add clearScreen at beginning --- src/main/java/Game.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/Game.java b/src/main/java/Game.java index b9615d1..8461484 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -11,6 +11,7 @@ public class Game { public static void main(String[] args) throws InterruptedException { Game g = new Game(); Player winner; + clearScreen(); while(true){ for (Player p : g.players) { int c = 0; From cc248b514ac464762dddf8b6a551cf28d64fb64d Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Tue, 15 Feb 2022 12:23:00 +0100 Subject: [PATCH 04/10] Add 1 Second Delay when Player swap --- src/main/java/Game.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/Game.java b/src/main/java/Game.java index 8461484..55f103b 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -39,6 +39,7 @@ public class Game { exit(42); } clearScreen(); + TimeUnit.SECONDS.sleep(1L); } } } From 81fb0fda4e8e2f4a118f114f3953263279fde467 Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 15 Feb 2022 12:35:51 +0100 Subject: [PATCH 05/10] Fix moving in the house --- src/main/java/Game.java | 7 +++++-- src/test/java/GameTest.java | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/Game.java b/src/main/java/Game.java index 8461484..5f7a1b1 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -38,6 +38,7 @@ public class Game { System.out.println("Spieler " + winner.name + " gewinnt!"); exit(42); } + TimeUnit.SECONDS.sleep(1L); clearScreen(); } } @@ -157,14 +158,16 @@ public class Game { */ public int setFigure(int figId, int dice, Player p, Game g) { int preCalculated; - if(p.figures.get(figId).getPosition() == -1) { + if (p.figures.get(figId).getPosition() == -1) { preCalculated = p.startPos; } else if (p.figures.get(figId).getPosition() <= p.jumpToHome && (p.figures.get(figId).getPosition() + dice) > p.jumpToHome) { System.out.println(p.startHome); System.out.println(dice); System.out.println(p.jumpToHome); preCalculated = p.startHome + (dice - (p.jumpToHome - p.figures.get(figId).getPosition()) - 1); - } else { + } else if (p.figures.get(figId).getPosition() >= p.startHome) { + preCalculated = p.figures.get(figId).getPosition() + dice; + }else { preCalculated = (p.figures.get(figId).getPosition() + dice) % 40; } for(Player currentPlayer : g.players) { diff --git a/src/test/java/GameTest.java b/src/test/java/GameTest.java index 2b0c43d..bf1f02b 100644 --- a/src/test/java/GameTest.java +++ b/src/test/java/GameTest.java @@ -181,7 +181,8 @@ public class GameTest { return Stream.of( Arguments.of("Figur wird auf Feld gesetzt - Niemand gekicked", 0, 4, 5, 9), Arguments.of("Figur wird auf Feld gesetzt - Jemand gekicked", 1, 4, 10, 14), - Arguments.of("Figur wird ins Haus gesetzt", 3, 4, 38, 42) + Arguments.of("Figur wird ins Haus gesetzt", 3, 4, 38, 42), + Arguments.of("Figur wird im Haus gesetzt", 3, 3, 41, 44) ); } } From f8c9d023e021769912f095ba0b7e82e8b79bc40f Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 15 Feb 2022 12:36:41 +0100 Subject: [PATCH 06/10] Fix Gametest for only 3 dice rolls --- src/test/java/GameTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/GameTest.java b/src/test/java/GameTest.java index bf1f02b..2ccc2e4 100644 --- a/src/test/java/GameTest.java +++ b/src/test/java/GameTest.java @@ -56,7 +56,7 @@ public class GameTest { Arrays.asList(-1, -1, -1, -1), 1, 3, - true + false ), Arguments.of("No figures on Field - d: 1 - c: 4", Arrays.asList(-1, -1, -1, -1), From 54b65e717e21ac8a0847a38e0c90fb88dfbb21fa Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 15 Feb 2022 12:39:45 +0100 Subject: [PATCH 07/10] Remove sout's --- src/main/java/Game.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/Game.java b/src/main/java/Game.java index 07267a3..c920527 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -162,9 +162,6 @@ public class Game { if (p.figures.get(figId).getPosition() == -1) { preCalculated = p.startPos; } else if (p.figures.get(figId).getPosition() <= p.jumpToHome && (p.figures.get(figId).getPosition() + dice) > p.jumpToHome) { - System.out.println(p.startHome); - System.out.println(dice); - System.out.println(p.jumpToHome); preCalculated = p.startHome + (dice - (p.jumpToHome - p.figures.get(figId).getPosition()) - 1); } else if (p.figures.get(figId).getPosition() >= p.startHome) { preCalculated = p.figures.get(figId).getPosition() + dice; From 8aceb8574cf75258271781d66af15c2f0f5976a5 Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 15 Feb 2022 12:41:04 +0100 Subject: [PATCH 08/10] Remove double sleep --- src/main/java/Game.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/Game.java b/src/main/java/Game.java index c920527..67b85a8 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -40,7 +40,6 @@ public class Game { } TimeUnit.SECONDS.sleep(1L); clearScreen(); - TimeUnit.SECONDS.sleep(1L); } } } From 6643e1d08e60d8b0796ef7d703fec77ec8dc5a9b Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 15 Feb 2022 13:04:38 +0100 Subject: [PATCH 09/10] Change condition of checkDice --- src/main/java/Game.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/Game.java b/src/main/java/Game.java index 9e3704a..6d843da 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -66,8 +66,14 @@ public class Game { * @return true if Player can roll the dice another time */ public boolean checkDice(int dice, Player p, int countRolls) { - int figuresInBase = p.checkFigureInBase(p.figures); - if(figuresInBase == 4) { + boolean figuresOnBoard = false; + for(Figure f : p.figures) { + if (f.getPosition() > -1 && f.getPosition() < 40) { + figuresOnBoard = true; + break; + } + } + if(!figuresOnBoard) { return countRolls < 3; } else return dice == 6; } From 4defdab7107faf31e3be785b3f27163f4f29aa13 Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 15 Feb 2022 23:35:27 +0100 Subject: [PATCH 10/10] Add 2d dice to Gameboard --- src/main/java/Game.java | 14 ++++--- src/main/java/Gameboard.java | 76 ++++++++++++++++++++++++++++++++---- 2 files changed, 78 insertions(+), 12 deletions(-) diff --git a/src/main/java/Game.java b/src/main/java/Game.java index 9e3704a..30a81e5 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -15,12 +15,14 @@ public class Game { for (Player p : g.players) { int c = 0; int dice; - System.out.println("Spieler " + p.name + " an der Reihe."); do { + clearScreen(); int figId; dice = p.rollDice(); - TimeUnit.SECONDS.sleep(1L); - System.out.println("Würfel: " + dice); + System.out.println(g.printGameboard(g,p,dice)); + TimeUnit.MILLISECONDS.sleep(600L); + System.out.println("Würfel: "+ dice); + TimeUnit.MILLISECONDS.sleep(600L); c++; ArrayList usableFigures = g.getUsableFigures(dice, p, g); if(usableFigures.size() > 0) { @@ -28,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,dice)); } } while (g.checkDice(dice, p, c)); if(p.checkGameWin(p.figures)) { @@ -49,8 +53,8 @@ public class Game { players.add(new Player("Grün",30, 52, 29)); } - public String printGameboard(Game g, Player p) { - return gb.printBoard(g, p); + public String printGameboard(Game g, Player p, int dice) { + return gb.printBoard(g, p, dice); } public static void clearScreen() { diff --git a/src/main/java/Gameboard.java b/src/main/java/Gameboard.java index 4f571f1..379d09d 100644 --- a/src/main/java/Gameboard.java +++ b/src/main/java/Gameboard.java @@ -28,8 +28,9 @@ public class Gameboard { } - public String printBoard(Game g, Player p) { + public String printBoard(Game g, Player p, int dice) { String[] f = getFiguresOnBoard(g); + String[] d = getDice(dice); String color = ""; switch (p.name) { case "Rot" -> color = RED; @@ -38,11 +39,11 @@ public class Gameboard { 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] + YELLOW + " | | "+ f[65] + YELLOW +" |\n" + - BLUE +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ YELLOW +"+---+ +---+\n" + - BLUE +"+---+ +---+"+ RESET +" +---+ "+ YELLOW +"+---+"+ RESET +" +---+ "+ YELLOW +"+---+ +---+\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 +" ---------\n" + + BLUE +"| "+ f[60] + BLUE +" | | "+ f[61] + BLUE +" |"+ RESET +" | "+ f[18] +" | | "+ f[19] +" | | "+ f[20] +" | "+ YELLOW +"| "+ f[64] + YELLOW + " | | "+ f[65] + YELLOW +" | "+ RESET + " | "+ d[0] +" "+ d[1] +" "+ d[2] +" |\n" + + BLUE +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ YELLOW +"+---+ +---+"+ RESET +" | "+ d[3] +" |\n" + + BLUE +"+---+ +---+"+ RESET +" +---+ "+ YELLOW +"+---+"+ RESET +" +---+ "+ YELLOW +"+---+ +---+"+ RESET +" | "+d[4]+" "+d[5]+" "+d[6]+" |\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 +" | "+ RESET +" ---------\n" + BLUE +"+---+ +---+"+ RESET +" +---+ "+ YELLOW +"+---+"+ RESET +" +---+ "+ YELLOW +"+---+ +---+\n" + RESET + " +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" + " | "+ f[16] +" | "+ YELLOW +" | "+ f[49] + YELLOW +" | "+ RESET +" | "+ f[22] +" |\n" + @@ -77,7 +78,68 @@ public class Gameboard { public static void main(String[] args) { Game g = new Game(); - System.out.println(g.gb.printBoard(g, g.players.get(0))); + System.out.println(g.gb.printBoard(g, g.players.get(0), 6)); + } + + public String[] getDice (int dice) { + String[] res = new String[7]; + switch (dice) { + case 1 -> { + res[0] = " "; + res[1] = " "; + res[2] = " "; + res[3] = "o"; + res[4] = " "; + res[5] = " "; + res[6] = " "; + } + case 2 -> { + res[0] = "o"; + res[1] = " "; + res[2] = " "; + res[3] = " "; + res[4] = " "; + res[5] = " "; + res[6] = "o"; + } + case 3 -> { + res[0] = "o"; + res[1] = " "; + res[2] = " "; + res[3] = "o"; + res[4] = " "; + res[5] = " "; + res[6] = "o"; + } + case 4 -> { + res[0] = "o"; + res[1] = " "; + res[2] = "o"; + res[3] = " "; + res[4] = "o"; + res[5] = " "; + res[6] = "o"; + } + case 5 -> { + res[0] = "o"; + res[1] = " "; + res[2] = "o"; + res[3] = "o"; + res[4] = "o"; + res[5] = " "; + res[6] = "o"; + } + case 6 -> { + res[0] = "o"; + res[1] = "o"; + res[2] = "o"; + res[3] = " "; + res[4] = "o"; + res[5] = "o"; + res[6] = "o"; + } + } + return res; } public String[] getFiguresOnBoard(Game g) {