2024-04-29 21:06:04 +02:00
|
|
|
package fassade;
|
|
|
|
|
|
|
|
import domain.Game;
|
|
|
|
import domain.Player;
|
2024-05-06 02:35:10 +02:00
|
|
|
import domain.Sheet;
|
|
|
|
import domain.sheets.Category;
|
2024-04-29 21:06:04 +02:00
|
|
|
|
2024-05-06 02:35:10 +02:00
|
|
|
import java.util.*;
|
2024-04-29 21:06:04 +02:00
|
|
|
|
|
|
|
public class KniffelSystem {
|
|
|
|
ArrayList<String> 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
|
|
|
|
}
|
|
|
|
|
2024-04-29 21:17:51 +02:00
|
|
|
|
2024-04-30 12:46:42 +02:00
|
|
|
public String LeaderBaordData(){
|
|
|
|
return "Leaderboard - TODO \n";
|
2024-04-29 21:06:04 +02:00
|
|
|
}
|
|
|
|
|
2024-04-29 21:17:51 +02:00
|
|
|
|
2024-04-29 21:06:04 +02:00
|
|
|
public String addPlayer(int playerNumber, String name) {
|
|
|
|
String playerColor = colorPicker(playerNumber);
|
|
|
|
Player playerToAdd = new Player(playerNumber, name, playerColor, 0);
|
|
|
|
game.addPlayer(playerToAdd);
|
|
|
|
|
2024-05-06 02:35:10 +02:00
|
|
|
return changeStringFormat(name, playerColor);
|
2024-04-29 21:06:04 +02:00
|
|
|
}
|
|
|
|
|
2024-04-29 21:17:51 +02:00
|
|
|
|
2024-05-06 02:35:10 +02:00
|
|
|
private String changeStringFormat(String string, String formatation){
|
2024-04-29 21:06:04 +02:00
|
|
|
String ANSI_RESET = "\u001B[0m";
|
|
|
|
|
2024-05-06 02:35:10 +02:00
|
|
|
return String.format(formatation + string + ANSI_RESET);
|
2024-04-29 21:06:04 +02:00
|
|
|
}
|
|
|
|
|
2024-04-29 21:17:51 +02:00
|
|
|
|
2024-04-29 21:06:04 +02:00
|
|
|
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
|
|
|
|
}
|
2024-04-30 12:46:42 +02:00
|
|
|
|
|
|
|
|
2024-05-06 02:35:10 +02:00
|
|
|
public Player getCurrentPlayer(){
|
|
|
|
return game.getCurrentPlayer();
|
2024-05-04 21:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void nextPlayer(){
|
|
|
|
game.nextTurn();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-06 02:35:10 +02:00
|
|
|
public void creteDevPlayers(int amountPlayer){
|
2024-05-04 21:06:05 +02:00
|
|
|
String[] names = {"Vic", "Nastja", "Lilli", "Emelie", "Esra", "Oli"};
|
2024-04-30 12:46:42 +02:00
|
|
|
for (int i = 0; i < amountPlayer; i++){
|
|
|
|
addPlayer(i+1, names[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-04 21:06:05 +02:00
|
|
|
public ArrayList<Integer> rollDices(ArrayList<Integer> rolls, String keptDice){
|
2024-05-05 18:12:50 +02:00
|
|
|
//? DEV TEST
|
|
|
|
if (keptDice.startsWith("dev")){
|
|
|
|
return createDevRoll(keptDice);
|
|
|
|
}
|
|
|
|
|
2024-05-04 21:06:05 +02:00
|
|
|
ArrayList<Integer> oldRolls = extractKeptDice(rolls, keptDice);
|
|
|
|
|
|
|
|
int amountNewRolls = oldRolls.size();
|
|
|
|
ArrayList<Integer> newRolls = rollMultipleDice(5 - amountNewRolls);
|
|
|
|
|
|
|
|
oldRolls.addAll(newRolls);
|
|
|
|
|
|
|
|
return oldRolls;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private ArrayList<Integer> extractKeptDice(ArrayList<Integer> previousRolls, String keptDice){
|
|
|
|
ArrayList<Integer> keptRolls = new ArrayList<Integer>();
|
|
|
|
|
|
|
|
if (keptDice.isEmpty()){
|
|
|
|
return keptRolls;
|
|
|
|
}
|
|
|
|
|
2024-05-05 18:12:50 +02:00
|
|
|
//? Remove whitespaces
|
|
|
|
keptDice = keptDice.replaceAll("\\s+","");
|
|
|
|
|
2024-05-04 21:06:05 +02:00
|
|
|
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<Integer> rollMultipleDice(int amountRolls){
|
|
|
|
ArrayList<Integer> rolls = new ArrayList<>();
|
|
|
|
if (amountRolls == 0){
|
|
|
|
return rolls;
|
|
|
|
}
|
|
|
|
|
2024-04-30 12:46:42 +02:00
|
|
|
for (int i = 0; i < amountRolls; i++){
|
2024-05-04 21:06:05 +02:00
|
|
|
rolls.add(game.rollDice());
|
2024-04-30 12:46:42 +02:00
|
|
|
}
|
2024-05-04 21:06:05 +02:00
|
|
|
return rolls;
|
2024-04-30 12:46:42 +02:00
|
|
|
}
|
|
|
|
|
2024-05-04 21:06:05 +02:00
|
|
|
|
2024-05-06 02:35:10 +02:00
|
|
|
public String evaluateRoll(ArrayList<Integer> rolls){
|
|
|
|
HashMap<String, Category> possibleCombinations = createCategoryHashMap();
|
|
|
|
ArrayList<Category> 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<String> 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<String, Category> createCategoryHashMap(){
|
|
|
|
// TODO starwars sheet implementieren
|
|
|
|
Sheet sheet = new Sheet();
|
|
|
|
|
|
|
|
return sheet.getAllCategories();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-05 18:12:50 +02:00
|
|
|
private ArrayList<Integer> createDevRoll(String keptDice){
|
|
|
|
// Format: dev(1,2,3,4,5)
|
|
|
|
// values aren't indices, they are the dice value
|
|
|
|
ArrayList<Integer> 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
|
2024-04-30 12:46:42 +02:00
|
|
|
public String[] getAllPlayerStrings(){
|
|
|
|
ArrayList<Player> 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;
|
|
|
|
}
|
2024-04-29 21:06:04 +02:00
|
|
|
}
|