Added evaluation for the lowe block half
Added automatic value calculation when setting or adding the amount of a Category object. Added value calculation for the lower half of the block categories.main
parent
495306ecb5
commit
da39625783
|
@ -18,12 +18,13 @@ public class Sheet {
|
|||
Sixes sixes;
|
||||
|
||||
// Sheet rows, second half
|
||||
int three_of_kind;
|
||||
int four_of_kind;
|
||||
int full_house;
|
||||
int small_straight;
|
||||
int large_straight;
|
||||
int chance;
|
||||
ThreeOfKind threeOfKind;
|
||||
FourOfKind fourOfKind;
|
||||
FullHouse fullHouse;
|
||||
SmallStraight smallStraight;
|
||||
LargeStraight largeStraight;
|
||||
Yahtzee yahtzee;
|
||||
Chance chance;
|
||||
|
||||
|
||||
public Sheet(){
|
||||
|
@ -33,9 +34,20 @@ public class Sheet {
|
|||
this.fours = new Fours();
|
||||
this.fives = new Fives();
|
||||
this.sixes = new Sixes();
|
||||
|
||||
this.threeOfKind = new ThreeOfKind();
|
||||
this.fourOfKind = new FourOfKind();
|
||||
this.fullHouse = new FullHouse();
|
||||
this.smallStraight = new SmallStraight();
|
||||
this.largeStraight = new LargeStraight();
|
||||
this.yahtzee = new Yahtzee();
|
||||
this.chance = new Chance();
|
||||
}
|
||||
|
||||
|
||||
public void writeCategory(String category)
|
||||
|
||||
|
||||
public HashMap<String, Category> getAllCategories(){
|
||||
HashMap<String, Category> allCategories = new HashMap<>();
|
||||
|
||||
|
@ -46,6 +58,14 @@ public class Sheet {
|
|||
allCategories.put("Fives", fives);
|
||||
allCategories.put("Sixes", sixes);
|
||||
|
||||
allCategories.put("ThreeOfKind", threeOfKind);
|
||||
allCategories.put("FourOfKind", fourOfKind);
|
||||
allCategories.put("FullHouse", fullHouse);
|
||||
allCategories.put("SmallStraight", smallStraight);
|
||||
allCategories.put("LargeStraight", largeStraight);
|
||||
allCategories.put("Yahtzee", yahtzee);
|
||||
allCategories.put("Chance", chance);
|
||||
|
||||
return allCategories;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package domain.sheets;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Category {
|
||||
int amount = 0;
|
||||
int value = 0;
|
||||
|
@ -10,6 +12,10 @@ public class Category {
|
|||
return amount;
|
||||
}
|
||||
|
||||
public void calcValueFromRoll(ArrayList<Integer> rolls){}
|
||||
|
||||
public void independentValue(){}
|
||||
|
||||
|
||||
//? Amount
|
||||
public int getAmount() {
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package domain.sheets;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Chance extends Category{
|
||||
|
||||
@Override
|
||||
public void calcValueFromRoll(ArrayList<Integer> rolls) {
|
||||
int sum = 0;
|
||||
for(int roll : rolls){
|
||||
sum += roll;
|
||||
}
|
||||
|
||||
this.value = sum;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package domain.sheets;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class FourOfKind extends Category {
|
||||
|
||||
@Override
|
||||
public void calcValueFromRoll(ArrayList<Integer> rolls) {
|
||||
int sum = 0;
|
||||
for(int roll : rolls){
|
||||
sum += roll;
|
||||
}
|
||||
|
||||
this.value = sum;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package domain.sheets;
|
||||
|
||||
public class FullHouse extends Category{
|
||||
|
||||
@Override
|
||||
public void independentValue(){
|
||||
this.value = 25;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package domain.sheets;
|
||||
|
||||
public class LargeStraight extends Category{
|
||||
|
||||
@Override
|
||||
public void independentValue() {
|
||||
this.value = 40;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package domain.sheets;
|
||||
|
||||
public class SmallStraight extends Category{
|
||||
|
||||
@Override
|
||||
public void independentValue() {
|
||||
this.value = 30;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package domain.sheets;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ThreeOfKind extends Category {
|
||||
|
||||
@Override
|
||||
public void calcValueFromRoll(ArrayList<Integer> rolls) {
|
||||
int sum = 0;
|
||||
for(int roll : rolls){
|
||||
sum += roll;
|
||||
}
|
||||
|
||||
this.value = sum;
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
package domain.sheets;
|
||||
|
||||
public class Three_of_kind extends Category {
|
||||
@Override
|
||||
public int calcValueFromAmount() {
|
||||
this.value = this.amount * 2;
|
||||
return this.value;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package domain.sheets;
|
||||
|
||||
public class Yahtzee extends Category{
|
||||
|
||||
@Override
|
||||
public void independentValue() {
|
||||
this.value = 50;
|
||||
}
|
||||
}
|
|
@ -2,10 +2,10 @@ package fassade;
|
|||
|
||||
import domain.Game;
|
||||
import domain.Player;
|
||||
import domain.Sheet;
|
||||
import domain.sheets.Category;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import java.util.*;
|
||||
|
||||
public class KniffelSystem {
|
||||
ArrayList<String> playerColors;
|
||||
|
@ -31,14 +31,14 @@ public class KniffelSystem {
|
|||
Player playerToAdd = new Player(playerNumber, name, playerColor, 0);
|
||||
game.addPlayer(playerToAdd);
|
||||
|
||||
return changePlayerNameColor(name, playerColor);
|
||||
return changeStringFormat(name, playerColor);
|
||||
}
|
||||
|
||||
|
||||
private String changePlayerNameColor(String name, String color){
|
||||
private String changeStringFormat(String string, String formatation){
|
||||
String ANSI_RESET = "\u001B[0m";
|
||||
|
||||
return String.format(color + name + ANSI_RESET);
|
||||
return String.format(formatation + string + ANSI_RESET);
|
||||
}
|
||||
|
||||
|
||||
|
@ -53,8 +53,8 @@ public class KniffelSystem {
|
|||
}
|
||||
|
||||
|
||||
public String getCurrentPlayer(){
|
||||
return game.getCurrentPlayer().toString();
|
||||
public Player getCurrentPlayer(){
|
||||
return game.getCurrentPlayer();
|
||||
}
|
||||
|
||||
|
||||
|
@ -63,8 +63,7 @@ public class KniffelSystem {
|
|||
}
|
||||
|
||||
|
||||
// TEST
|
||||
public void createTestPlayers(int amountPlayer){
|
||||
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]);
|
||||
|
@ -83,19 +82,6 @@ public class KniffelSystem {
|
|||
int amountNewRolls = oldRolls.size();
|
||||
ArrayList<Integer> newRolls = rollMultipleDice(5 - amountNewRolls);
|
||||
|
||||
// --TEST
|
||||
System.out.println("Old rolls:");
|
||||
for(int oldRoll: oldRolls){
|
||||
System.out.println(oldRoll);
|
||||
}
|
||||
|
||||
System.out.println("New Rolls:");
|
||||
for(int newRoll: newRolls){
|
||||
System.out.println(newRoll);
|
||||
}
|
||||
System.out.println();
|
||||
// TEST--
|
||||
|
||||
oldRolls.addAll(newRolls);
|
||||
|
||||
return oldRolls;
|
||||
|
@ -118,11 +104,8 @@ public class KniffelSystem {
|
|||
return keptRolls;
|
||||
}
|
||||
|
||||
|
||||
keptDice = keptDice.substring(1, keptDice.length()-1);
|
||||
|
||||
System.out.printf("Edited keptDice String: %s \n", keptDice); //! TEST
|
||||
|
||||
String[] keptDiceIndicesStrings = keptDice.split(",");
|
||||
|
||||
for (String keptDiceIndicesString : keptDiceIndicesStrings) {
|
||||
|
@ -135,7 +118,6 @@ public class KniffelSystem {
|
|||
|
||||
|
||||
private ArrayList<Integer> rollMultipleDice(int amountRolls){
|
||||
System.out.printf("Amount rolls: %d \n", amountRolls); //! TEST
|
||||
ArrayList<Integer> rolls = new ArrayList<>();
|
||||
if (amountRolls == 0){
|
||||
return rolls;
|
||||
|
@ -148,6 +130,193 @@ public class KniffelSystem {
|
|||
}
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
private ArrayList<Integer> createDevRoll(String keptDice){
|
||||
// Format: dev(1,2,3,4,5)
|
||||
// values aren't indices, they are the dice value
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
dev(5,5,5,5,4)
|
||||
|
||||
dev(5,5,5,5,5)
|
||||
|
||||
dev(5,5,5,4,4)
|
||||
|
||||
dev(6,6,6,5,5)
|
93
tui/TUI.java
93
tui/TUI.java
|
@ -1,13 +1,9 @@
|
|||
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;
|
||||
|
@ -23,11 +19,11 @@ public class TUI {
|
|||
|
||||
// DEV:
|
||||
gameSystem = new KniffelSystem();
|
||||
gameSystem.createTestPlayers(6);
|
||||
gameSystem.creteDevPlayers(6);
|
||||
gameLoop();
|
||||
}
|
||||
|
||||
private static int mainMenuOutput(){
|
||||
private static void mainMenuOutput(){
|
||||
System.out.println("What do you want to do?");
|
||||
System.out.println("1 - Play");
|
||||
System.out.println("2 - See leaderboard");
|
||||
|
@ -39,21 +35,15 @@ public class TUI {
|
|||
if ((mainMenuUserInput.equals("1"))
|
||||
|| (mainMenuUserInput.equals("play"))){
|
||||
mainMenuPlay();
|
||||
System.out.println("play"); // TEST
|
||||
return 1;
|
||||
}
|
||||
else if ((mainMenuUserInput.equals("2"))
|
||||
|| (mainMenuUserInput.equals("see leaderboard"))
|
||||
|| (mainMenuUserInput.equals("see"))
|
||||
|| (mainMenuUserInput.equals("leaderboard"))){
|
||||
System.out.println("leaderboard"); // TEST
|
||||
mainMenuLeaderBoard();
|
||||
return 2;
|
||||
}
|
||||
else {
|
||||
System.out.println("exit"); // TEST
|
||||
mainMenuExit();
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -100,94 +90,41 @@ public class TUI {
|
|||
String keptDice = "";
|
||||
|
||||
while(rollscount < 3){
|
||||
System.out.printf("Roll Nr. %d \n", rollscount); // TEST
|
||||
System.out.printf("Kept dice: %s \n\n", keptDice); // TEST
|
||||
|
||||
|
||||
rolls = gameSystem.rollDices(rolls, keptDice);
|
||||
String newRollsString = diceArrToString(rolls);
|
||||
System.out.println(newRollsString);
|
||||
evaluateRoll(rolls);
|
||||
String evaluatedRolls = gameSystem.evaluateRoll(rolls);
|
||||
System.out.println(evaluatedRolls);
|
||||
|
||||
|
||||
if (rollscount < 2) {
|
||||
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.");
|
||||
System.out.print("> ");
|
||||
keptDice = sc.nextLine();
|
||||
}
|
||||
|
||||
rollscount++;
|
||||
}
|
||||
|
||||
writeToSheet();
|
||||
|
||||
turncounter = triggerNextTurn(turncounter);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static String evaluateRoll(ArrayList<Integer> rolls){
|
||||
HashMap<String, Category> possibleCombinations = createCategorieHashMap();
|
||||
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;
|
||||
private static void writeToSheet(){
|
||||
System.out.println("Which row do you want to fill?");
|
||||
System.out.print(">");
|
||||
String sheetInput = sc.nextLine();
|
||||
gameSystem.writeToSheet(sheetInput);
|
||||
}
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Possible combinations: \n");
|
||||
|
||||
Set<String> 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<String, Category> createCategorieHashMap(){
|
||||
// TODO starwars sheet implementieren
|
||||
Sheet sheet = new Sheet();
|
||||
|
||||
return sheet.getAllCategories();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static String diceArrToString(ArrayList<Integer> diceArr){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Throwing dice... \n");
|
||||
sb.append("Your rolls: \n");
|
||||
for (int i = 0; i < diceArr.size(); i++){
|
||||
sb.append(String.format("Dice %d: %s \n", i+1, Integer.toString(diceArr.get(i))));
|
||||
|
|
Loading…
Reference in New Issue