Browse Source

refactoring: Umbenennen der Variablen in playBJ()

remotes/origin/stefan
fdai7472 11 months ago
parent
commit
217bd7cc48
  1. 64
      src/main/c/Stefan/blackjack.c

64
src/main/c/Stefan/blackjack.c

@ -15,12 +15,15 @@ int blackjack(){
while(balance > 0){ while(balance > 0){
printf("Aktuelles Guthaben: %d\n", balance); printf("Aktuelles Guthaben: %d\n", balance);
int bet = getBetBJ(balance, exitPtr); int bet = getBetBJ(balance, exitPtr);
// Abbruchbedingung, um das Blackjack zu verlassen
if(exit) { if(exit) {
return -1; return -1;
} }
// Bei jeder Runde wird der Wetteinsatz vom Guthaben abgezogen
balance -= bet; balance -= bet;
// Nach jeder Runde wird das Guthaben aktualisiert
balance = playBJ(bet, balance); balance = playBJ(bet, balance);
} }
@ -42,13 +45,14 @@ int getBetBJ(int balance, bool *exitPtr){
if(bet <= balance && bet > 0){ if(bet <= balance && bet > 0){
return bet; return bet;
} else if(bet == 0) {
}
// Blackjack verlassen, wenn die Eingabe 0 ist
else if(bet == 0) {
*exitPtr = true; *exitPtr = true;
break; break;
} else { } else {
printf("Ungueltige Eingabe!\n"); printf("Ungueltige Eingabe!\n");
} }
} }
} }
@ -82,24 +86,24 @@ int getRandCard(){
int playBJ(int bet, int balance){ int playBJ(int bet, int balance){
int winnings = 0; int winnings = 0;
int userCardsTotal = 0, computerCardsTotal = 0;
int userCardsTotal = 0, dealerCardsTotal = 0;
int *computerCardArray = (int*) malloc(10 * sizeof(int));
int *dealerCardsArray = (int*) malloc(10 * sizeof(int));
if(computerCardArray){
computerCardArray[0] = getRandCard();
printf("computer rand card: %d\n", computerCardArray[0]);
if(dealerCardsArray){
dealerCardsArray[0] = getRandCard();
printf("computer rand card: %d\n", dealerCardsArray[0]);
int *userCardArray = (int*) malloc(10 * sizeof(int));
int *userCardsArray = (int*) malloc(10 * sizeof(int));
if(userCardArray){
if(userCardsArray){
userCardArray[0] = getRandCard();
printf("user rand card: %d\n", userCardArray[0]);
userCardArray[1] = getRandCard();
printf("user rand card: %d\n", userCardArray[1]);
userCardsArray[0] = getRandCard();
printf("user rand card: %d\n", userCardsArray[0]);
userCardsArray[1] = getRandCard();
printf("user rand card: %d\n", userCardsArray[1]);
if(checkForBlackjack(userCardArray)){
if(checkForBlackjack(userCardsArray)){
winnings = calculateWinnings(true, bet); winnings = calculateWinnings(true, bet);
return balance + winnings; return balance + winnings;
} }
@ -108,21 +112,21 @@ int playBJ(int bet, int balance){
for(int i = 2; i < 10; i++){ for(int i = 2; i < 10; i++){
userCardsTotal = calculateCardsTotal(userCardArray, i);
userCardsTotal = calculateCardsTotal(userCardsArray, i);
printf("Your Cards Total : %d\n", userCardsTotal); printf("Your Cards Total : %d\n", userCardsTotal);
if(userCardsTotal > 21){ if(userCardsTotal > 21){
printf("BUST\n"); printf("BUST\n");
free(userCardArray);
free(computerCardArray);
free(userCardsArray);
free(dealerCardsArray);
return balance; return balance;
} }
if(hit()){ if(hit()){
userCardArray[i] = getRandCard();
printf("new card: %d\n", userCardArray[i]);
userCardsArray[i] = getRandCard();
printf("new card: %d\n", userCardsArray[i]);
} else{ } else{
break; break;
} }
@ -131,23 +135,23 @@ int playBJ(int bet, int balance){
for(int i = 1; i < 10; i++){ for(int i = 1; i < 10; i++){
computerCardsTotal = calculateCardsTotal(computerCardArray, i);
dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i);
if(computerCardsTotal < 17){
computerCardArray[i] = getRandCard();
printf("Computer Card: %d\n", computerCardArray[i]);
computerCardsTotal = calculateCardsTotal(computerCardArray, i + 1);
if(dealerCardsTotal < 17){
dealerCardsArray[i] = getRandCard();
printf("Computer Card: %d\n", dealerCardsArray[i]);
dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i + 1);
} }
printf("Computer Cards Total: %d\n", computerCardsTotal);
printf("Computer Cards Total: %d\n", dealerCardsTotal);
if(computerCardsTotal >= 17 && computerCardsTotal < 21){
if(calculateIfYouWon(userCardsTotal, computerCardsTotal)){
if(dealerCardsTotal >= 17 && dealerCardsTotal < 21){
if(calculateIfYouWon(userCardsTotal, dealerCardsTotal)){
winnings = calculateWinnings(false, bet); winnings = calculateWinnings(false, bet);
} }
break; break;
} }
if(computerCardsTotal > 21){
if(dealerCardsTotal > 21){
printf("COMPUTER BUST\n"); printf("COMPUTER BUST\n");
winnings = calculateWinnings(false, bet); winnings = calculateWinnings(false, bet);
break; break;
@ -160,10 +164,10 @@ int playBJ(int bet, int balance){
free(userCardArray);
free(userCardsArray);
} }
free(computerCardArray);
free(dealerCardsArray);
} }
return balance + winnings; return balance + winnings;

Loading…
Cancel
Save