From ff65ed4d40eb44fd91bab2c31fbde9d9e99d4bd7 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 18:00:26 +0100 Subject: [PATCH] ueberprueft, ob Ass den Wert 1 oder 11 haben muss + Tests --- src/main/c/Stefan/blackjack.c | 7 ++++ src/test/c/Stefan/test_blackjack.c | 56 ++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 3c2a20c..06a765a 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -110,7 +110,14 @@ bool checkForBlackjack(int userCardArray[]){ int calculateCardsTotal(int cardsArray[], int len){ int sum = 0; + bool aceFound = false; for(int i = 0; i < len; i++){ + if(cardsArray[i] == 11){ + aceFound = true; + } + if(aceFound && sum + cardsArray[i] > 21){ + sum -= 10; + } sum += cardsArray[i]; } return sum; diff --git a/src/test/c/Stefan/test_blackjack.c b/src/test/c/Stefan/test_blackjack.c index 2d6b01c..334affa 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -44,4 +44,60 @@ void test_2_cards_total_equals_20() { //assert TEST_ASSERT_EQUAL_INT(expected_result, actual_result); +} + +void test_cards_10_2_ace_total_equals_13() { + + //arrange + int userCards[] = {10, 2, 11}; + int expected_result = 13; + + //act + int actual_result = calculateCardsTotal(userCards, 3); + + //assert + TEST_ASSERT_EQUAL_INT(expected_result, actual_result); + +} + +void test_cards_8_ace_ace_total_equals_20() { + + //arrange + int userCards[] = {8, 11, 11}; + int expected_result = 20; + + //act + int actual_result = calculateCardsTotal(userCards, 3); + + //assert + TEST_ASSERT_EQUAL_INT(expected_result, actual_result); + +} + +void test_cards_8_ace_10_total_equals_19() { + + //arrange + int userCards[] = {8, 11, 10}; + int expected_result = 19; + + //act + int actual_result = calculateCardsTotal(userCards, 3); + + //assert + TEST_ASSERT_EQUAL_INT(expected_result, actual_result); + +} + +void test_cards_ace_10_total_equals_21() { + + //arrange + int userCards[] = { 11, 10}; + int expected_result = 21; + + //act + int actual_result = calculateCardsTotal(userCards, 2); + + //assert + TEST_ASSERT_EQUAL_INT(expected_result, actual_result); + } \ No newline at end of file