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;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class Game {
private ArrayList<Player> currentPlayers;
@ -10,13 +12,23 @@ public class Game {
public Game(){
currentPlayers = new ArrayList<Player>();
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(){
return dice.roll();
}
public void addPlayer(Player playerToAdd){
currentPlayers.add(playerToAdd);
}

View File

@ -5,18 +5,27 @@ public class Player {
String name;
String color;
int score;
Sheet sheet;
public Player(int playerNumber, String name, String color, int score) {
this.playerNumber = playerNumber;
this.name = name;
this.color = color;
this.score = score;
this.sheet = new Sheet();
}
public void newSheet(){
this.sheet = new Sheet();
}
@Override
public String toString() {
String ANSI_RESET = "\u001B[0m";
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;
public class Sheet {
String[] usedRows;
String[] canceledRows;
String[] emptyRows;
private String[] usedRows;
private String[] canceledRows;
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){
String[] names = {"Vic", "Natja", "Lilli", "Emelie", "Esra", "Oli"};
String[] names = {"Vic", "Nastja", "Lilli", "Emelie", "Esra", "Oli"};
for (int i = 0; i < amountPlayer; i++){
addPlayer(i+1, names[i]);
}
}
public void rollDice(int amountRolls){
public ArrayList<Integer> rollDices(ArrayList<Integer> rolls, String keptDice){
ArrayList<Integer> oldRolls = extractKeptDice(rolls, keptDice);
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++){
System.out.println(game.rollDice());
rolls.add(game.rollDice());
}
return rolls;
}
// TEST
public String[] getAllPlayerStrings(){
ArrayList<Player> players = game.getPlayers();

View File

@ -2,6 +2,7 @@ package tui;
import fassade.KniffelSystem;
import java.util.ArrayList;
import java.util.Scanner;
public class TUI {
@ -11,14 +12,15 @@ public class TUI {
public static void main(String[] args) {
System.out.println("Welcome to the PR2 Kniffel game!");
// while (true){
// mainMenuOutput();
// }
while (true){
mainMenuOutput();
}
// DEV:
gameSystem = new KniffelSystem();
gameLoop();
// gameSystem = new KniffelSystem();
// gameSystem.createTestPlayers(6);
// gameLoop();
}
private static int mainMenuOutput(){
@ -69,24 +71,84 @@ public class TUI {
String coloredPlayerName = gameSystem.addPlayer(i+1, playerName);
System.out.printf("Welcome %s! \n\n", coloredPlayerName);
}
gameLoop();
}
private static void gameLoop(){
gameSystem.createTestPlayers(6);
String[] playerStrings = gameSystem.getAllPlayerStrings();
System.out.println("Participating players:");
for (String player : playerStrings){
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(){
gameSystem = new KniffelSystem(); // Scorboard System
System.out.println(gameSystem.LeaderBaordData()); // TODO
}
private static void mainMenuExit(){
System.out.println("Do you really want to exit? (Y/n)");
System.out.print("> ");
@ -103,8 +165,5 @@ public class TUI {
System.out.println();
mainMenuOutput();
}
System.out.printf("|%s| \nIs blank? %b\n", mainMenuExitUserInput, mainMenuExitUserInput.isBlank()); // TEST
}
}