2024-04-29 21:06:04 +02:00
|
|
|
package tui;
|
|
|
|
|
|
|
|
import fassade.KniffelSystem;
|
|
|
|
|
2024-05-06 22:55:58 +02:00
|
|
|
import java.io.IOException;
|
2024-05-04 21:06:05 +02:00
|
|
|
import java.util.ArrayList;
|
2024-04-29 21:06:04 +02:00
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
|
|
public class TUI {
|
|
|
|
static KniffelSystem gameSystem;
|
|
|
|
static Scanner sc = new Scanner(System.in);
|
|
|
|
|
2024-05-06 22:55:58 +02:00
|
|
|
public static void main(String[] args) throws IOException {
|
2024-04-29 21:06:04 +02:00
|
|
|
System.out.println("Welcome to the PR2 Kniffel game!");
|
2024-04-30 12:46:42 +02:00
|
|
|
|
2024-05-06 22:55:58 +02:00
|
|
|
while (true){
|
|
|
|
mainMenuOutput();
|
|
|
|
}
|
2024-04-30 12:46:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
// DEV:
|
2024-05-06 22:55:58 +02:00
|
|
|
// gameSystem = new KniffelSystem();
|
|
|
|
// gameSystem.creteDevPlayers(1);
|
|
|
|
// gameLoop();
|
2024-04-29 21:06:04 +02:00
|
|
|
}
|
|
|
|
|
2024-05-06 22:55:58 +02:00
|
|
|
private static void mainMenuOutput() throws IOException {
|
2024-04-29 21:06:04 +02:00
|
|
|
System.out.println("What do you want to do?");
|
|
|
|
System.out.println("1 - Play");
|
2024-04-30 12:46:42 +02:00
|
|
|
System.out.println("2 - See leaderboard");
|
2024-04-29 21:06:04 +02:00
|
|
|
System.out.println("3 - Exit");
|
|
|
|
System.out.print("> ");
|
|
|
|
String mainMenuUserInput = sc.nextLine().toLowerCase();
|
|
|
|
|
|
|
|
|
|
|
|
if ((mainMenuUserInput.equals("1"))
|
|
|
|
|| (mainMenuUserInput.equals("play"))){
|
|
|
|
mainMenuPlay();
|
|
|
|
}
|
|
|
|
else if ((mainMenuUserInput.equals("2"))
|
2024-04-30 12:46:42 +02:00
|
|
|
|| (mainMenuUserInput.equals("see leaderboard"))
|
2024-04-29 21:06:04 +02:00
|
|
|
|| (mainMenuUserInput.equals("see"))
|
2024-04-30 12:46:42 +02:00
|
|
|
|| (mainMenuUserInput.equals("leaderboard"))){
|
|
|
|
mainMenuLeaderBoard();
|
2024-04-29 21:06:04 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mainMenuExit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-06 22:55:58 +02:00
|
|
|
private static void mainMenuPlay() throws IOException {
|
2024-04-29 21:06:04 +02:00
|
|
|
gameSystem = new KniffelSystem();
|
|
|
|
|
2024-05-06 22:55:58 +02:00
|
|
|
System.out.println("Do you want to play the default mode (1) or the starwars mode (2)");
|
|
|
|
System.out.print("> ");
|
|
|
|
String mainMenuGamemodeOutput = sc.nextLine();
|
|
|
|
decideGamemode(mainMenuGamemodeOutput);
|
|
|
|
|
|
|
|
|
2024-04-29 21:06:04 +02:00
|
|
|
System.out.println("How many players are you? (1-6)");
|
|
|
|
System.out.print("> ");
|
|
|
|
String mainMenuPlayAmountPlayersInput = sc.nextLine().toLowerCase();
|
|
|
|
|
|
|
|
int amountPlayers = Integer.parseInt(mainMenuPlayAmountPlayersInput);
|
2024-04-30 12:46:42 +02:00
|
|
|
|
2024-04-29 21:06:04 +02:00
|
|
|
for (int i = 0; i < amountPlayers; i++){
|
|
|
|
System.out.printf("Player %d: ", i + 1);
|
|
|
|
System.out.println("Enter your name: ");
|
|
|
|
System.out.print("> ");
|
|
|
|
String playerName = sc.nextLine();
|
|
|
|
|
|
|
|
String coloredPlayerName = gameSystem.addPlayer(i+1, playerName);
|
|
|
|
System.out.printf("Welcome %s! \n\n", coloredPlayerName);
|
|
|
|
}
|
2024-05-04 21:06:05 +02:00
|
|
|
|
|
|
|
gameLoop();
|
2024-05-06 17:19:11 +02:00
|
|
|
afterGame();
|
2024-04-29 21:06:04 +02:00
|
|
|
}
|
2024-05-06 22:55:58 +02:00
|
|
|
private static void decideGamemode(String userInput){
|
|
|
|
System.out.println(gameSystem.setGamemode(userInput));
|
|
|
|
System.out.println();
|
|
|
|
}
|
|
|
|
|
2024-04-29 21:06:04 +02:00
|
|
|
|
2024-05-07 09:54:34 +02:00
|
|
|
private static void gameLoop() {
|
2024-04-30 12:46:42 +02:00
|
|
|
String[] playerStrings = gameSystem.getAllPlayerStrings();
|
|
|
|
|
2024-05-04 21:06:05 +02:00
|
|
|
System.out.println("Participating players:");
|
2024-04-30 12:46:42 +02:00
|
|
|
for (String player : playerStrings){
|
|
|
|
System.out.println(player);
|
|
|
|
}
|
2024-05-04 21:06:05 +02:00
|
|
|
System.out.println();
|
|
|
|
|
|
|
|
int rollscount;
|
|
|
|
|
|
|
|
while (true){
|
2024-05-06 17:19:11 +02:00
|
|
|
|
|
|
|
if (gameSystem.checkGameEnd()){
|
|
|
|
return;
|
|
|
|
}
|
2024-05-04 21:06:05 +02:00
|
|
|
rollscount = 0;
|
2024-05-06 17:19:11 +02:00
|
|
|
|
2024-05-04 21:06:05 +02:00
|
|
|
System.out.printf("It's your turn %s!\n\n", gameSystem.getCurrentPlayer());
|
|
|
|
|
|
|
|
ArrayList<Integer> rolls = new ArrayList<Integer>();
|
|
|
|
String keptDice = "";
|
|
|
|
|
|
|
|
while(rollscount < 3){
|
|
|
|
rolls = gameSystem.rollDices(rolls, keptDice);
|
|
|
|
String newRollsString = diceArrToString(rolls);
|
|
|
|
System.out.println(newRollsString);
|
2024-05-06 02:35:10 +02:00
|
|
|
String evaluatedRolls = gameSystem.evaluateRoll(rolls);
|
|
|
|
System.out.println(evaluatedRolls);
|
2024-05-04 21:06:05 +02:00
|
|
|
|
2024-05-05 18:12:50 +02:00
|
|
|
|
2024-05-06 02:35:10 +02:00
|
|
|
if (rollscount < 2) {
|
2024-05-06 17:19:11 +02:00
|
|
|
String keeping = gameSystem.changeStringFormat("Which dice do you want to keep?", "\u001B[32m");
|
|
|
|
System.out.println(keeping);
|
2024-05-06 02:35:10 +02:00
|
|
|
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++;
|
2024-05-05 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
2024-05-06 02:35:10 +02:00
|
|
|
writeToSheet();
|
2024-05-05 18:12:50 +02:00
|
|
|
|
2024-05-06 17:19:11 +02:00
|
|
|
triggerNextTurn();
|
2024-05-05 18:12:50 +02:00
|
|
|
}
|
2024-05-04 21:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-06 02:35:10 +02:00
|
|
|
private static void writeToSheet(){
|
2024-05-06 17:19:11 +02:00
|
|
|
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);
|
2024-05-06 02:35:10 +02:00
|
|
|
System.out.print(">");
|
|
|
|
String sheetInput = sc.nextLine();
|
|
|
|
gameSystem.writeToSheet(sheetInput);
|
2024-05-05 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-04 21:06:05 +02:00
|
|
|
private static String diceArrToString(ArrayList<Integer> diceArr){
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2024-05-06 02:35:10 +02:00
|
|
|
sb.append("Throwing dice... \n");
|
2024-05-04 21:06:05 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-06 17:19:11 +02:00
|
|
|
private static void triggerNextTurn(){
|
2024-05-04 21:06:05 +02:00
|
|
|
gameSystem.nextPlayer();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-06 22:55:58 +02:00
|
|
|
private static void afterGame() throws IOException {
|
|
|
|
System.out.println(gameSystem.afterGame());
|
2024-05-06 17:19:11 +02:00
|
|
|
}
|
|
|
|
|
2024-05-04 21:06:05 +02:00
|
|
|
|
2024-05-06 22:55:58 +02:00
|
|
|
private static void mainMenuLeaderBoard() throws IOException {
|
2024-04-30 12:46:42 +02:00
|
|
|
gameSystem = new KniffelSystem(); // Scorboard System
|
2024-05-06 22:55:58 +02:00
|
|
|
|
|
|
|
ArrayList<String> leaderboardRows = gameSystem.readFromFile();
|
|
|
|
|
|
|
|
if(leaderboardRows.isEmpty()){
|
|
|
|
System.out.println("\nThe leadboard is empty, be the first one! \n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
for(String leaderboardRow : leaderboardRows) {
|
|
|
|
String[] row = leaderboardRow.split(",");
|
2024-05-07 09:54:34 +02:00
|
|
|
System.out.printf("Len row: %d \n", row.length);
|
2024-05-06 22:55:58 +02:00
|
|
|
sb.append(String.format("%s - %s - %s \n", row[1], row[0], row[2]));
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println("\nLEADERBOARD");
|
|
|
|
System.out.println(sb.toString());
|
|
|
|
|
|
|
|
String deleteLeaderboardOutput = deleteLeaderboardInput();
|
|
|
|
|
|
|
|
if (!(deleteLeaderboardOutput.isEmpty())){
|
|
|
|
System.out.println(deleteLeaderboardOutput);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
System.out.println();
|
|
|
|
}
|
|
|
|
|
2024-04-29 21:06:04 +02:00
|
|
|
}
|
|
|
|
|
2024-05-06 22:55:58 +02:00
|
|
|
private static String deleteLeaderboardInput() throws IOException {
|
|
|
|
System.out.println("Clear lederboard? (y/N)");
|
|
|
|
System.out.print("> ");
|
|
|
|
String deleteInput = sc.nextLine().toLowerCase();
|
|
|
|
if ((deleteInput.equals("y")) || (deleteInput.equals("yes"))){
|
|
|
|
gameSystem.clearLeaderboard();
|
|
|
|
return "Cleared leaderboard \n";
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-04 21:06:05 +02:00
|
|
|
|
2024-05-06 22:55:58 +02:00
|
|
|
private static void mainMenuExit() throws IOException {
|
2024-04-29 21:06:04 +02:00
|
|
|
System.out.println("Do you really want to exit? (Y/n)");
|
|
|
|
System.out.print("> ");
|
|
|
|
String mainMenuExitUserInput = sc.nextLine().toLowerCase();
|
|
|
|
|
|
|
|
if ((mainMenuExitUserInput.equals("y"))
|
|
|
|
|| (mainMenuExitUserInput.equals("yes"))
|
|
|
|
|| mainMenuExitUserInput.isBlank()){
|
|
|
|
System.out.print("Exiting, see you next time!");
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
System.out.println("Returning to main menu");
|
|
|
|
System.out.println();
|
|
|
|
mainMenuOutput();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|