Browse Source

Implemenitierung calculateWinnings + Tests

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

14
src/main/c/Stefan/blackjack.c

@ -73,6 +73,8 @@ int getRandCard(){
int playBJ(int bet, int balance){ int playBJ(int bet, int balance){
int winnings = 0;
int *computerCardArray = (int*) malloc(10 * sizeof(int)); int *computerCardArray = (int*) malloc(10 * sizeof(int));
if(computerCardArray){ if(computerCardArray){
@ -88,7 +90,10 @@ int playBJ(int bet, int balance){
userCardArray[1] = getRandCard(); userCardArray[1] = getRandCard();
printf("user rand card: %d\n", userCardArray[1]); printf("user rand card: %d\n", userCardArray[1]);
checkForBlackjack(userCardArray);
if(checkForBlackjack(userCardArray)){
winnings = calculateWinnings(true, bet);
return balance + winnings;
}
free(userCardArray); free(userCardArray);
@ -121,4 +126,11 @@ int calculateCardsTotal(int cardsArray[], int len){
sum += cardsArray[i]; sum += cardsArray[i];
} }
return sum; return sum;
}
int calculateWinnings(bool blackjack, int bet){
if(blackjack){
return 3 * bet;
}
return 2 * bet;
} }

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

@ -11,5 +11,6 @@ int getRandCard();
int playBJ(int bet, int balance); 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);
#endif // BLACKJACK_H #endif // BLACKJACK_H

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

@ -100,4 +100,32 @@ void test_cards_ace_10_total_equals_21() {
//assert //assert
TEST_ASSERT_EQUAL_INT(expected_result, actual_result); TEST_ASSERT_EQUAL_INT(expected_result, actual_result);
}
void test_blackjack_bet_equals_10() {
//arrange
int bet = 10;
int expected_result = 30;
//act
int actual_result = calculateWinnings(true, bet);
//assert
TEST_ASSERT_EQUAL_INT(expected_result, actual_result);
}
void test_no_blackjack_bet_equals_10() {
//arrange
int bet = 10;
int expected_result = 20;
//act
int actual_result = calculateWinnings(false, bet);
//assert
TEST_ASSERT_EQUAL_INT(expected_result, actual_result);
} }
Loading…
Cancel
Save