Finished BlackJack

Implemented game UI and game logic.
master
Victor Hans-Georg Waitz 2024-03-26 00:46:12 +01:00
parent 9c64c60544
commit adca38fdae
5 changed files with 154 additions and 28 deletions

View File

@ -1,10 +1,18 @@
public class BlackJackSpiel { public class BlackJackSpiel {
Kartenstapel ks;
void BlackJackSpiel(){ Hand hand;
public BlackJackSpiel(){
this.ks = new Kartenstapel();
this.hand = new Hand(this.ks);
} }
Hand getNeueHand(){ Hand getNeueHand(){
return new Hand(new Kartenstapel()); this.hand.drawCard(2);
return this.hand;
}
Hand turn(){
this.hand.drawCard(1);
return this.hand;
} }
} }

View File

@ -1,7 +1,83 @@
import java.util.ArrayList;
import java.util.Objects;
public class Hand { public class Hand {
Kartenstapel ks; Kartenstapel ks;
private ArrayList<Karte> hand = new ArrayList<>();
public Hand(Kartenstapel ks) { public Hand(Kartenstapel ks) {
this.ks = 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();
}
} }

View File

@ -7,9 +7,7 @@ public class Karte {
this.wert = wert; this.wert = wert;
} }
public int getPunkte() { public int getWert() {
// 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
if ((this.wert.equals("Bube")) || (this.wert.equals("Dame")) || (this.wert.equals("König"))){ if ((this.wert.equals("Bube")) || (this.wert.equals("Dame")) || (this.wert.equals("König"))){
return 10; return 10;
} }
@ -21,6 +19,6 @@ public class Karte {
@Override @Override
public String toString() { public String toString() {
return String.format("%s %s", this.farbe, this.wert); return String.format("%s ", this.wert);
} }
} }

View File

@ -1,30 +1,48 @@
import java.util.ArrayList;
import java.util.Random;
public class Kartenstapel { public class Kartenstapel {
String[][] stapel; private ArrayList<Karte> stapel = new ArrayList<>();
Kartenstapel(){ Kartenstapel(){
stapel = generateNewStapel(); stapel = generateNewStapel();
} }
String[][] generateNewStapel(){
public ArrayList<Karte> generateNewStapel(){
String[] possibleFarben = {"Herz", "Karo", "Schippe", "Kreuz"}; String[] possibleFarben = {"Herz", "Karo", "Schippe", "Kreuz"};
String[] possibleWerte = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Bube", "Dame", "König", "Ass"}; String[] possibleWerte = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Bube", "Dame", "König", "Ass"};
String[][] returnStapel = new String[52][2]; ArrayList<Karte> returnStapel = new ArrayList<>();
int counter = 0;
for (String farbe : possibleFarben){ for (String farbe : possibleFarben){
for (String wert : possibleWerte){ for (String wert : possibleWerte){
returnStapel[counter] = new String[]{farbe, wert}; returnStapel.add(new Karte(farbe, wert));
counter ++;
} }
} }
return returnStapel; 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(){ public String toString(){
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (String[] karte : stapel){ for (Karte karte : stapel){
sb.append(String.format("%s %s \n", karte[0], karte[1])); sb.append(String.format("%s %s \n", karte.farbe, karte.wert));
} }
return sb.toString(); return sb.toString();
} }

View File

@ -4,33 +4,59 @@ public class Main {
static Scanner keyboard = new Scanner(System.in); static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Willkommen zum PR2 Blackjack!"); System.out.println("Willkommen zum PR2 Blackjack! \n");
gameLoop(); gameLoop();
} }
private static void gameLoop(){ private static void gameLoop(){
while (true){ BlackJackSpiel game = new BlackJackSpiel();
Kartenstapel ks = new Kartenstapel(); Hand hand = game.getNeueHand();
System.out.println(ks);
String newRoundPrombt = "Möchten Sie noch eine Runde spielen? (ja/nein)"; while (true){
boolean newRoundDecision = yesNoDecider(newRoundPrombt); System.out.printf("%s Ihr Wert beträgt %d \n", hand, hand.getPunkte());
System.out.printf("Decision: %b %n", newRoundDecision); //? TEST
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)) { if (!(newRoundDecision)) {
System.out.println("Auf Wiedersehen :)"); System.out.println("Auf Wiedersehen :)");
break; break;
} }
game = new BlackJackSpiel();
hand = game.getNeueHand();
} }
} }
private static boolean yesNoDecider(String prombt){ private static boolean yesNoDecider(String prompt){
System.out.println(prombt); System.out.println(prompt);
System.out.print("> "); System.out.print("> ");
String userInput = keyboard.nextLine(); String userInput = keyboard.nextLine();
return userInput.equalsIgnoreCase("ja"); return userInput.equalsIgnoreCase("ja");
} }
} }