From adca38fdae62404c527d539dccf730da1616a81d Mon Sep 17 00:00:00 2001 From: 3013050 <3013050@stud.hs-mannheim.de> Date: Tue, 26 Mar 2024 00:46:12 +0100 Subject: [PATCH] Finished BlackJack Implemented game UI and game logic. --- src/BlackJackSpiel.java | 16 ++++++--- src/Hand.java | 76 +++++++++++++++++++++++++++++++++++++++++ src/Karte.java | 6 ++-- src/Kartenstapel.java | 36 ++++++++++++++----- src/Main.java | 48 ++++++++++++++++++++------ 5 files changed, 154 insertions(+), 28 deletions(-) diff --git a/src/BlackJackSpiel.java b/src/BlackJackSpiel.java index 8c868f6..fb8f74d 100644 --- a/src/BlackJackSpiel.java +++ b/src/BlackJackSpiel.java @@ -1,10 +1,18 @@ public class BlackJackSpiel { - - void BlackJackSpiel(){ - + Kartenstapel ks; + Hand hand; + public BlackJackSpiel(){ + this.ks = new Kartenstapel(); + this.hand = new Hand(this.ks); } Hand getNeueHand(){ - return new Hand(new Kartenstapel()); + this.hand.drawCard(2); + return this.hand; + } + + Hand turn(){ + this.hand.drawCard(1); + return this.hand; } } diff --git a/src/Hand.java b/src/Hand.java index d25d7a0..d5dd959 100644 --- a/src/Hand.java +++ b/src/Hand.java @@ -1,7 +1,83 @@ +import java.util.ArrayList; +import java.util.Objects; + public class Hand { Kartenstapel ks; + private ArrayList hand = new ArrayList<>(); public Hand(Kartenstapel ks) { this.ks = ks; } + + public void drawCard(int amount){ + for (int i = 0; i < amount; i++){ + hand.add(ks.getKarte()); + } + } + + + public int getPunkte(){ + int score = 0; + for (Karte karte : hand){ + score += karte.getWert(); + } + return score; + } + + public boolean hasAss(){ + for (Karte karte : hand){ + if (Objects.equals(karte.wert, "Ass")){ + return true; + } + } + return false; + } + + + public boolean isBlackJack(){ + boolean hasAss = hasAss(); + int score = getPunkte(); + if (score == 21){ + return true; + } + + if (hasAss){ + score -= 10; + return score == 21; + } + return false; + } + + + public boolean isOvershot(){ + boolean hasAss = hasAss(); + int score = getPunkte(); + + if (hasAss){ + score -= 10; + return score > 21; + } + + return score > 21; + } + + + @Override + public String toString() { + if (hand.isEmpty()){ + return "Hand ist leer."; + } + + StringBuilder sb = new StringBuilder(); + + sb.append("Du hast ein*e "); + for (int i = 0; i < (hand.size()-1); i++){ + sb.append(String.format("%s, ", hand.get(i).wert)); + } + int lastCardIndex = hand.size() - 1; + sb.append(String.format("%s ", hand.get(lastCardIndex).wert)); + sb.append("auf der Hand."); + + return sb.toString(); + } } diff --git a/src/Karte.java b/src/Karte.java index c1d3a53..9b66d83 100644 --- a/src/Karte.java +++ b/src/Karte.java @@ -7,9 +7,7 @@ public class Karte { this.wert = wert; } - public int getPunkte() { - // 2, 3, 4, 5, 6, 7, 8, 9, 10, Bube, Dame, König, Ass - // 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 , 1 o. 11 + public int getWert() { if ((this.wert.equals("Bube")) || (this.wert.equals("Dame")) || (this.wert.equals("König"))){ return 10; } @@ -21,6 +19,6 @@ public class Karte { @Override public String toString() { - return String.format("%s %s", this.farbe, this.wert); + return String.format("%s ", this.wert); } } diff --git a/src/Kartenstapel.java b/src/Kartenstapel.java index cf792d0..6d0c953 100644 --- a/src/Kartenstapel.java +++ b/src/Kartenstapel.java @@ -1,30 +1,48 @@ +import java.util.ArrayList; +import java.util.Random; + public class Kartenstapel { - String[][] stapel; + private ArrayList stapel = new ArrayList<>(); Kartenstapel(){ stapel = generateNewStapel(); } - String[][] generateNewStapel(){ + + public ArrayList generateNewStapel(){ String[] possibleFarben = {"Herz", "Karo", "Schippe", "Kreuz"}; String[] possibleWerte = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Bube", "Dame", "König", "Ass"}; - String[][] returnStapel = new String[52][2]; + ArrayList returnStapel = new ArrayList<>(); - int counter = 0; for (String farbe : possibleFarben){ for (String wert : possibleWerte){ - returnStapel[counter] = new String[]{farbe, wert}; - - counter ++; + returnStapel.add(new Karte(farbe, wert)); } } return returnStapel; } + + public int getZahlÜbrigerKarten(){ + return stapel.size(); + } + + + public Karte getKarte(){ + if (stapel.isEmpty()){ + throw new RuntimeException(); + } + + Random rand = new Random(); + int randomCardIndex = rand.nextInt(stapel.size()); + return stapel.remove(randomCardIndex); + } + + public String toString(){ StringBuilder sb = new StringBuilder(); - for (String[] karte : stapel){ - sb.append(String.format("%s %s \n", karte[0], karte[1])); + for (Karte karte : stapel){ + sb.append(String.format("%s %s \n", karte.farbe, karte.wert)); } return sb.toString(); } diff --git a/src/Main.java b/src/Main.java index aefe0f1..f18e634 100644 --- a/src/Main.java +++ b/src/Main.java @@ -4,33 +4,59 @@ public class Main { static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { - System.out.println("Willkommen zum PR2 Blackjack!"); + System.out.println("Willkommen zum PR2 Blackjack! \n"); gameLoop(); } private static void gameLoop(){ - while (true){ - Kartenstapel ks = new Kartenstapel(); - System.out.println(ks); + BlackJackSpiel game = new BlackJackSpiel(); + Hand hand = game.getNeueHand(); - String newRoundPrombt = "Möchten Sie noch eine Runde spielen? (ja/nein)"; - boolean newRoundDecision = yesNoDecider(newRoundPrombt); - System.out.printf("Decision: %b %n", newRoundDecision); //? TEST + while (true){ + System.out.printf("%s Ihr Wert beträgt %d \n", hand, hand.getPunkte()); + + String nextRoundPrompt = ""; + + + if (hand.isBlackJack()){ + nextRoundPrompt = "Glückwunsch, sie haben einen BlackJack und damit gewonnen!"; + } + + if (hand.isOvershot()){ + nextRoundPrompt = String.format("Leider verloren, der Wert der Hand ist mit %d höher als 21.", hand.getPunkte()); + } + + if (nextRoundPrompt.isEmpty()) { + //? Next card decider + String newCardPrompt = "Möchten Sie noch eine Karte ziehen? (ja/nein)"; + boolean newCardDecision = yesNoDecider(newCardPrompt); + + if (newCardDecision) { + hand = game.turn(); + continue; + } + } + + //? Next round decider + System.out.printf("%s \n\n", nextRoundPrompt); + String newRoundPrompt = "Möchten Sie noch eine Runde spielen? (ja/nein)"; + boolean newRoundDecision = yesNoDecider(newRoundPrompt); if (!(newRoundDecision)) { System.out.println("Auf Wiedersehen :)"); break; } + game = new BlackJackSpiel(); + hand = game.getNeueHand(); } } - private static boolean yesNoDecider(String prombt){ - System.out.println(prombt); + private static boolean yesNoDecider(String prompt){ + System.out.println(prompt); System.out.print("> "); String userInput = keyboard.nextLine(); return userInput.equalsIgnoreCase("ja"); } - -} \ No newline at end of file +}