Compare commits

...

2 Commits

Author SHA1 Message Date
Victor Hans-Georg Waitz 9d5f8858e0 Completed default gamemode
Added complete default gamemode, added leaderboard saved and loaded from file, added beginings of the starwars mode implementation.
2024-05-06 22:56:12 +02:00
Victor Hans-Georg Waitz e2604101bf Completed default gamemode
Added complete default gamemode, added leaderboard saved and loaded from file, added beginings of the starwars mode implementation.
2024-05-06 22:55:58 +02:00
12 changed files with 331 additions and 49 deletions

View File

@ -8,11 +8,30 @@ public class Game {
private ArrayList<Player> currentPlayers;
private int turnPlayer;
private Dice dice;
private String gamemode;
public Game(){
currentPlayers = new ArrayList<Player>();
turnPlayer = 0;
dice = new Dice(6);
}
public String setGamemode(String gamemode){
if (gamemode.equals("1")){
this.gamemode = "default";
dice = new Dice(6);
return "Default mode selected.";
} else {
this.gamemode = "starwars";
dice = new Dice(8);
return "Starwars mode selected.";
}
}
public String getGamemode(){
return this.gamemode;
}
@ -20,10 +39,12 @@ public class Game {
Collections.rotate(currentPlayers, -1);
}
public Player getCurrentPlayer(){
return currentPlayers.getFirst();
}
public int rollDice(){
return dice.roll();
}
@ -33,6 +54,7 @@ public class Game {
currentPlayers.add(playerToAdd);
}
public ArrayList<Player> getPlayers(){
return currentPlayers;
}

View File

@ -7,12 +7,19 @@ public class Player {
int score;
Sheet sheet;
public Player(int playerNumber, String name, String color, int score) {
public Player(int playerNumber, String name, String color, int score, String gamemode) {
this.playerNumber = playerNumber;
this.name = name;
this.color = color;
this.score = score;
this.sheet = new Sheet();
if (gamemode.equals("default")){
this.sheet = new Sheet();
}
else if (gamemode.equals("starwars")){
this.sheet = new StarwarsSheet();
}
}
@ -24,11 +31,25 @@ public class Player {
return this.sheet;
}
public void setScore(int score){
this.score = score;
}
public int getScore(){
return this.score;
}
public String getName(){
return this.name;
}
@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("%s", coloredName);
}
}

View File

@ -11,7 +11,7 @@ public class Sheet {
ArrayList<String> usedRows = new ArrayList<>();
ArrayList<String> crossedRows = new ArrayList<>();
// Sheet rows, first half
//? Sheet rows, upper half
Aces aces;
Twos twos;
Threes threes;
@ -19,7 +19,7 @@ public class Sheet {
Fives fives;
Sixes sixes;
// Sheet rows, second half
//? Sheet rows, lower half
ThreeOfKind threeOfKind;
FourOfKind fourOfKind;
FullHouse fullHouse;
@ -143,6 +143,48 @@ public class Sheet {
}
public int calcSheet(){
int upperSum = calcUpperHalf();
int upperScore = calcUpperBonus(upperSum);
int lowerScore = calcLowerHalf();
return upperScore + lowerScore;
}
private int calcUpperHalf(){
int upperSum = 0;
upperSum += aces.getValue();
upperSum += twos.getValue();
upperSum += threes.getValue();
upperSum += fours.getValue();
upperSum += fives.getValue();
upperSum += sixes.getValue();
return upperSum;
}
private int calcLowerHalf(){
int lowerSum = 0;
lowerSum += threeOfKind.getValue();
lowerSum += fourOfKind.getValue();
lowerSum += fullHouse.getValue();
lowerSum += smallStraight.getValue();
lowerSum += largeStraight.getValue();
lowerSum += yahtzee.getValue();
lowerSum += chance.getValue();
return lowerSum;
}
private int calcUpperBonus(int upperSum){
if (upperSum >= 63){
upperSum += 35;
}
return upperSum;
}
public boolean checkGameEnd(){
return (usedRows.size() + crossedRows.size()) == amountTurns;
}

View File

@ -0,0 +1,52 @@
package domain;
import domain.sheets.*;
public class StarwarsSheet extends Sheet{
//? Additional upper half
Sevens sevens;
Eights eights;
//? Additional lower half
StarWarsDay starWarsDay;
R2D2 r2D2;
public StarwarsSheet(){
super();
this.sevens = new Sevens();
super.unusedRows.add(this.sevens.toString());
this.eights = new Eights();
super.unusedRows.add(this.eights.toString());
this.starWarsDay = new StarWarsDay();
super.unusedRows.add(this.starWarsDay.toString());
this.r2D2 = new R2D2();
super.unusedRows.add(this.r2D2.toString());
}
public void writeStarwarsCategory(Category categoryToWrite, boolean crossing){
switch (categoryToWrite.toString()){
case "Sevens":
sevens = (Sevens) categoryToWrite;
break;
case "Eights":
eights = (Eights) categoryToWrite;
break;
case "StarWarsDay":
starWarsDay = (StarWarsDay) categoryToWrite;
break;
case "R2D2":
r2D2 = (R2D2) categoryToWrite;
break;
default:
super.writeCategory(categoryToWrite, crossing);
break;
}
}
}

View File

@ -0,0 +1,10 @@
package domain.sheets;
public class Eights extends Category{
@Override
public int calcValueFromAmount() {
this.value = this.amount * 8;
return this.value;
}
}

View File

@ -0,0 +1,5 @@
package domain.sheets;
public class R2D2 extends Category{
//TODO
}

View File

@ -0,0 +1,10 @@
package domain.sheets;
public class Sevens extends Category{
@Override
public int calcValueFromAmount() {
this.value = this.amount * 7;
return this.value;
}
}

View File

@ -0,0 +1,6 @@
package domain.sheets;
public class StarWarsDay extends Category{
//TODO
}

View File

@ -1,28 +1,5 @@
package fassade;
public class GameCycle {
// public class DiceRoll {
// public static boolean hasSmallStraight(int[] dice) {
// Arrays.sort(dice);
// for (int i = 0; i < dice.length - 3; i++) {
// if (dice[i] + 1 == dice[i + 1] && dice[i] + 2 == dice[i + 2] && dice[i] + 3 == dice[i + 3]) {
// return true;
// }
// }
// return false;
// }
//
// public static boolean hasLargeStraight(int[] dice) {
// Arrays.sort(dice);
// for (int i = 0; i < dice.length - 4; i++) {
// if (dice[i] + 1 == dice[i + 1] && dice[i] + 2 == dice[i + 2] && dice[i] + 3 == dice[i + 3] && dice[i] + 4 == dice[i + 4]) {
// return true;
// }
// }
// return false;
// }
//
// public static void main(String[] args) {
// int[] dice = {3, 1, 4, 2, 5};}
// } // example array System.out.println("Has small straight: " + hasSmallStraight(dice)); System.out.println("Has large straight: " + hasLargeStraight(dice)); }}```Here's how the code works:1. First, we sort the array of dice rolls in ascending order using `Arrays.sort()`.2. In the `hasSmallStraight()` method, we iterate through the sorted array and check if four consecutive numbers appear. We do this by checking if the current element plus 1, 2, and 3 are equal to the next three elements in the array. If we find a match, we return `true`.3. In the `hasLargeStraight()` method, we do a similar check,
}
}

View File

@ -5,9 +5,13 @@ import domain.Player;
import domain.Sheet;
import domain.sheets.Category;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
public class KniffelSystem {
String lederboardLocation = "scores.csv";
ArrayList<String> playerColors;
ArrayList<Category> allValidCombinations = new ArrayList<>();
Game game;
@ -22,14 +26,18 @@ public class KniffelSystem {
}
public String LeaderBaordData(){
return "Leaderboard - TODO \n";
public String setGamemode(String gamemode){
return game.setGamemode(gamemode);
}
private String getGamemode(){
return game.getGamemode();
}
public String addPlayer(int playerNumber, String name) {
String playerColor = colorPicker(playerNumber);
Player playerToAdd = new Player(playerNumber, name, playerColor, 0);
Player playerToAdd = new Player(playerNumber, name, playerColor, 0, getGamemode());
game.addPlayer(playerToAdd);
return changeStringFormat(name, playerColor);
@ -326,7 +334,11 @@ public class KniffelSystem {
}
if (contains){
currentPlayerSheet.writeCategory(categoryToWrite, false);
if (getGamemode().equals("default")) {
currentPlayerSheet.writeCategory(categoryToWrite, false);
}else {
currentPlayerSheet.wr
}
}else {
currentPlayerSheet.writeCategory(categoryToWrite, true);
}
@ -344,6 +356,81 @@ public class KniffelSystem {
}
public String afterGame() throws IOException {
StringBuilder sb = new StringBuilder();
for (Player player : game.getPlayers()){
int score = player.getSheet().calcSheet();
player.setScore(score);
sb.append(String.format("%s scored %d points!", player.toString(), score));
writeToFile(createLeaderboardString(player));
}
return sb.toString();
}
private String createLeaderboardString(Player player){
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd.MM.yyy");
LocalDateTime now = LocalDateTime.now();
String date = dtf.format(now);
int score = player.getScore();
String name = player.getName();
return String.format("%d - %s - %s \n", score, name, date);
}
public ArrayList<String> readFromFile() throws FileNotFoundException {
ArrayList<String> leaderboardRows = new ArrayList<>();
Scanner fsc = null;
try {
fsc = new Scanner(new File(lederboardLocation));
} catch (FileNotFoundException e) {
System.out.println("There is an error opening the leaderboard.");
System.out.println("Returning to main menu \n");
return leaderboardRows;
}
while (fsc.hasNextLine()){
leaderboardRows.add(fsc.nextLine());
}
return leaderboardRows;
}
public void writeToFile(String stringToEnter) throws IOException {
if (stringToEnter.isEmpty()){
BufferedWriter writer = new BufferedWriter(new FileWriter(lederboardLocation));
writer.write("");
return;
}
System.out.printf("Not empty |%s| \n", stringToEnter);
ArrayList<String> currentRows = readFromFile();
currentRows.add(stringToEnter);
StringBuilder sb = new StringBuilder();
for(String leaderboardRow : currentRows) {
System.out.println(leaderboardRow);
String[] row = leaderboardRow.split(",");
System.out.printf("len row: %d \n", row.length);
sb.append(String.format("%s - %s - %s \n", row[1], row[0], row[2]));
}
BufferedWriter writer = new BufferedWriter(new FileWriter(lederboardLocation));
writer.write(sb.toString());
}
public void clearLeaderboard() throws IOException {
writeToFile("");
}
private HashMap<String, Category> createCategoryHashMap(){
// TODO starwars sheet implementieren
Sheet sheet = new Sheet();

0
scores.csv 100644
View File

View File

@ -3,6 +3,8 @@ package tui;
import domain.Player;
import fassade.KniffelSystem;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
@ -10,21 +12,21 @@ public class TUI {
static KniffelSystem gameSystem;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
public static void main(String[] args) throws IOException {
System.out.println("Welcome to the PR2 Kniffel game!");
// while (true){
// mainMenuOutput();
// }
while (true){
mainMenuOutput();
}
// DEV:
gameSystem = new KniffelSystem();
gameSystem.creteDevPlayers(1);
gameLoop();
// gameSystem = new KniffelSystem();
// gameSystem.creteDevPlayers(1);
// gameLoop();
}
private static void mainMenuOutput(){
private static void mainMenuOutput() throws IOException {
System.out.println("What do you want to do?");
System.out.println("1 - Play");
System.out.println("2 - See leaderboard");
@ -48,9 +50,15 @@ public class TUI {
}
}
private static void mainMenuPlay(){
private static void mainMenuPlay() throws IOException {
gameSystem = new KniffelSystem();
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);
System.out.println("How many players are you? (1-6)");
System.out.print("> ");
String mainMenuPlayAmountPlayersInput = sc.nextLine().toLowerCase();
@ -68,9 +76,13 @@ public class TUI {
}
gameLoop();
System.out.println("GAME END"); //! TEST
afterGame();
}
private static void decideGamemode(String userInput){
System.out.println(gameSystem.setGamemode(userInput));
System.out.println();
}
private static void gameLoop(){
String[] playerStrings = gameSystem.getAllPlayerStrings();
@ -155,18 +167,56 @@ public class TUI {
}
private static void afterGame(){
//TODO sheet berechnen, gewinner entscheiden
private static void afterGame() throws IOException {
System.out.println(gameSystem.afterGame());
}
private static void mainMenuLeaderBoard(){
private static void mainMenuLeaderBoard() throws IOException {
gameSystem = new KniffelSystem(); // Scorboard System
System.out.println(gameSystem.LeaderBaordData()); // TODO
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(",");
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();
}
}
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 "";
}
private static void mainMenuExit(){
private static void mainMenuExit() throws IOException {
System.out.println("Do you really want to exit? (Y/n)");
System.out.print("> ");
String mainMenuExitUserInput = sc.nextLine().toLowerCase();