package fassade; import domain.Game; import domain.Player; import domain.Sheet; import domain.sheets.Category; import java.util.*; public class KniffelSystem { ArrayList playerColors; Game game; public KniffelSystem(){ game = new Game(); playerColors = new ArrayList<>(Arrays.asList( "\u001B[31m", // Quelle 2 Anfang "\u001B[32m", // "\u001B[34m", // "\u001B[33m", // "\u001B[36m")); // Quelle 2 Ende } public String LeaderBaordData(){ return "Leaderboard - TODO \n"; } public String addPlayer(int playerNumber, String name) { String playerColor = colorPicker(playerNumber); Player playerToAdd = new Player(playerNumber, name, playerColor, 0); game.addPlayer(playerToAdd); return changeStringFormat(name, playerColor); } private String changeStringFormat(String string, String formatation){ String ANSI_RESET = "\u001B[0m"; return String.format(formatation + string + ANSI_RESET); } private String colorPicker(int playerNumber){ if (playerNumber == 1){ return "\u001B[35m"; // Quelle 2 } Random rand = new Random(); // Quelle 1 Anfang int randomIndex = rand.nextInt(playerColors.size()); // return playerColors.remove(randomIndex); // Quelle 1 Ende } public Player getCurrentPlayer(){ return game.getCurrentPlayer(); } public void nextPlayer(){ game.nextTurn(); } 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]); } } 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(); ArrayList newRolls = rollMultipleDice(5 - amountNewRolls); oldRolls.addAll(newRolls); return oldRolls; } private ArrayList extractKeptDice(ArrayList previousRolls, String keptDice){ ArrayList keptRolls = new ArrayList(); if (keptDice.isEmpty()){ return keptRolls; } //? Remove whitespaces keptDice = keptDice.replaceAll("\\s+",""); if (keptDice.length() == 1){ int singleIndex = Integer.parseInt(keptDice); keptRolls.add(previousRolls.get(singleIndex - 1)); return keptRolls; } keptDice = keptDice.substring(1, keptDice.length()-1); String[] keptDiceIndicesStrings = keptDice.split(","); for (String keptDiceIndicesString : keptDiceIndicesStrings) { int index = Integer.parseInt(keptDiceIndicesString); keptRolls.add(previousRolls.get(index - 1)); } return keptRolls; } private ArrayList rollMultipleDice(int amountRolls){ ArrayList rolls = new ArrayList<>(); if (amountRolls == 0){ return rolls; } for (int i = 0; i < amountRolls; i++){ rolls.add(game.rollDice()); } return rolls; } 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 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()]; for (int i = 0; i < players.size(); i++){ returnStrings[i] = players.get(i).toString(); } return returnStrings; } }