From 495306ecb5e6022302dbcfbd6487fc41bb880ed2 Mon Sep 17 00:00:00 2001 From: 3013050 <3013050@stud.hs-mannheim.de> Date: Sun, 5 May 2024 18:12:50 +0200 Subject: [PATCH] Added evaluation for the upper block half Added automatic value calculation when setting or adding the amount of a Category object. Added value calculation for the upper half of the block categories. --- domain/Game.java | 2 +- domain/Sheet.java | 40 ++++++++++++++--- domain/sheets/Aces.java | 10 +++++ domain/sheets/Category.java | 52 ++++++++++++++++++++++ domain/sheets/Fives.java | 10 +++++ domain/sheets/Fours.java | 10 +++++ domain/sheets/Sixes.java | 10 +++++ domain/sheets/Three_of_kind.java | 9 ++++ domain/sheets/Threes.java | 10 +++++ domain/sheets/Twos.java | 10 +++++ fassade/KniffelSystem.java | 32 +++++++++++--- tui/TUI.java | 76 +++++++++++++++++++++++++++++--- 12 files changed, 253 insertions(+), 18 deletions(-) create mode 100644 domain/sheets/Aces.java create mode 100644 domain/sheets/Category.java create mode 100644 domain/sheets/Fives.java create mode 100644 domain/sheets/Fours.java create mode 100644 domain/sheets/Sixes.java create mode 100644 domain/sheets/Three_of_kind.java create mode 100644 domain/sheets/Threes.java create mode 100644 domain/sheets/Twos.java diff --git a/domain/Game.java b/domain/Game.java index 9603078..420607e 100644 --- a/domain/Game.java +++ b/domain/Game.java @@ -12,7 +12,7 @@ public class Game { public Game(){ currentPlayers = new ArrayList(); turnPlayer = 0; - dice = new Dice(8); + dice = new Dice(6); } diff --git a/domain/Sheet.java b/domain/Sheet.java index 6413c66..06f1f03 100644 --- a/domain/Sheet.java +++ b/domain/Sheet.java @@ -1,17 +1,21 @@ package domain; +import domain.sheets.*; + +import java.util.HashMap; + public class Sheet { private String[] usedRows; private String[] canceledRows; private String[] emptyRows; // Sheet rows, first half - int aces; - int twos; - int threes; - int fours; - int fives; - int sixes; + Aces aces; + Twos twos; + Threes threes; + Fours fours; + Fives fives; + Sixes sixes; // Sheet rows, second half int three_of_kind; @@ -20,4 +24,28 @@ public class Sheet { int small_straight; int large_straight; int chance; + + + public Sheet(){ + this.aces = new Aces(); + this.twos = new Twos(); + this.threes = new Threes(); + this.fours = new Fours(); + this.fives = new Fives(); + this.sixes = new Sixes(); + } + + + public HashMap getAllCategories(){ + HashMap allCategories = new HashMap<>(); + + allCategories.put("Aces", aces); + allCategories.put("Twos", twos); + allCategories.put("Threes", threes); + allCategories.put("Fours", fours); + allCategories.put("Fives", fives); + allCategories.put("Sixes", sixes); + + return allCategories; + } } diff --git a/domain/sheets/Aces.java b/domain/sheets/Aces.java new file mode 100644 index 0000000..20615af --- /dev/null +++ b/domain/sheets/Aces.java @@ -0,0 +1,10 @@ +package domain.sheets; + +public class Aces extends Category { + + @Override + public int calcValueFromAmount() { + this.value = this.amount; + return this.value; + } +} diff --git a/domain/sheets/Category.java b/domain/sheets/Category.java new file mode 100644 index 0000000..50b7880 --- /dev/null +++ b/domain/sheets/Category.java @@ -0,0 +1,52 @@ +package domain.sheets; + +public class Category { + int amount = 0; + int value = 0; + boolean crossed = false; + + + public int calcValueFromAmount(){ + return amount; + } + + + //? Amount + public int getAmount() { + return amount; + } + public Category setAmount(int amount) { + this.amount = amount; + this.value = calcValueFromAmount(); + return this; + } + public void addAmount(){ + setAmount(this.amount + 1); + } + + + //? Value + public int getValue() { + return value; + } + public Category setValue(int value) { + this.value = value; + return this; + } + + + //? Crossed + public boolean getCrossed() { + return crossed; + } + public Category setCrossed(boolean crossed) { + this.crossed = crossed; + return this; + } + + + @Override + public String toString() { + return String.format("%s", this.getClass().getSimpleName()); + } +} diff --git a/domain/sheets/Fives.java b/domain/sheets/Fives.java new file mode 100644 index 0000000..41f92b5 --- /dev/null +++ b/domain/sheets/Fives.java @@ -0,0 +1,10 @@ +package domain.sheets; + +public class Fives extends Category { + + @Override + public int calcValueFromAmount() { + this.value = this.amount * 5; + return this.value; + } +} diff --git a/domain/sheets/Fours.java b/domain/sheets/Fours.java new file mode 100644 index 0000000..9834039 --- /dev/null +++ b/domain/sheets/Fours.java @@ -0,0 +1,10 @@ +package domain.sheets; + +public class Fours extends Category { + + @Override + public int calcValueFromAmount() { + this.value = this.amount * 4; + return this.value; + } +} diff --git a/domain/sheets/Sixes.java b/domain/sheets/Sixes.java new file mode 100644 index 0000000..4cdf613 --- /dev/null +++ b/domain/sheets/Sixes.java @@ -0,0 +1,10 @@ +package domain.sheets; + +public class Sixes extends Category { + + @Override + public int calcValueFromAmount() { + this.value = this.amount * 6; + return this.value; + } +} diff --git a/domain/sheets/Three_of_kind.java b/domain/sheets/Three_of_kind.java new file mode 100644 index 0000000..769c3d1 --- /dev/null +++ b/domain/sheets/Three_of_kind.java @@ -0,0 +1,9 @@ +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/Threes.java b/domain/sheets/Threes.java new file mode 100644 index 0000000..4be8d9f --- /dev/null +++ b/domain/sheets/Threes.java @@ -0,0 +1,10 @@ +package domain.sheets; + +public class Threes extends Category { + + @Override + public int calcValueFromAmount() { + this.value = this.amount * 3; + return this.value; + } +} diff --git a/domain/sheets/Twos.java b/domain/sheets/Twos.java new file mode 100644 index 0000000..688d52f --- /dev/null +++ b/domain/sheets/Twos.java @@ -0,0 +1,10 @@ +package domain.sheets; + +public class Twos extends Category { + + @Override + public int calcValueFromAmount() { + this.value = this.amount * 2; + return this.value; + } +} diff --git a/fassade/KniffelSystem.java b/fassade/KniffelSystem.java index a7f3823..8e33d04 100644 --- a/fassade/KniffelSystem.java +++ b/fassade/KniffelSystem.java @@ -73,6 +73,11 @@ public class KniffelSystem { public ArrayList rollDices(ArrayList rolls, String keptDice){ + //? DEV TEST + if (keptDice.startsWith("dev")){ + return createDevRoll(keptDice); + } + ArrayList oldRolls = extractKeptDice(rolls, keptDice); int amountNewRolls = oldRolls.size(); @@ -104,6 +109,9 @@ public class KniffelSystem { return keptRolls; } + //? Remove whitespaces + keptDice = keptDice.replaceAll("\\s+",""); + if (keptDice.length() == 1){ int singleIndex = Integer.parseInt(keptDice); keptRolls.add(previousRolls.get(singleIndex - 1)); @@ -111,11 +119,9 @@ public class KniffelSystem { } - keptDice = keptDice.replaceAll("\\s+",""); keptDice = keptDice.substring(1, keptDice.length()-1); - System.out.printf("Edited keptDice String: %s \n", keptDice); // TEST - + System.out.printf("Edited keptDice String: %s \n", keptDice); //! TEST String[] keptDiceIndicesStrings = keptDice.split(","); @@ -129,7 +135,7 @@ public class KniffelSystem { private ArrayList rollMultipleDice(int amountRolls){ - System.out.printf("Amount rolls: %d \n", amountRolls); // TEST + System.out.printf("Amount rolls: %d \n", amountRolls); //! TEST ArrayList rolls = new ArrayList<>(); if (amountRolls == 0){ return rolls; @@ -142,7 +148,23 @@ public class KniffelSystem { } - // TEST + private ArrayList createDevRoll(String keptDice){ + // Format: dev(1,2,3,4,5) + // values aren't indices, they are the dice value + ArrayList devRoll = new ArrayList<>(); + + keptDice = keptDice.replaceAll("\\s+",""); + keptDice = keptDice.substring(4, keptDice.length()-1); + + String[] rollStrings = keptDice.split(","); + for (String rollString : rollStrings){ + devRoll.add(Integer.parseInt(rollString)); + } + return devRoll; + } + + + //! TEST public String[] getAllPlayerStrings(){ ArrayList players = game.getPlayers(); String[] returnStrings = new String[players.size()]; diff --git a/tui/TUI.java b/tui/TUI.java index 3fe8e45..45fa5d1 100644 --- a/tui/TUI.java +++ b/tui/TUI.java @@ -1,9 +1,13 @@ 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; @@ -12,15 +16,15 @@ public class TUI { public static void main(String[] args) { System.out.println("Welcome to the PR2 Kniffel game!"); - while (true){ - mainMenuOutput(); - } +// while (true){ +// mainMenuOutput(); +// } // DEV: -// gameSystem = new KniffelSystem(); -// gameSystem.createTestPlayers(6); -// gameLoop(); + gameSystem = new KniffelSystem(); + gameSystem.createTestPlayers(6); + gameLoop(); } private static int mainMenuOutput(){ @@ -103,6 +107,7 @@ public class TUI { rolls = gameSystem.rollDices(rolls, keptDice); String newRollsString = diceArrToString(rolls); System.out.println(newRollsString); + evaluateRoll(rolls); 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."); @@ -118,10 +123,69 @@ public class TUI { 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 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("Your rolls: \n");