From da39625783445a229a70b92387a75a4b076267a2 Mon Sep 17 00:00:00 2001 From: 3013050 <3013050@stud.hs-mannheim.de> Date: Mon, 6 May 2024 02:35:10 +0200 Subject: [PATCH] Added evaluation for the lowe block half Added automatic value calculation when setting or adding the amount of a Category object. Added value calculation for the lower half of the block categories. --- domain/Sheet.java | 32 ++++- domain/sheets/Category.java | 6 + domain/sheets/Chance.java | 16 +++ domain/sheets/FourOfKind.java | 16 +++ domain/sheets/FullHouse.java | 9 ++ domain/sheets/LargeStraight.java | 9 ++ domain/sheets/SmallStraight.java | 9 ++ domain/sheets/ThreeOfKind.java | 16 +++ domain/sheets/Three_of_kind.java | 9 -- domain/sheets/Yahtzee.java | 9 ++ fassade/KniffelSystem.java | 223 +++++++++++++++++++++++++++---- templates.txt | 7 + tui/TUI.java | 101 +++----------- 13 files changed, 338 insertions(+), 124 deletions(-) create mode 100644 domain/sheets/Chance.java create mode 100644 domain/sheets/FourOfKind.java create mode 100644 domain/sheets/FullHouse.java create mode 100644 domain/sheets/LargeStraight.java create mode 100644 domain/sheets/SmallStraight.java create mode 100644 domain/sheets/ThreeOfKind.java delete mode 100644 domain/sheets/Three_of_kind.java create mode 100644 domain/sheets/Yahtzee.java create mode 100644 templates.txt diff --git a/domain/Sheet.java b/domain/Sheet.java index 06f1f03..3d7c657 100644 --- a/domain/Sheet.java +++ b/domain/Sheet.java @@ -18,12 +18,13 @@ public class Sheet { Sixes sixes; // Sheet rows, second half - int three_of_kind; - int four_of_kind; - int full_house; - int small_straight; - int large_straight; - int chance; + ThreeOfKind threeOfKind; + FourOfKind fourOfKind; + FullHouse fullHouse; + SmallStraight smallStraight; + LargeStraight largeStraight; + Yahtzee yahtzee; + Chance chance; public Sheet(){ @@ -33,9 +34,20 @@ public class Sheet { this.fours = new Fours(); this.fives = new Fives(); this.sixes = new Sixes(); + + this.threeOfKind = new ThreeOfKind(); + this.fourOfKind = new FourOfKind(); + this.fullHouse = new FullHouse(); + this.smallStraight = new SmallStraight(); + this.largeStraight = new LargeStraight(); + this.yahtzee = new Yahtzee(); + this.chance = new Chance(); } + public void writeCategory(String category) + + public HashMap getAllCategories(){ HashMap allCategories = new HashMap<>(); @@ -46,6 +58,14 @@ public class Sheet { allCategories.put("Fives", fives); allCategories.put("Sixes", sixes); + allCategories.put("ThreeOfKind", threeOfKind); + allCategories.put("FourOfKind", fourOfKind); + allCategories.put("FullHouse", fullHouse); + allCategories.put("SmallStraight", smallStraight); + allCategories.put("LargeStraight", largeStraight); + allCategories.put("Yahtzee", yahtzee); + allCategories.put("Chance", chance); + return allCategories; } } diff --git a/domain/sheets/Category.java b/domain/sheets/Category.java index 50b7880..3ec26b4 100644 --- a/domain/sheets/Category.java +++ b/domain/sheets/Category.java @@ -1,5 +1,7 @@ package domain.sheets; +import java.util.ArrayList; + public class Category { int amount = 0; int value = 0; @@ -10,6 +12,10 @@ public class Category { return amount; } + public void calcValueFromRoll(ArrayList rolls){} + + public void independentValue(){} + //? Amount public int getAmount() { diff --git a/domain/sheets/Chance.java b/domain/sheets/Chance.java new file mode 100644 index 0000000..6952259 --- /dev/null +++ b/domain/sheets/Chance.java @@ -0,0 +1,16 @@ +package domain.sheets; + +import java.util.ArrayList; + +public class Chance extends Category{ + + @Override + public void calcValueFromRoll(ArrayList rolls) { + int sum = 0; + for(int roll : rolls){ + sum += roll; + } + + this.value = sum; + } +} diff --git a/domain/sheets/FourOfKind.java b/domain/sheets/FourOfKind.java new file mode 100644 index 0000000..908c240 --- /dev/null +++ b/domain/sheets/FourOfKind.java @@ -0,0 +1,16 @@ +package domain.sheets; + +import java.util.ArrayList; + +public class FourOfKind extends Category { + + @Override + public void calcValueFromRoll(ArrayList rolls) { + int sum = 0; + for(int roll : rolls){ + sum += roll; + } + + this.value = sum; + } +} diff --git a/domain/sheets/FullHouse.java b/domain/sheets/FullHouse.java new file mode 100644 index 0000000..bffb191 --- /dev/null +++ b/domain/sheets/FullHouse.java @@ -0,0 +1,9 @@ +package domain.sheets; + +public class FullHouse extends Category{ + + @Override + public void independentValue(){ + this.value = 25; + } +} diff --git a/domain/sheets/LargeStraight.java b/domain/sheets/LargeStraight.java new file mode 100644 index 0000000..0bc5091 --- /dev/null +++ b/domain/sheets/LargeStraight.java @@ -0,0 +1,9 @@ +package domain.sheets; + +public class LargeStraight extends Category{ + + @Override + public void independentValue() { + this.value = 40; + } +} diff --git a/domain/sheets/SmallStraight.java b/domain/sheets/SmallStraight.java new file mode 100644 index 0000000..dd48c6f --- /dev/null +++ b/domain/sheets/SmallStraight.java @@ -0,0 +1,9 @@ +package domain.sheets; + +public class SmallStraight extends Category{ + + @Override + public void independentValue() { + this.value = 30; + } +} diff --git a/domain/sheets/ThreeOfKind.java b/domain/sheets/ThreeOfKind.java new file mode 100644 index 0000000..1c579c3 --- /dev/null +++ b/domain/sheets/ThreeOfKind.java @@ -0,0 +1,16 @@ +package domain.sheets; + +import java.util.ArrayList; + +public class ThreeOfKind extends Category { + + @Override + public void calcValueFromRoll(ArrayList rolls) { + int sum = 0; + for(int roll : rolls){ + sum += roll; + } + + this.value = sum; + } +} diff --git a/domain/sheets/Three_of_kind.java b/domain/sheets/Three_of_kind.java deleted file mode 100644 index 769c3d1..0000000 --- a/domain/sheets/Three_of_kind.java +++ /dev/null @@ -1,9 +0,0 @@ -package domain.sheets; - -public class Three_of_kind extends Category { - @Override - public int calcValueFromAmount() { - this.value = this.amount * 2; - return this.value; - } -} diff --git a/domain/sheets/Yahtzee.java b/domain/sheets/Yahtzee.java new file mode 100644 index 0000000..71c55be --- /dev/null +++ b/domain/sheets/Yahtzee.java @@ -0,0 +1,9 @@ +package domain.sheets; + +public class Yahtzee extends Category{ + + @Override + public void independentValue() { + this.value = 50; + } +} diff --git a/fassade/KniffelSystem.java b/fassade/KniffelSystem.java index 8e33d04..71ffd4e 100644 --- a/fassade/KniffelSystem.java +++ b/fassade/KniffelSystem.java @@ -2,10 +2,10 @@ package fassade; import domain.Game; import domain.Player; +import domain.Sheet; +import domain.sheets.Category; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Random; +import java.util.*; public class KniffelSystem { ArrayList playerColors; @@ -31,14 +31,14 @@ public class KniffelSystem { Player playerToAdd = new Player(playerNumber, name, playerColor, 0); game.addPlayer(playerToAdd); - return changePlayerNameColor(name, playerColor); + return changeStringFormat(name, playerColor); } - private String changePlayerNameColor(String name, String color){ + private String changeStringFormat(String string, String formatation){ String ANSI_RESET = "\u001B[0m"; - return String.format(color + name + ANSI_RESET); + return String.format(formatation + string + ANSI_RESET); } @@ -53,8 +53,8 @@ public class KniffelSystem { } - public String getCurrentPlayer(){ - return game.getCurrentPlayer().toString(); + public Player getCurrentPlayer(){ + return game.getCurrentPlayer(); } @@ -63,8 +63,7 @@ public class KniffelSystem { } - // TEST - public void createTestPlayers(int amountPlayer){ + public void creteDevPlayers(int amountPlayer){ String[] names = {"Vic", "Nastja", "Lilli", "Emelie", "Esra", "Oli"}; for (int i = 0; i < amountPlayer; i++){ addPlayer(i+1, names[i]); @@ -83,19 +82,6 @@ public class KniffelSystem { int amountNewRolls = oldRolls.size(); ArrayList newRolls = rollMultipleDice(5 - amountNewRolls); - // --TEST - System.out.println("Old rolls:"); - for(int oldRoll: oldRolls){ - System.out.println(oldRoll); - } - - System.out.println("New Rolls:"); - for(int newRoll: newRolls){ - System.out.println(newRoll); - } - System.out.println(); - // TEST-- - oldRolls.addAll(newRolls); return oldRolls; @@ -118,11 +104,8 @@ public class KniffelSystem { return keptRolls; } - keptDice = keptDice.substring(1, keptDice.length()-1); - System.out.printf("Edited keptDice String: %s \n", keptDice); //! TEST - String[] keptDiceIndicesStrings = keptDice.split(","); for (String keptDiceIndicesString : keptDiceIndicesStrings) { @@ -135,7 +118,6 @@ public class KniffelSystem { private ArrayList rollMultipleDice(int amountRolls){ - System.out.printf("Amount rolls: %d \n", amountRolls); //! TEST ArrayList rolls = new ArrayList<>(); if (amountRolls == 0){ return rolls; @@ -148,6 +130,193 @@ public class KniffelSystem { } + public String evaluateRoll(ArrayList rolls){ + HashMap possibleCombinations = createCategoryHashMap(); + ArrayList validCombinations = new ArrayList<>(); + //TODO Add starwars logic + + + for (int dice : rolls){ + switch (dice){ + case 1: + possibleCombinations.get("Aces").addAmount(); + break; + case 2: + possibleCombinations.get("Twos").addAmount(); + break; + case 3: + possibleCombinations.get("Threes").addAmount(); + break; + case 4: + possibleCombinations.get("Fours").addAmount(); + break; + case 5: + possibleCombinations.get("Fives").addAmount(); + break; + case 6: + possibleCombinations.get("Sixes").addAmount(); + break; + } + } + + StringBuilder sb = new StringBuilder(); + sb.append(String.format("%s \n", changeStringFormat("Possible combinations:", "\u001B[32m"))); + sb.append(String.format("%s \n", changeStringFormat("Upper half:", "\u001b[4m"))); + + Set keys = possibleCombinations.keySet(); + for (String key : keys) { + Category keyObj = possibleCombinations.get(key); + int keyAmount = keyObj.getAmount(); + int keyValue = keyObj.getValue(); + String keyValueString = changeStringFormat(Integer.toString(keyValue), "\u001b[1m"); + + if (keyAmount != 0){ + validCombinations.add(keyObj); + addCombinationToStringbuilder(sb, keyObj, keyAmount); + } + } + + //? ------------------------------ LOWER HALF------------------------------------------------- + sb.append("\n"); + sb.append(String.format("%s \n", changeStringFormat("Lower half:", "\u001b[4m"))); + + int index = 0; + Category keyObj; + for(Category category : validCombinations){ + + //? Three of a kind + if (category.getAmount() >= 3){ + keyObj = possibleCombinations.get("ThreeOfKind"); + + keyObj.calcValueFromRoll(rolls); + addCombinationToStringbuilder(sb, keyObj); + } + + //? Four of a kind + if (category.getAmount() >= 4){ + keyObj = possibleCombinations.get("FourOfKind"); + + keyObj.calcValueFromRoll(rolls); + addCombinationToStringbuilder(sb, keyObj); + } + + //? Full House + if (category.getAmount() == 3){ + for(Category innerCategory : validCombinations){ + if ((innerCategory.getAmount() == 2) && !(innerCategory.toString().equals(category.toString()))){ + keyObj = possibleCombinations.get("FullHouse"); + + keyObj.independentValue(); + addCombinationToStringbuilder(sb, keyObj); + } + } + } + + int amountRolls = rolls.size(); + //? Small Straight + + if (index == 0) { + boolean isNotSmallStraight = false; + if ((rolls.get(index) + 1) == rolls.get(index + 1)){ + for (int i = 1; i < (amountRolls - 2); i++){ + System.out.printf("1 Index: %d \nComp pairs: %d - %d \n", i, rolls.get(i), rolls.get(i+1)); + } + } + + if (!(isNotSmallStraight)){ + keyObj = possibleCombinations.get("SmallStraight"); + + keyObj.independentValue(); + addCombinationToStringbuilder(sb, keyObj); + } + } + + else if (index == 1){ + boolean isNotSmallStraight = false; + if ((rolls.get(index) + 1) == rolls.get(index + 1)){ + for (int i = 2; i < (amountRolls - 1); i++){ + System.out.printf("2 Index: %d \nComp pairs: %d - %d \n", i, rolls.get(i), rolls.get(i+1)); + } + } + + if (!(isNotSmallStraight)){ + keyObj = possibleCombinations.get("SmallStraight"); + + keyObj.independentValue(); + addCombinationToStringbuilder(sb, keyObj); + } + } + + //? Large Straight + if (index == 0){ + if ((rolls.get(index) + 1) == rolls.get(index + 1)){ + boolean isNotLargeStraight = false; + + for (int i = 1; i < (amountRolls - 1); i++){ + + // System.out.printf("Index: %d \nComp pairs: %d - %d \n", i, rolls.get(i), rolls.get(i+1)); //! TEST + if ((rolls.get(i) + 1) != rolls.get(i + 1)){ + isNotLargeStraight = true; + break; + } + } + if (!(isNotLargeStraight)){ + keyObj = possibleCombinations.get("LargeStraight"); + + keyObj.independentValue(); + addCombinationToStringbuilder(sb, keyObj); + } + } + } + + //? Yahtzee + if (category.getAmount() == 5){ + keyObj = possibleCombinations.get("Yahtzee"); + + keyObj.independentValue(); + addCombinationToStringbuilder(sb, keyObj); + } + + index ++; + } + + keyObj = possibleCombinations.get("Chance"); + + keyObj.calcValueFromRoll(rolls); + addCombinationToStringbuilder(sb, keyObj); + + + + return sb.toString(); + } + + + private void addCombinationToStringbuilder(StringBuilder sb, Category keyObj){ + int keyValue = keyObj.getValue(); + String keyValueString = changeStringFormat(Integer.toString(keyValue), "\u001b[1m"); + sb.append(String.format("%s: Value: %s \n",keyObj, keyValueString)); + } + + private void addCombinationToStringbuilder(StringBuilder sb, Category keyObj, int amount){ + int keyValue = keyObj.getValue(); + String keyValueString = changeStringFormat(Integer.toString(keyValue), "\u001b[1m"); + sb.append(String.format("%s: %d, Value: %s \n",keyObj, amount, keyValueString)); + } + + + public void writeToSheet(String sheetInput){ + System.out.printf("Writing on %s sheet \n", getCurrentPlayer()); + } + + + private HashMap createCategoryHashMap(){ + // TODO starwars sheet implementieren + Sheet sheet = new Sheet(); + + return sheet.getAllCategories(); + } + + private ArrayList createDevRoll(String keptDice){ // Format: dev(1,2,3,4,5) // values aren't indices, they are the dice value diff --git a/templates.txt b/templates.txt new file mode 100644 index 0000000..7382681 --- /dev/null +++ b/templates.txt @@ -0,0 +1,7 @@ +dev(5,5,5,5,4) + +dev(5,5,5,5,5) + +dev(5,5,5,4,4) + +dev(6,6,6,5,5) \ No newline at end of file diff --git a/tui/TUI.java b/tui/TUI.java index 45fa5d1..43a0958 100644 --- a/tui/TUI.java +++ b/tui/TUI.java @@ -1,13 +1,9 @@ package tui; -import domain.Sheet; -import domain.sheets.*; import fassade.KniffelSystem; import java.util.ArrayList; -import java.util.HashMap; import java.util.Scanner; -import java.util.Set; public class TUI { static KniffelSystem gameSystem; @@ -23,11 +19,11 @@ public class TUI { // DEV: gameSystem = new KniffelSystem(); - gameSystem.createTestPlayers(6); + gameSystem.creteDevPlayers(6); gameLoop(); } - private static int mainMenuOutput(){ + private static void mainMenuOutput(){ System.out.println("What do you want to do?"); System.out.println("1 - Play"); System.out.println("2 - See leaderboard"); @@ -39,21 +35,15 @@ public class TUI { if ((mainMenuUserInput.equals("1")) || (mainMenuUserInput.equals("play"))){ mainMenuPlay(); - System.out.println("play"); // TEST - return 1; } else if ((mainMenuUserInput.equals("2")) || (mainMenuUserInput.equals("see leaderboard")) || (mainMenuUserInput.equals("see")) || (mainMenuUserInput.equals("leaderboard"))){ - System.out.println("leaderboard"); // TEST mainMenuLeaderBoard(); - return 2; } else { - System.out.println("exit"); // TEST mainMenuExit(); - return 3; } } @@ -100,94 +90,41 @@ public class TUI { String keptDice = ""; while(rollscount < 3){ - System.out.printf("Roll Nr. %d \n", rollscount); // TEST - System.out.printf("Kept dice: %s \n\n", keptDice); // TEST - - rolls = gameSystem.rollDices(rolls, keptDice); String newRollsString = diceArrToString(rolls); System.out.println(newRollsString); - evaluateRoll(rolls); + String evaluatedRolls = gameSystem.evaluateRoll(rolls); + System.out.println(evaluatedRolls); - System.out.println("Which dice do you want to keep?"); - System.out.println("Empty for none, single digit for one dice or (1,3,4) for multiple dice."); - System.out.print("> "); - keptDice = sc.nextLine(); + + if (rollscount < 2) { + System.out.println("Which dice do you want to keep?"); + System.out.println("Empty for none, single digit for one dice or (1,3,4) for multiple dice."); + System.out.print("> "); + keptDice = sc.nextLine(); + } rollscount++; } + writeToSheet(); + turncounter = triggerNextTurn(turncounter); } } - private static String evaluateRoll(ArrayList rolls){ - HashMap possibleCombinations = createCategorieHashMap(); - ArrayList validCombinations = new ArrayList<>(); - //TODO Add starwars logic - - - for (int dice : rolls){ - switch (dice){ - case 1: - possibleCombinations.get("Aces").addAmount(); - break; - case 2: - possibleCombinations.get("Twos").addAmount(); - break; - case 3: - possibleCombinations.get("Threes").addAmount(); - break; - case 4: - possibleCombinations.get("Fours").addAmount(); - break; - case 5: - possibleCombinations.get("Fives").addAmount(); - break; - case 6: - possibleCombinations.get("Sixes").addAmount(); - break; - } - } - - StringBuilder sb = new StringBuilder(); - sb.append("Possible combinations: \n"); - - Set keys = possibleCombinations.keySet(); - for (String key : keys) { - Category keyObj = possibleCombinations.get(key); - int keyAmount = keyObj.getAmount(); - int keyValue = keyObj.getValue(); - - if (keyAmount != 0){ - validCombinations.add(keyObj); - sb.append(String.format("%s: Amount: %d, Value: %d \n",key, keyAmount, keyValue)); - } - } - System.out.println(sb.toString()); - - for(Category categorie : validCombinations){ - if (categorie.getAmount() == 3){ - - } - } - - return "TODO"; + private static void writeToSheet(){ + System.out.println("Which row do you want to fill?"); + System.out.print(">"); + String sheetInput = sc.nextLine(); + gameSystem.writeToSheet(sheetInput); } - private static HashMap createCategorieHashMap(){ - // TODO starwars sheet implementieren - Sheet sheet = new Sheet(); - - return sheet.getAllCategories(); - } - - - private static String diceArrToString(ArrayList diceArr){ StringBuilder sb = new StringBuilder(); + sb.append("Throwing dice... \n"); sb.append("Your rolls: \n"); for (int i = 0; i < diceArr.size(); i++){ sb.append(String.format("Dice %d: %s \n", i+1, Integer.toString(diceArr.get(i))));