From 979037094290079b322edf912ac50fd6dca472bd Mon Sep 17 00:00:00 2001 From: 3013050 <3013050@stud.hs-mannheim.de> Date: Mon, 6 May 2024 17:19:11 +0200 Subject: [PATCH] Added that score is written to sheet Every turn the chosen row gets written to the sheet. the sheet keeps track of filled rows and ends it if all rows are filled. --- domain/Player.java | 3 + domain/Sheet.java | 123 +++++++++++++++++++++++- fassade/GameCycle.java | 25 ++++- fassade/KniffelSystem.java | 191 ++++++++++++++++++++++--------------- quellen.txt | 4 + templates.txt | 9 +- tui/TUI.java | 40 +++++--- 7 files changed, 299 insertions(+), 96 deletions(-) diff --git a/domain/Player.java b/domain/Player.java index bdc5769..ca6d615 100644 --- a/domain/Player.java +++ b/domain/Player.java @@ -20,6 +20,9 @@ public class Player { this.sheet = new Sheet(); } + public Sheet getSheet(){ + return this.sheet; + } @Override public String toString() { diff --git a/domain/Sheet.java b/domain/Sheet.java index 3d7c657..9ee7bca 100644 --- a/domain/Sheet.java +++ b/domain/Sheet.java @@ -2,12 +2,14 @@ package domain; import domain.sheets.*; +import java.util.ArrayList; import java.util.HashMap; public class Sheet { - private String[] usedRows; - private String[] canceledRows; - private String[] emptyRows; + int amountTurns; + ArrayList unusedRows = new ArrayList<>(); + ArrayList usedRows = new ArrayList<>(); + ArrayList crossedRows = new ArrayList<>(); // Sheet rows, first half Aces aces; @@ -29,23 +31,136 @@ public class Sheet { public Sheet(){ this.aces = new Aces(); + unusedRows.add(this.aces.toString()); + this.twos = new Twos(); + unusedRows.add(this.twos.toString()); + this.threes = new Threes(); + unusedRows.add(this.threes.toString()); + this.fours = new Fours(); + unusedRows.add(this.fours.toString()); + this.fives = new Fives(); + unusedRows.add(this.fives.toString()); + this.sixes = new Sixes(); + unusedRows.add(this.sixes.toString()); this.threeOfKind = new ThreeOfKind(); + unusedRows.add(this.threeOfKind.toString()); + this.fourOfKind = new FourOfKind(); + unusedRows.add(this.fourOfKind.toString()); + this.fullHouse = new FullHouse(); + unusedRows.add(this.fullHouse.toString()); + this.smallStraight = new SmallStraight(); + unusedRows.add(this.smallStraight.toString()); + this.largeStraight = new LargeStraight(); + unusedRows.add(this.largeStraight.toString()); + this.yahtzee = new Yahtzee(); + unusedRows.add(this.yahtzee.toString()); + this.chance = new Chance(); + unusedRows.add(this.chance.toString()); + + amountTurns = unusedRows.size(); } - public void writeCategory(String category) + public void writeCategory(Category categoryToWrite, boolean crossing){ + switch (categoryToWrite.toString()){ + case "Aces": + aces = (Aces) categoryToWrite; + manageArrays(crossing, aces); + break; + case "Twos": + twos = (Twos) categoryToWrite; + manageArrays(crossing, twos); + break; + case "Threes": + threes = (Threes) categoryToWrite; + manageArrays(crossing, threes); + break; + case "Fours": + fours = (Fours) categoryToWrite; + manageArrays(crossing, fours); + break; + case "Fives": + fives = (Fives) categoryToWrite; + manageArrays(crossing, fives); + break; + case "Sixes": + sixes = (Sixes) categoryToWrite; + manageArrays(crossing, sixes); + break; + case "ThreeOfKind": + threeOfKind = (ThreeOfKind) categoryToWrite; + manageArrays(crossing, threeOfKind); + break; + case "FourOfKind": + fourOfKind = (FourOfKind) categoryToWrite; + manageArrays(crossing, fourOfKind); + break; + case "FullHouse": + fullHouse = (FullHouse) categoryToWrite; + manageArrays(crossing, fullHouse); + break; + case "SmallStraight": + smallStraight = (SmallStraight) categoryToWrite; + manageArrays(crossing, smallStraight); + break; + case "LargeStraight": + largeStraight = (LargeStraight) categoryToWrite; + manageArrays(crossing, largeStraight); + break; + case "Yahtzee": + yahtzee = (Yahtzee) categoryToWrite; + manageArrays(crossing, yahtzee); + break; + case "Chance": + chance = (Chance) categoryToWrite; + manageArrays(crossing, chance); + break; + } + } + + + private void manageArrays(boolean crossing, Category category){ + category.setCrossed(crossing); + unusedRows.remove(category.toString()); + + if (crossing){ + crossedRows.add(category.toString()); + } else { + usedRows.add(category.toString()); + } + } + + + public boolean checkGameEnd(){ + return (usedRows.size() + crossedRows.size()) == amountTurns; + } + + + public ArrayList getCrossedRows(){ + return crossedRows; + } + + + public ArrayList getUnusedRows(){ + return unusedRows; + } + + + public ArrayList getUsedRows(){ + return usedRows; + } public HashMap getAllCategories(){ diff --git a/fassade/GameCycle.java b/fassade/GameCycle.java index ba4f5fd..57a07b5 100644 --- a/fassade/GameCycle.java +++ b/fassade/GameCycle.java @@ -1,5 +1,28 @@ package fassade; public class GameCycle { - +// public class DiceRoll { +// public static boolean hasSmallStraight(int[] dice) { +// Arrays.sort(dice); +// for (int i = 0; i < dice.length - 3; i++) { +// if (dice[i] + 1 == dice[i + 1] && dice[i] + 2 == dice[i + 2] && dice[i] + 3 == dice[i + 3]) { +// return true; +// } +// } +// return false; +// } +// +// public static boolean hasLargeStraight(int[] dice) { +// Arrays.sort(dice); +// for (int i = 0; i < dice.length - 4; i++) { +// if (dice[i] + 1 == dice[i + 1] && dice[i] + 2 == dice[i + 2] && dice[i] + 3 == dice[i + 3] && dice[i] + 4 == dice[i + 4]) { +// return true; +// } +// } +// return false; +// } +// +// public static void main(String[] args) { +// int[] dice = {3, 1, 4, 2, 5};} +// } // example array System.out.println("Has small straight: " + hasSmallStraight(dice)); System.out.println("Has large straight: " + hasLargeStraight(dice)); }}```Here's how the code works:1. First, we sort the array of dice rolls in ascending order using `Arrays.sort()`.2. In the `hasSmallStraight()` method, we iterate through the sorted array and check if four consecutive numbers appear. We do this by checking if the current element plus 1, 2, and 3 are equal to the next three elements in the array. If we find a match, we return `true`.3. In the `hasLargeStraight()` method, we do a similar check, } diff --git a/fassade/KniffelSystem.java b/fassade/KniffelSystem.java index 71ffd4e..fe4eeff 100644 --- a/fassade/KniffelSystem.java +++ b/fassade/KniffelSystem.java @@ -9,6 +9,7 @@ import java.util.*; public class KniffelSystem { ArrayList playerColors; + ArrayList allValidCombinations = new ArrayList<>(); Game game; public KniffelSystem(){ game = new Game(); @@ -35,7 +36,7 @@ public class KniffelSystem { } - private String changeStringFormat(String string, String formatation){ + public String changeStringFormat(String string, String formatation){ String ANSI_RESET = "\u001B[0m"; return String.format(formatation + string + ANSI_RESET); @@ -132,7 +133,8 @@ public class KniffelSystem { public String evaluateRoll(ArrayList rolls){ HashMap possibleCombinations = createCategoryHashMap(); - ArrayList validCombinations = new ArrayList<>(); + ArrayList validUpperCombinations = new ArrayList<>(); + allValidCombinations = new ArrayList<>(); //TODO Add starwars logic @@ -163,16 +165,13 @@ public class KniffelSystem { 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); + for (String key : possibleCombinations.keySet()) { + Category keyObj = possibleCombinations.get(key); + + if (keyObj.getAmount() != 0){ + validUpperCombinations.add(keyObj); + addCombinationToStringbuilder(sb, keyObj, allValidCombinations); } } @@ -182,14 +181,14 @@ public class KniffelSystem { int index = 0; Category keyObj; - for(Category category : validCombinations){ + for(Category category : validUpperCombinations){ //? Three of a kind if (category.getAmount() >= 3){ keyObj = possibleCombinations.get("ThreeOfKind"); keyObj.calcValueFromRoll(rolls); - addCombinationToStringbuilder(sb, keyObj); + addCombinationToStringbuilder(sb, keyObj, allValidCombinations); } //? Four of a kind @@ -197,76 +196,33 @@ public class KniffelSystem { keyObj = possibleCombinations.get("FourOfKind"); keyObj.calcValueFromRoll(rolls); - addCombinationToStringbuilder(sb, keyObj); + addCombinationToStringbuilder(sb, keyObj, allValidCombinations); } //? Full House if (category.getAmount() == 3){ - for(Category innerCategory : validCombinations){ + for(Category innerCategory : validUpperCombinations){ if ((innerCategory.getAmount() == 2) && !(innerCategory.toString().equals(category.toString()))){ keyObj = possibleCombinations.get("FullHouse"); keyObj.independentValue(); - addCombinationToStringbuilder(sb, keyObj); + addCombinationToStringbuilder(sb, keyObj, allValidCombinations); } } } - int amountRolls = rolls.size(); + ArrayList sortedRolls = new ArrayList<>(rolls); + Collections.sort(sortedRolls); + + //? 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); - } - } + if (index == 0) { + checkForSmallStraight(sortedRolls, possibleCombinations, sb); + } //? 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); - } - } + checkForLargeStraight(sortedRolls, index, possibleCombinations, sb); } //? Yahtzee @@ -274,7 +230,7 @@ public class KniffelSystem { keyObj = possibleCombinations.get("Yahtzee"); keyObj.independentValue(); - addCombinationToStringbuilder(sb, keyObj); + addCombinationToStringbuilder(sb, keyObj, allValidCombinations); } index ++; @@ -283,29 +239,108 @@ public class KniffelSystem { keyObj = possibleCombinations.get("Chance"); keyObj.calcValueFromRoll(rolls); - addCombinationToStringbuilder(sb, keyObj); - - + addCombinationToStringbuilder(sb, keyObj, allValidCombinations); 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 checkForLargeStraight(ArrayList sortedRolls, int index, HashMap possibleCombinations, StringBuilder sb) { + Category keyObj; + int amountRolls = sortedRolls.size(); + + if ((sortedRolls.get(index) + 1) == sortedRolls.get(index + 1)){ + boolean isNotLargeStraight = false; + + for (int i = 1; i < (amountRolls - 1); i++){ + + if ((sortedRolls.get(i) + 1) != sortedRolls.get(i + 1)){ + isNotLargeStraight = true; + break; + } + } + if (!(isNotLargeStraight)){ + keyObj = possibleCombinations.get("LargeStraight"); + + keyObj.independentValue(); + addCombinationToStringbuilder(sb, keyObj, allValidCombinations); + } + } } - private void addCombinationToStringbuilder(StringBuilder sb, Category keyObj, int amount){ + private void checkForSmallStraight(ArrayList sortedRolls, HashMap possibleCombinations, StringBuilder sb) { + Category keyObj; + + boolean isSmallStraight = false; + for (int i = 0; i < sortedRolls.size() - 3; i++) { // Quelle 3 Anfang + if (sortedRolls.get(i) + 1 == sortedRolls.get(i + 1) // + && sortedRolls.get(i) + 2 == sortedRolls.get(i + 2) // + && sortedRolls.get(i) + 3 == sortedRolls.get(i + 3)) // + { // Quelle 3 Ende + isSmallStraight = true; + break; + } + } + + if (isSmallStraight) { + keyObj = possibleCombinations.get("SmallStraight"); + + keyObj.independentValue(); + addCombinationToStringbuilder(sb, keyObj, allValidCombinations); + } + } + + + private void addCombinationToStringbuilder(StringBuilder sb, Category keyObj, ArrayList allValidCombinations){ int keyValue = keyObj.getValue(); String keyValueString = changeStringFormat(Integer.toString(keyValue), "\u001b[1m"); - sb.append(String.format("%s: %d, Value: %s \n",keyObj, amount, keyValueString)); + + allValidCombinations.add(keyObj); + + int keyAmount = keyObj.getAmount(); + if (keyAmount == 0) { + sb.append(String.format("%s: Value: %s \n", keyObj, keyValueString)); + return; + } + + sb.append(String.format("%s: %d, Value: %s \n",keyObj, keyAmount, keyValueString)); } public void writeToSheet(String sheetInput){ - System.out.printf("Writing on %s sheet \n", getCurrentPlayer()); + HashMap possibleCombinations = createCategoryHashMap(); + + Sheet currentPlayerSheet = getCurrentPlayer().getSheet(); + + Category categoryToWrite = possibleCombinations.get(sheetInput); + + + boolean contains = false; + + for (Category validCombination : allValidCombinations){ + if(validCombination.toString().equals(categoryToWrite.toString())){ + categoryToWrite = validCombination; + contains = true; + break; + } + } + + if (contains){ + currentPlayerSheet.writeCategory(categoryToWrite, false); + }else { + currentPlayerSheet.writeCategory(categoryToWrite, true); + } + } + + + public ArrayList getUnusedRows(){ + Sheet currentPlayerSheet = getCurrentPlayer().getSheet(); + return currentPlayerSheet.getUnusedRows(); + } + + + public boolean checkGameEnd(){ + return getCurrentPlayer().getSheet().checkGameEnd(); } diff --git a/quellen.txt b/quellen.txt index 27c16c8..9a2e97f 100644 --- a/quellen.txt +++ b/quellen.txt @@ -5,3 +5,7 @@ Prompt: "pick a random element from an array list and remove it afterwards" 2: Von KI: ChatGPT 3.5 Prompt: "could you give me 5 popular colors and pink in the ANSI format?" + +3; +von KI: Meta Llama 3 70B +Prompt: "i have an array of 5 ints in java. i want to check if this array contains a small straight, i want it to not be confused with a large straight" diff --git a/templates.txt b/templates.txt index 7382681..bf8fdc9 100644 --- a/templates.txt +++ b/templates.txt @@ -4,4 +4,11 @@ dev(5,5,5,5,5) dev(5,5,5,4,4) -dev(6,6,6,5,5) \ No newline at end of file +dev(6,6,6,5,5) + +dev(1,2,3,4,5) + +dev(1,2,4,5,3) + +dev(1,2,3,4,3) +dev(2,2,3,4,5) diff --git a/tui/TUI.java b/tui/TUI.java index 43a0958..14c7be2 100644 --- a/tui/TUI.java +++ b/tui/TUI.java @@ -1,5 +1,6 @@ package tui; +import domain.Player; import fassade.KniffelSystem; import java.util.ArrayList; @@ -19,7 +20,7 @@ public class TUI { // DEV: gameSystem = new KniffelSystem(); - gameSystem.creteDevPlayers(6); + gameSystem.creteDevPlayers(1); gameLoop(); } @@ -67,6 +68,8 @@ public class TUI { } gameLoop(); + System.out.println("GAME END"); //! TEST + afterGame(); } private static void gameLoop(){ @@ -82,8 +85,12 @@ public class TUI { int rollscount; while (true){ + + if (gameSystem.checkGameEnd()){ + return; + } rollscount = 0; - // System.out.printf("Turn Nr.%d\n", turncounter); + System.out.printf("It's your turn %s!\n\n", gameSystem.getCurrentPlayer()); ArrayList rolls = new ArrayList(); @@ -98,24 +105,34 @@ public class TUI { if (rollscount < 2) { - System.out.println("Which dice do you want to keep?"); + String keeping = gameSystem.changeStringFormat("Which dice do you want to keep?", "\u001B[32m"); + System.out.println(keeping); 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); + triggerNextTurn(); } } private static void writeToSheet(){ - System.out.println("Which row do you want to fill?"); + ArrayList unusedRows = gameSystem.getUnusedRows(); + StringBuilder sb = new StringBuilder(); + + sb.append("Unused rows: \n"); + for (String unusedRow : unusedRows){ + sb.append(String.format("- %s \n", unusedRow)); + } + System.out.println(sb.toString()); + + String keeping = gameSystem.changeStringFormat("Which row do you want to fill?", "\u001B[31m"); + System.out.println(keeping); System.out.print(">"); String sheetInput = sc.nextLine(); gameSystem.writeToSheet(sheetInput); @@ -133,16 +150,15 @@ public class TUI { } - private static int triggerNextTurn(int turnCounter){ - turnCounter++; - System.out.print("Next turn? \n"); - System.out.print("> "); - sc.nextLine(); + private static void triggerNextTurn(){ gameSystem.nextPlayer(); - return turnCounter; } + private static void afterGame(){ + //TODO sheet berechnen, gewinner entscheiden + } + private static void mainMenuLeaderBoard(){ gameSystem = new KniffelSystem(); // Scorboard System