Browse Source

Berechnung, wer gewinnt + Tests + Guthaben aktualisiert nach jeder Runde

remotes/origin/stefan
fdai7472 11 months ago
parent
commit
03c6dc3fa6
  1. 83
      src/main/c/Stefan/blackjack.c
  2. 2
      src/main/c/Stefan/blackjack.h
  3. 28
      src/test/c/Stefan/test_blackjack.c

83
src/main/c/Stefan/blackjack.c

@ -12,6 +12,7 @@ int blackjack(){
while(balance > 0){ while(balance > 0){
printf("Guthaben: %d\n", balance);
int bet = getBetBJ(balance); int bet = getBetBJ(balance);
balance -= bet; balance -= bet;
@ -74,6 +75,7 @@ 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 *computerCardArray = (int*) malloc(10 * sizeof(int)); int *computerCardArray = (int*) malloc(10 * sizeof(int));
@ -96,13 +98,68 @@ int playBJ(int bet, int balance){
} }
for(int i = 2; i < 10; i++){
userCardsTotal = calculateCardsTotal(userCardArray, i);
printf("Your Cards Total : %d\n", userCardsTotal);
if(userCardsTotal > 21){
printf("BUST\n");
free(userCardArray);
free(computerCardArray);
return balance;
}
if(hit()){
userCardArray[i] = getRandCard();
printf("new card: %d\n", userCardArray[i]);
} else{
break;
}
}
for(int i = 1; i < 10; i++){
computerCardsTotal = calculateCardsTotal(computerCardArray, i);
if(computerCardsTotal < 17){
computerCardArray[i] = getRandCard();
printf("Computer Card: %d\n", computerCardArray[i]);
computerCardsTotal = calculateCardsTotal(computerCardArray, i + 1);
}
printf("Computer Cards Total: %d\n", computerCardsTotal);
if(computerCardsTotal >= 17 && computerCardsTotal < 21){
if(calculateIfYouWon(userCardsTotal, computerCardsTotal)){
winnings = calculateWinnings(false, bet);
}
break;
}
if(computerCardsTotal > 21){
printf("COMPUTER BUST\n");
winnings = calculateWinnings(false, bet);
break;
}
}
free(userCardArray); free(userCardArray);
} }
free(computerCardArray); free(computerCardArray);
} }
return balance;
return balance + winnings;
} }
bool checkForBlackjack(int userCardArray[]){ bool checkForBlackjack(int userCardArray[]){
@ -113,6 +170,28 @@ bool checkForBlackjack(int userCardArray[]){
return false; return false;
} }
bool hit(){
int answer;
printf("Hit (1) or stand (2): ");
scanf("%d", &answer);
if(answer != 1){
printf("stand.\n");
return false;
}
printf("hit.\n");
return true;
}
bool calculateIfYouWon(int userCardsTotal, int computerCardsTotal){
if(userCardsTotal > computerCardsTotal){
return true;
}
printf("Leider verloren.\n");
return false;
}
int calculateCardsTotal(int cardsArray[], int len){ int calculateCardsTotal(int cardsArray[], int len){
int sum = 0; int sum = 0;
bool aceFound = false; bool aceFound = false;
@ -130,7 +209,9 @@ int calculateCardsTotal(int cardsArray[], int len){
int calculateWinnings(bool blackjack, int bet){ int calculateWinnings(bool blackjack, int bet){
if(blackjack){ if(blackjack){
printf("Du hast %d gewonnen!\n", bet * 2);
return 3 * bet; return 3 * bet;
} }
printf("Du hast %d gewonnen!\n", bet);
return 2 * bet; return 2 * bet;
} }

2
src/main/c/Stefan/blackjack.h

@ -12,5 +12,7 @@ int playBJ(int bet, int balance);
bool checkForBlackjack(int userCards[]); bool checkForBlackjack(int userCards[]);
int calculateCardsTotal(int cardsArray[], int len); int calculateCardsTotal(int cardsArray[], int len);
int calculateWinnings(bool blackjack, int bet); int calculateWinnings(bool blackjack, int bet);
bool hit();
bool calculateIfYouWon(int userCardsTotal, int computerCardsTotal);
#endif // BLACKJACK_H #endif // BLACKJACK_H

28
src/test/c/Stefan/test_blackjack.c

@ -128,4 +128,32 @@ void test_no_blackjack_bet_equals_10() {
//assert //assert
TEST_ASSERT_EQUAL_INT(expected_result, actual_result); TEST_ASSERT_EQUAL_INT(expected_result, actual_result);
}
void test_calculate_if_you_won_20_vs_19() {
//arrange
int userCardsTotal = 20;
int dealerCardsTotal = 19;
//act
bool result = calculateIfYouWon(userCardsTotal, dealerCardsTotal);
//assert
TEST_ASSERT_TRUE(result);
}
void test_calculate_if_you_won_20_vs_21() {
//arrange
int userCardsTotal = 20;
int dealerCardsTotal = 21;
//act
bool result = calculateIfYouWon(userCardsTotal, dealerCardsTotal);
//assert
TEST_ASSERT_FALSE(result);
} }
Loading…
Cancel
Save