parent
9c64c60544
commit
adca38fdae
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,83 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Hand {
|
||||
Kartenstapel ks;
|
||||
private ArrayList<Karte> 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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,30 +1,48 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
public class Kartenstapel {
|
||||
String[][] stapel;
|
||||
private ArrayList<Karte> stapel = new ArrayList<>();
|
||||
Kartenstapel(){
|
||||
stapel = generateNewStapel();
|
||||
}
|
||||
|
||||
String[][] generateNewStapel(){
|
||||
|
||||
public ArrayList<Karte> 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<Karte> 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();
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue