diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 5e995bd..bb649ec 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -12,6 +12,7 @@ int blackjack(){ while(balance > 0){ + printf("Guthaben: %d\n", balance); int bet = getBetBJ(balance); balance -= bet; @@ -74,6 +75,7 @@ int getRandCard(){ int playBJ(int bet, int balance){ int winnings = 0; + int userCardsTotal = 0, computerCardsTotal = 0; 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(computerCardArray); } - return balance; + return balance + winnings; } bool checkForBlackjack(int userCardArray[]){ @@ -113,6 +170,28 @@ bool checkForBlackjack(int userCardArray[]){ 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 sum = 0; bool aceFound = false; @@ -130,7 +209,9 @@ int calculateCardsTotal(int cardsArray[], int len){ int calculateWinnings(bool blackjack, int bet){ if(blackjack){ + printf("Du hast %d gewonnen!\n", bet * 2); return 3 * bet; } + printf("Du hast %d gewonnen!\n", bet); return 2 * bet; } \ No newline at end of file diff --git a/src/main/c/Stefan/blackjack.h b/src/main/c/Stefan/blackjack.h index 71656e0..adc9a15 100644 --- a/src/main/c/Stefan/blackjack.h +++ b/src/main/c/Stefan/blackjack.h @@ -12,5 +12,7 @@ int playBJ(int bet, int balance); bool checkForBlackjack(int userCards[]); int calculateCardsTotal(int cardsArray[], int len); int calculateWinnings(bool blackjack, int bet); +bool hit(); +bool calculateIfYouWon(int userCardsTotal, int computerCardsTotal); #endif // BLACKJACK_H \ No newline at end of file diff --git a/src/test/c/Stefan/test_blackjack.c b/src/test/c/Stefan/test_blackjack.c index 5fee88e..25d5957 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -128,4 +128,32 @@ void test_no_blackjack_bet_equals_10() { //assert 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); + } \ No newline at end of file