From 217bd7cc4844af1010516d3ffb1b5f87d7cfe06e Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 20:08:48 +0100 Subject: [PATCH] refactoring: Umbenennen der Variablen in playBJ() --- src/main/c/Stefan/blackjack.c | 64 +++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 4af7f15..b39ce29 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -15,12 +15,15 @@ int blackjack(){ while(balance > 0){ printf("Aktuelles Guthaben: %d\n", balance); int bet = getBetBJ(balance, exitPtr); + // Abbruchbedingung, um das Blackjack zu verlassen if(exit) { return -1; } + // Bei jeder Runde wird der Wetteinsatz vom Guthaben abgezogen balance -= bet; + // Nach jeder Runde wird das Guthaben aktualisiert balance = playBJ(bet, balance); } @@ -42,13 +45,14 @@ int getBetBJ(int balance, bool *exitPtr){ if(bet <= balance && bet > 0){ return bet; - } else if(bet == 0) { + } + // Blackjack verlassen, wenn die Eingabe 0 ist + else if(bet == 0) { *exitPtr = true; break; } else { printf("Ungueltige Eingabe!\n"); } - } } @@ -82,24 +86,24 @@ int getRandCard(){ int playBJ(int bet, int balance){ 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); return balance + winnings; } @@ -108,21 +112,21 @@ int playBJ(int bet, int balance){ for(int i = 2; i < 10; i++){ - userCardsTotal = calculateCardsTotal(userCardArray, i); + userCardsTotal = calculateCardsTotal(userCardsArray, i); printf("Your Cards Total : %d\n", userCardsTotal); if(userCardsTotal > 21){ printf("BUST\n"); - free(userCardArray); - free(computerCardArray); + free(userCardsArray); + free(dealerCardsArray); return balance; } if(hit()){ - userCardArray[i] = getRandCard(); - printf("new card: %d\n", userCardArray[i]); + userCardsArray[i] = getRandCard(); + printf("new card: %d\n", userCardsArray[i]); } else{ break; } @@ -131,23 +135,23 @@ int playBJ(int bet, int balance){ 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); } break; } - if(computerCardsTotal > 21){ + if(dealerCardsTotal > 21){ printf("COMPUTER BUST\n"); winnings = calculateWinnings(false, bet); break; @@ -160,10 +164,10 @@ int playBJ(int bet, int balance){ - free(userCardArray); + free(userCardsArray); } - free(computerCardArray); + free(dealerCardsArray); } return balance + winnings;