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.
main
Victor Hans-Georg Waitz 2024-05-06 17:19:11 +02:00
parent da39625783
commit 9790370942
7 changed files with 299 additions and 96 deletions

View File

@ -20,6 +20,9 @@ public class Player {
this.sheet = new Sheet(); this.sheet = new Sheet();
} }
public Sheet getSheet(){
return this.sheet;
}
@Override @Override
public String toString() { public String toString() {

View File

@ -2,12 +2,14 @@ package domain;
import domain.sheets.*; import domain.sheets.*;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
public class Sheet { public class Sheet {
private String[] usedRows; int amountTurns;
private String[] canceledRows; ArrayList<String> unusedRows = new ArrayList<>();
private String[] emptyRows; ArrayList<String> usedRows = new ArrayList<>();
ArrayList<String> crossedRows = new ArrayList<>();
// Sheet rows, first half // Sheet rows, first half
Aces aces; Aces aces;
@ -29,23 +31,136 @@ public class Sheet {
public Sheet(){ public Sheet(){
this.aces = new Aces(); this.aces = new Aces();
unusedRows.add(this.aces.toString());
this.twos = new Twos(); this.twos = new Twos();
unusedRows.add(this.twos.toString());
this.threes = new Threes(); this.threes = new Threes();
unusedRows.add(this.threes.toString());
this.fours = new Fours(); this.fours = new Fours();
unusedRows.add(this.fours.toString());
this.fives = new Fives(); this.fives = new Fives();
unusedRows.add(this.fives.toString());
this.sixes = new Sixes(); this.sixes = new Sixes();
unusedRows.add(this.sixes.toString());
this.threeOfKind = new ThreeOfKind(); this.threeOfKind = new ThreeOfKind();
unusedRows.add(this.threeOfKind.toString());
this.fourOfKind = new FourOfKind(); this.fourOfKind = new FourOfKind();
unusedRows.add(this.fourOfKind.toString());
this.fullHouse = new FullHouse(); this.fullHouse = new FullHouse();
unusedRows.add(this.fullHouse.toString());
this.smallStraight = new SmallStraight(); this.smallStraight = new SmallStraight();
unusedRows.add(this.smallStraight.toString());
this.largeStraight = new LargeStraight(); this.largeStraight = new LargeStraight();
unusedRows.add(this.largeStraight.toString());
this.yahtzee = new Yahtzee(); this.yahtzee = new Yahtzee();
unusedRows.add(this.yahtzee.toString());
this.chance = new Chance(); 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<String> getCrossedRows(){
return crossedRows;
}
public ArrayList<String> getUnusedRows(){
return unusedRows;
}
public ArrayList<String> getUsedRows(){
return usedRows;
}
public HashMap<String, Category> getAllCategories(){ public HashMap<String, Category> getAllCategories(){

View File

@ -1,5 +1,28 @@
package fassade; package fassade;
public class GameCycle { 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,
} }

View File

@ -9,6 +9,7 @@ import java.util.*;
public class KniffelSystem { public class KniffelSystem {
ArrayList<String> playerColors; ArrayList<String> playerColors;
ArrayList<Category> allValidCombinations = new ArrayList<>();
Game game; Game game;
public KniffelSystem(){ public KniffelSystem(){
game = new Game(); 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"; String ANSI_RESET = "\u001B[0m";
return String.format(formatation + string + ANSI_RESET); return String.format(formatation + string + ANSI_RESET);
@ -132,7 +133,8 @@ public class KniffelSystem {
public String evaluateRoll(ArrayList<Integer> rolls){ public String evaluateRoll(ArrayList<Integer> rolls){
HashMap<String, Category> possibleCombinations = createCategoryHashMap(); HashMap<String, Category> possibleCombinations = createCategoryHashMap();
ArrayList<Category> validCombinations = new ArrayList<>(); ArrayList<Category> validUpperCombinations = new ArrayList<>();
allValidCombinations = new ArrayList<>();
//TODO Add starwars logic //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("Possible combinations:", "\u001B[32m")));
sb.append(String.format("%s \n", changeStringFormat("Upper half:", "\u001b[4m"))); 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){ for (String key : possibleCombinations.keySet()) {
validCombinations.add(keyObj); Category keyObj = possibleCombinations.get(key);
addCombinationToStringbuilder(sb, keyObj, keyAmount);
if (keyObj.getAmount() != 0){
validUpperCombinations.add(keyObj);
addCombinationToStringbuilder(sb, keyObj, allValidCombinations);
} }
} }
@ -182,14 +181,14 @@ public class KniffelSystem {
int index = 0; int index = 0;
Category keyObj; Category keyObj;
for(Category category : validCombinations){ for(Category category : validUpperCombinations){
//? Three of a kind //? Three of a kind
if (category.getAmount() >= 3){ if (category.getAmount() >= 3){
keyObj = possibleCombinations.get("ThreeOfKind"); keyObj = possibleCombinations.get("ThreeOfKind");
keyObj.calcValueFromRoll(rolls); keyObj.calcValueFromRoll(rolls);
addCombinationToStringbuilder(sb, keyObj); addCombinationToStringbuilder(sb, keyObj, allValidCombinations);
} }
//? Four of a kind //? Four of a kind
@ -197,76 +196,33 @@ public class KniffelSystem {
keyObj = possibleCombinations.get("FourOfKind"); keyObj = possibleCombinations.get("FourOfKind");
keyObj.calcValueFromRoll(rolls); keyObj.calcValueFromRoll(rolls);
addCombinationToStringbuilder(sb, keyObj); addCombinationToStringbuilder(sb, keyObj, allValidCombinations);
} }
//? Full House //? Full House
if (category.getAmount() == 3){ if (category.getAmount() == 3){
for(Category innerCategory : validCombinations){ for(Category innerCategory : validUpperCombinations){
if ((innerCategory.getAmount() == 2) && !(innerCategory.toString().equals(category.toString()))){ if ((innerCategory.getAmount() == 2) && !(innerCategory.toString().equals(category.toString()))){
keyObj = possibleCombinations.get("FullHouse"); keyObj = possibleCombinations.get("FullHouse");
keyObj.independentValue(); keyObj.independentValue();
addCombinationToStringbuilder(sb, keyObj); addCombinationToStringbuilder(sb, keyObj, allValidCombinations);
} }
} }
} }
int amountRolls = rolls.size(); ArrayList<Integer> sortedRolls = new ArrayList<>(rolls);
Collections.sort(sortedRolls);
//? Small Straight //? Small Straight
if (index == 0) { if (index == 0) {
boolean isNotSmallStraight = false; checkForSmallStraight(sortedRolls, possibleCombinations, sb);
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 //? Large Straight
if (index == 0){ if (index == 0){
if ((rolls.get(index) + 1) == rolls.get(index + 1)){ checkForLargeStraight(sortedRolls, index, possibleCombinations, sb);
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 //? Yahtzee
@ -274,7 +230,7 @@ public class KniffelSystem {
keyObj = possibleCombinations.get("Yahtzee"); keyObj = possibleCombinations.get("Yahtzee");
keyObj.independentValue(); keyObj.independentValue();
addCombinationToStringbuilder(sb, keyObj); addCombinationToStringbuilder(sb, keyObj, allValidCombinations);
} }
index ++; index ++;
@ -283,29 +239,108 @@ public class KniffelSystem {
keyObj = possibleCombinations.get("Chance"); keyObj = possibleCombinations.get("Chance");
keyObj.calcValueFromRoll(rolls); keyObj.calcValueFromRoll(rolls);
addCombinationToStringbuilder(sb, keyObj); addCombinationToStringbuilder(sb, keyObj, allValidCombinations);
return sb.toString(); return sb.toString();
} }
private void addCombinationToStringbuilder(StringBuilder sb, Category keyObj){ private void checkForLargeStraight(ArrayList<Integer> sortedRolls, int index, HashMap<String, Category> possibleCombinations, StringBuilder sb) {
int keyValue = keyObj.getValue(); Category keyObj;
String keyValueString = changeStringFormat(Integer.toString(keyValue), "\u001b[1m"); int amountRolls = sortedRolls.size();
sb.append(String.format("%s: Value: %s \n",keyObj, keyValueString));
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<Integer> sortedRolls, HashMap<String, Category> 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<Category> allValidCombinations){
int keyValue = keyObj.getValue(); int keyValue = keyObj.getValue();
String keyValueString = changeStringFormat(Integer.toString(keyValue), "\u001b[1m"); 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){ public void writeToSheet(String sheetInput){
System.out.printf("Writing on %s sheet \n", getCurrentPlayer()); HashMap<String, Category> 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<String> getUnusedRows(){
Sheet currentPlayerSheet = getCurrentPlayer().getSheet();
return currentPlayerSheet.getUnusedRows();
}
public boolean checkGameEnd(){
return getCurrentPlayer().getSheet().checkGameEnd();
} }

View File

@ -5,3 +5,7 @@ Prompt: "pick a random element from an array list and remove it afterwards"
2: 2:
Von KI: ChatGPT 3.5 Von KI: ChatGPT 3.5
Prompt: "could you give me 5 popular colors and pink in the ANSI format?" 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"

View File

@ -5,3 +5,10 @@ dev(5,5,5,5,5)
dev(5,5,5,4,4) dev(5,5,5,4,4)
dev(6,6,6,5,5) 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)

View File

@ -1,5 +1,6 @@
package tui; package tui;
import domain.Player;
import fassade.KniffelSystem; import fassade.KniffelSystem;
import java.util.ArrayList; import java.util.ArrayList;
@ -19,7 +20,7 @@ public class TUI {
// DEV: // DEV:
gameSystem = new KniffelSystem(); gameSystem = new KniffelSystem();
gameSystem.creteDevPlayers(6); gameSystem.creteDevPlayers(1);
gameLoop(); gameLoop();
} }
@ -67,6 +68,8 @@ public class TUI {
} }
gameLoop(); gameLoop();
System.out.println("GAME END"); //! TEST
afterGame();
} }
private static void gameLoop(){ private static void gameLoop(){
@ -82,8 +85,12 @@ public class TUI {
int rollscount; int rollscount;
while (true){ while (true){
if (gameSystem.checkGameEnd()){
return;
}
rollscount = 0; rollscount = 0;
// System.out.printf("Turn Nr.%d\n", turncounter);
System.out.printf("It's your turn %s!\n\n", gameSystem.getCurrentPlayer()); System.out.printf("It's your turn %s!\n\n", gameSystem.getCurrentPlayer());
ArrayList<Integer> rolls = new ArrayList<Integer>(); ArrayList<Integer> rolls = new ArrayList<Integer>();
@ -98,24 +105,34 @@ public class TUI {
if (rollscount < 2) { 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.println("Empty for none, single digit for one dice or (1,3,4) for multiple dice.");
System.out.print("> "); System.out.print("> ");
keptDice = sc.nextLine(); keptDice = sc.nextLine();
} }
rollscount++; rollscount++;
} }
writeToSheet(); writeToSheet();
turncounter = triggerNextTurn(turncounter); triggerNextTurn();
} }
} }
private static void writeToSheet(){ private static void writeToSheet(){
System.out.println("Which row do you want to fill?"); ArrayList<String> 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(">"); System.out.print(">");
String sheetInput = sc.nextLine(); String sheetInput = sc.nextLine();
gameSystem.writeToSheet(sheetInput); gameSystem.writeToSheet(sheetInput);
@ -133,16 +150,15 @@ public class TUI {
} }
private static int triggerNextTurn(int turnCounter){ private static void triggerNextTurn(){
turnCounter++;
System.out.print("Next turn? \n");
System.out.print("> ");
sc.nextLine();
gameSystem.nextPlayer(); gameSystem.nextPlayer();
return turnCounter;
} }
private static void afterGame(){
//TODO sheet berechnen, gewinner entscheiden
}
private static void mainMenuLeaderBoard(){ private static void mainMenuLeaderBoard(){
gameSystem = new KniffelSystem(); // Scorboard System gameSystem = new KniffelSystem(); // Scorboard System