diff --git a/domain/Game.java b/domain/Game.java index e6f950a..9603078 100644 --- a/domain/Game.java +++ b/domain/Game.java @@ -1,6 +1,8 @@ package domain; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; public class Game { private ArrayList currentPlayers; @@ -10,13 +12,23 @@ public class Game { public Game(){ currentPlayers = new ArrayList(); 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); } diff --git a/domain/Player.java b/domain/Player.java index ba2ac73..bdc5769 100644 --- a/domain/Player.java +++ b/domain/Player.java @@ -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); } } diff --git a/domain/Sheet.java b/domain/Sheet.java index d79ac99..6413c66 100644 --- a/domain/Sheet.java +++ b/domain/Sheet.java @@ -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; } diff --git a/fassade/KniffelSystem.java b/fassade/KniffelSystem.java index 625c799..a7f3823 100644 --- a/fassade/KniffelSystem.java +++ b/fassade/KniffelSystem.java @@ -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){ - for (int i = 0; i < amountRolls; i++){ - System.out.println(game.rollDice()); + public ArrayList rollDices(ArrayList rolls, String keptDice){ + ArrayList oldRolls = extractKeptDice(rolls, keptDice); + + int amountNewRolls = oldRolls.size(); + ArrayList 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 extractKeptDice(ArrayList previousRolls, String keptDice){ + ArrayList keptRolls = new ArrayList(); + + 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 rollMultipleDice(int amountRolls){ + System.out.printf("Amount rolls: %d \n", amountRolls); // TEST + ArrayList rolls = new ArrayList<>(); + if (amountRolls == 0){ + return rolls; + } + + for (int i = 0; i < amountRolls; i++){ + rolls.add(game.rollDice()); + } + return rolls; + } + + // TEST public String[] getAllPlayerStrings(){ ArrayList players = game.getPlayers(); diff --git a/tui/TUI.java b/tui/TUI.java index fb2d29b..3fe8e45 100644 --- a/tui/TUI.java +++ b/tui/TUI.java @@ -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 rolls = new ArrayList(); + 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 rolls){ + return "TODO"; + } + + + private static String diceArrToString(ArrayList 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 - } }