Browse Source

refactoring: input innerhalb hit() nach userInputBJ() verlagert + Kommentare

remotes/origin/stefan
fdai7472 11 months ago
parent
commit
7619ce3c06
  1. 17
      src/main/c/Stefan/blackjack.c

17
src/main/c/Stefan/blackjack.c

@ -89,7 +89,6 @@ int getRandCard(int offset){
int playBJ(int bet, int balance){ int playBJ(int bet, int balance){
int winnings = 0; int winnings = 0;
int userCardsTotal = 0, dealerCardsTotal = 0; int userCardsTotal = 0, dealerCardsTotal = 0;
@ -154,6 +153,7 @@ int playBJ(int bet, int balance){
dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i); dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i);
// Wenn Dealer total < 17, muss er noch eine Karte ziehen
if(dealerCardsTotal < 17){ if(dealerCardsTotal < 17){
dealerCardsArray[i] = getRandCard(i); dealerCardsArray[i] = getRandCard(i);
printf("Dealer Card: %d\n", dealerCardsArray[i]); printf("Dealer Card: %d\n", dealerCardsArray[i]);
@ -162,35 +162,36 @@ int playBJ(int bet, int balance){
} }
printf("Dealer Cards Total: %d\n", dealerCardsTotal); printf("Dealer Cards Total: %d\n", dealerCardsTotal);
// Wenn Dealer total > 17 und <= 21, dann keine Karte mehr ziehen
if(dealerCardsTotal >= 17 && dealerCardsTotal <= 21){ if(dealerCardsTotal >= 17 && dealerCardsTotal <= 21){
// Überprüfen, wer gewonnen hat und entsprechend Gewinn berechnen
if(calculateIfYouWon(userCardsTotal, dealerCardsTotal)){ if(calculateIfYouWon(userCardsTotal, dealerCardsTotal)){
winnings = calculateWinnings(false, bet); winnings = calculateWinnings(false, bet);
} }
break; break;
} }
// Wenn dealer total > 21, dann hat der Spieler gewonnen + Gewinn berechnen
if(dealerCardsTotal > 21){ if(dealerCardsTotal > 21){
printf("DEALER BUST\n\n"); printf("DEALER BUST\n\n");
winnings = calculateWinnings(false, bet); winnings = calculateWinnings(false, bet);
break; break;
} }
} }
// Speicher wieder freigeben/zurücksetzen
free(userCardsArray); free(userCardsArray);
} }
// Speicher wieder freigeben/zurücksetzen
free(dealerCardsArray); free(dealerCardsArray);
} }
return balance + winnings; return balance + winnings;
} }
// Wenn die ersten beiden Karten in Summe den Wert 21 haben, heißt das Blackjack
bool checkForBlackjack(int userCardArray[]){ bool checkForBlackjack(int userCardArray[]){
if(userCardArray[0] + userCardArray[1] == 21){ if(userCardArray[0] + userCardArray[1] == 21){
printf("BLACKJACK!\n\n"); printf("BLACKJACK!\n\n");
@ -200,10 +201,8 @@ bool checkForBlackjack(int userCardArray[]){
} }
bool hit(){ bool hit(){
int answer;
printf("Hit (1) or stand (2): "); printf("Hit (1) or stand (2): ");
scanf("%d", &answer);
int answer = userInputBJ();
if(answer != 1){ if(answer != 1){
printf("You stand.\n\n"); printf("You stand.\n\n");

Loading…
Cancel
Save