Added dice-roll logic.

Added the whole dice throw and keep logic for every player. Added the first idea of an play sheet.
main
Victor Hans-Georg Waitz 2024-05-04 21:06:05 +02:00
parent 76623aa66f
commit b7731de0b2
5 changed files with 190 additions and 19 deletions

View File

@ -1,6 +1,8 @@
package domain; package domain;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class Game { public class Game {
private ArrayList<Player> currentPlayers; private ArrayList<Player> currentPlayers;
@ -10,13 +12,23 @@ public class Game {
public Game(){ public Game(){
currentPlayers = new ArrayList<Player>(); currentPlayers = new ArrayList<Player>();
turnPlayer = 0; turnPlayer = 0;
dice = new Dice(6); dice = new Dice(8);
}
public void nextTurn(){
Collections.rotate(currentPlayers, -1);
}
public Player getCurrentPlayer(){
return currentPlayers.getFirst();
} }
public int rollDice(){ public int rollDice(){
return dice.roll(); return dice.roll();
} }
public void addPlayer(Player playerToAdd){ public void addPlayer(Player playerToAdd){
currentPlayers.add(playerToAdd); currentPlayers.add(playerToAdd);
} }

View File

@ -5,18 +5,27 @@ public class Player {
String name; String name;
String color; String color;
int score; int score;
Sheet sheet;
public Player(int playerNumber, String name, String color, int score) { public Player(int playerNumber, String name, String color, int score) {
this.playerNumber = playerNumber; this.playerNumber = playerNumber;
this.name = name; this.name = name;
this.color = color; this.color = color;
this.score = score; this.score = score;
this.sheet = new Sheet();
} }
public void newSheet(){
this.sheet = new Sheet();
}
@Override @Override
public String toString() { public String toString() {
String ANSI_RESET = "\u001B[0m"; String ANSI_RESET = "\u001B[0m";
String coloredName = String.format(this.color + this.name + ANSI_RESET); String coloredName = String.format(this.color + this.name + ANSI_RESET);
return String.format("Player %d: %s", this.playerNumber, coloredName); // return String.format("Player %d: %s", this.playerNumber, coloredName);
return String.format("%s", coloredName);
} }
} }

View File

@ -1,7 +1,23 @@
package domain; package domain;
public class Sheet { public class Sheet {
String[] usedRows; private String[] usedRows;
String[] canceledRows; private String[] canceledRows;
String[] emptyRows; private String[] emptyRows;
// Sheet rows, first half
int aces;
int twos;
int threes;
int fours;
int fives;
int sixes;
// Sheet rows, second half
int three_of_kind;
int four_of_kind;
int full_house;
int small_straight;
int large_straight;
int chance;
} }

View File

@ -53,20 +53,95 @@ public class KniffelSystem {
} }
public String getCurrentPlayer(){
return game.getCurrentPlayer().toString();
}
public void nextPlayer(){
game.nextTurn();
}
// TEST
public void createTestPlayers(int amountPlayer){ public void createTestPlayers(int amountPlayer){
String[] names = {"Vic", "Natja", "Lilli", "Emelie", "Esra", "Oli"}; String[] names = {"Vic", "Nastja", "Lilli", "Emelie", "Esra", "Oli"};
for (int i = 0; i < amountPlayer; i++){ for (int i = 0; i < amountPlayer; i++){
addPlayer(i+1, names[i]); addPlayer(i+1, names[i]);
} }
} }
public void rollDice(int amountRolls){ public ArrayList<Integer> rollDices(ArrayList<Integer> rolls, String keptDice){
for (int i = 0; i < amountRolls; i++){ ArrayList<Integer> oldRolls = extractKeptDice(rolls, keptDice);
System.out.println(game.rollDice());
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;
} }
private ArrayList<Integer> extractKeptDice(ArrayList<Integer> previousRolls, String keptDice){
ArrayList<Integer> keptRolls = new ArrayList<Integer>();
if (keptDice.isEmpty()){
return keptRolls;
}
if (keptDice.length() == 1){
int singleIndex = Integer.parseInt(keptDice);
keptRolls.add(previousRolls.get(singleIndex - 1));
return keptRolls;
}
keptDice = keptDice.replaceAll("\\s+","");
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) {
int index = Integer.parseInt(keptDiceIndicesString);
keptRolls.add(previousRolls.get(index - 1));
}
return keptRolls;
}
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;
}
for (int i = 0; i < amountRolls; i++){
rolls.add(game.rollDice());
}
return rolls;
}
// TEST // TEST
public String[] getAllPlayerStrings(){ public String[] getAllPlayerStrings(){
ArrayList<Player> players = game.getPlayers(); ArrayList<Player> players = game.getPlayers();

View File

@ -2,6 +2,7 @@ package tui;
import fassade.KniffelSystem; import fassade.KniffelSystem;
import java.util.ArrayList;
import java.util.Scanner; import java.util.Scanner;
public class TUI { public class TUI {
@ -11,14 +12,15 @@ public class TUI {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Welcome to the PR2 Kniffel game!"); System.out.println("Welcome to the PR2 Kniffel game!");
// while (true){ while (true){
// mainMenuOutput(); mainMenuOutput();
// } }
// DEV: // DEV:
gameSystem = new KniffelSystem(); // gameSystem = new KniffelSystem();
gameLoop(); // gameSystem.createTestPlayers(6);
// gameLoop();
} }
private static int mainMenuOutput(){ private static int mainMenuOutput(){
@ -69,24 +71,84 @@ public class TUI {
String coloredPlayerName = gameSystem.addPlayer(i+1, playerName); String coloredPlayerName = gameSystem.addPlayer(i+1, playerName);
System.out.printf("Welcome %s! \n\n", coloredPlayerName); System.out.printf("Welcome %s! \n\n", coloredPlayerName);
} }
gameLoop();
} }
private static void gameLoop(){ private static void gameLoop(){
gameSystem.createTestPlayers(6);
String[] playerStrings = gameSystem.getAllPlayerStrings(); String[] playerStrings = gameSystem.getAllPlayerStrings();
System.out.println("Participating players:");
for (String player : playerStrings){ for (String player : playerStrings){
System.out.println(player); System.out.println(player);
} }
System.out.println();
gameSystem.rollDice(40); int turncounter = 0;
int rollscount;
while (true){
rollscount = 0;
// System.out.printf("Turn Nr.%d\n", turncounter);
System.out.printf("It's your turn %s!\n\n", gameSystem.getCurrentPlayer());
ArrayList<Integer> rolls = new ArrayList<Integer>();
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);
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++;
}
turncounter = triggerNextTurn(turncounter);
}
} }
private static String evaluateRoll(ArrayList<Integer> rolls){
return "TODO";
}
private static String diceArrToString(ArrayList<Integer> diceArr){
StringBuilder sb = new StringBuilder();
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))));
}
return sb.toString();
}
private static int triggerNextTurn(int turnCounter){
turnCounter++;
System.out.print("Next turn? \n");
System.out.print("> ");
sc.nextLine();
gameSystem.nextPlayer();
return turnCounter;
}
private static void mainMenuLeaderBoard(){ private static void mainMenuLeaderBoard(){
gameSystem = new KniffelSystem(); // Scorboard System gameSystem = new KniffelSystem(); // Scorboard System
System.out.println(gameSystem.LeaderBaordData()); // TODO System.out.println(gameSystem.LeaderBaordData()); // TODO
} }
private static void mainMenuExit(){ private static void mainMenuExit(){
System.out.println("Do you really want to exit? (Y/n)"); System.out.println("Do you really want to exit? (Y/n)");
System.out.print("> "); System.out.print("> ");
@ -103,8 +165,5 @@ public class TUI {
System.out.println(); System.out.println();
mainMenuOutput(); mainMenuOutput();
} }
System.out.printf("|%s| \nIs blank? %b\n", mainMenuExitUserInput, mainMenuExitUserInput.isBlank()); // TEST
} }
} }