From f9618bcee92f53a520993a77f41bbf84e337e47a Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 11:31:38 +0100 Subject: [PATCH 01/46] Blackjack Dateistruktur angelegt --- src/main/c/Stefan/blackjack.c | 10 ++++++++++ src/main/c/Stefan/blackjack.h | 7 +++++++ src/test/c/Stefan/test_blackjack.c | 27 +++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 src/main/c/Stefan/blackjack.c create mode 100644 src/main/c/Stefan/blackjack.h create mode 100644 src/test/c/Stefan/test_blackjack.c diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c new file mode 100644 index 0000000..7c54106 --- /dev/null +++ b/src/main/c/Stefan/blackjack.c @@ -0,0 +1,10 @@ +#include "blackjack.h" + + + +int blackjack(){ + + + + return 0; +} \ No newline at end of file diff --git a/src/main/c/Stefan/blackjack.h b/src/main/c/Stefan/blackjack.h new file mode 100644 index 0000000..26037b2 --- /dev/null +++ b/src/main/c/Stefan/blackjack.h @@ -0,0 +1,7 @@ +#ifndef BLACKJACK_H +#define BLACKJACK_H + + +int blackjack(); + +#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 new file mode 100644 index 0000000..092557c --- /dev/null +++ b/src/test/c/Stefan/test_blackjack.c @@ -0,0 +1,27 @@ +#include "blackjack.h" +#include "unity.h" + +void setUp() {} + +void tearDown() {} + + +void test_1() { + + //arrange + + //act + + //assert + +} + +void test_2() { + + //arrange + + //act + + //assert + +} \ No newline at end of file From 5a3455879908258d58fac89fa98b8e16c562f92f Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 12:23:16 +0100 Subject: [PATCH 02/46] Blackjack in main inkludiert --- src/main/c/Stefan/blackjack.c | 4 ++-- src/main/c/main.c | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 7c54106..63f8bd8 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -1,10 +1,10 @@ #include "blackjack.h" - +#include int blackjack(){ - + printf("Blackjack \n"); return 0; } \ No newline at end of file diff --git a/src/main/c/main.c b/src/main/c/main.c index 49f4cdd..118c247 100644 --- a/src/main/c/main.c +++ b/src/main/c/main.c @@ -15,6 +15,7 @@ #include "hangman.h" #include "tictactoe.h" #include "slot_machine.h" +#include "blackjack.h" void openInterface(); @@ -37,7 +38,8 @@ void openInterface() "1: Schere-Stein-Papier\n" "2: Hangman\n" "3: TicTacToe\n" - "4: Slot Machine\n"); + "4: Slot Machine\n" + "6: Blackjack\n"); printf("\nBitte waehle die Zahl des entsprechenden Spiels aus, um damit zu starten.\nAm Ende eines Spiels kannst du mit der Taste 0 wieder zurueck zum Hauptmenue kommen.\nIm Hauptmenue beendest du mit der Auswahl 0 das Programm \n\n"); scanf_s("%d", &selection); @@ -59,9 +61,12 @@ void openInterface() case(4): slotMachine(); break; - /*case(5): + case(5): //Spiel() - break;*/ + break; + case(6): + blackjack(); + break; default: break; } From 03cfef5d90d6883082fb8a119e43346be407de24 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 13:27:48 +0100 Subject: [PATCH 03/46] =?UTF-8?q?string=20winnerMessage=20l=C3=A4nger=20in?= =?UTF-8?q?itialisiert,=20um=20overflow=20zu=20vermeiden?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Stefan/slot_machine.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index f659eab..c3dbd65 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -129,7 +129,7 @@ int getWinnings(char symbols[], int bet){ void showResult(char symbols[], int winnings){ - char winnerMessage[] = {""}; + char winnerMessage[10] = {""}; if(winnings > 0){ strcpy(winnerMessage, " WINNER "); From f3c289167b6d28f8552c128857de57560a4c9f5b Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 13:31:44 +0100 Subject: [PATCH 04/46] blackjack guthaben einwerfen --- src/main/c/Stefan/blackjack.c | 15 +++++++++++++-- src/main/c/Stefan/blackjack.h | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 63f8bd8..a7af619 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -5,6 +5,17 @@ int blackjack(){ - printf("Blackjack \n"); + printf("Welcome to Blackjack \n"); + + int balance = getBalanceBJ(); + return 0; -} \ No newline at end of file +} + +int getBalanceBJ(){ + int balance = 0; + printf("Guthaben einwerfen: "); + scanf("%d", &balance); + printf("Dein Guthaben: %d", balance); + return balance; +} diff --git a/src/main/c/Stefan/blackjack.h b/src/main/c/Stefan/blackjack.h index 26037b2..69f1176 100644 --- a/src/main/c/Stefan/blackjack.h +++ b/src/main/c/Stefan/blackjack.h @@ -3,5 +3,6 @@ int blackjack(); +int getBalanceBJ(); #endif // BLACKJACK_H \ No newline at end of file From c9604b763599a2d95fc6d94b804951f062fd6ff4 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 15:48:11 +0100 Subject: [PATCH 05/46] Implementierung getBet und userInput BJ --- src/main/c/Stefan/blackjack.c | 25 ++++++++++++++++++++++++- src/main/c/Stefan/blackjack.h | 2 ++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index a7af619..9c353d4 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -8,6 +8,8 @@ int blackjack(){ printf("Welcome to Blackjack \n"); int balance = getBalanceBJ(); + int bet = getBetBJ(balance); + printf("bet: %d\n", bet); return 0; } @@ -16,6 +18,27 @@ int getBalanceBJ(){ int balance = 0; printf("Guthaben einwerfen: "); scanf("%d", &balance); - printf("Dein Guthaben: %d", balance); + printf("Dein Guthaben: %d\n", balance); return balance; } + +int getBetBJ(int balance){ + + while(1){ + printf("Wetteinsatz: "); + int bet = userInputBJ(); + + if(bet <= balance && bet > 0){ + return bet; + } else { + printf("Ungueltige Eingabe!\n"); + } + + } +} + +int userInputBJ(){ + int input; + scanf("%d", &input); + return input; +} diff --git a/src/main/c/Stefan/blackjack.h b/src/main/c/Stefan/blackjack.h index 69f1176..8f7fe1e 100644 --- a/src/main/c/Stefan/blackjack.h +++ b/src/main/c/Stefan/blackjack.h @@ -4,5 +4,7 @@ int blackjack(); int getBalanceBJ(); +int getBetBJ(int balance); +int userInputBJ(); #endif // BLACKJACK_H \ No newline at end of file From 3334346f81944597725433b3bae1a535ed494b16 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 16:03:44 +0100 Subject: [PATCH 06/46] Implementierung getRandCard, liefert Zufallszahl zwischen 1 und 10 --- src/main/c/Stefan/blackjack.c | 22 ++++++++++++++++++++++ src/main/c/Stefan/blackjack.h | 1 + 2 files changed, 23 insertions(+) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 9c353d4..5a0a77d 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -1,5 +1,7 @@ #include "blackjack.h" #include +#include +#include int blackjack(){ @@ -10,6 +12,7 @@ int blackjack(){ int balance = getBalanceBJ(); int bet = getBetBJ(balance); printf("bet: %d\n", bet); + printf("rand card: %d\n", getRandCard()); return 0; } @@ -42,3 +45,22 @@ int userInputBJ(){ scanf("%d", &input); return input; } + +// Liefert eine Zufallszahl zwischen 1 und 10 +// Chance für 10 ist 4 Mal höher als für den Rest +// 1 = Ass +int getRandCard(){ + + srand(time(NULL)); + + int r = rand() % 13; + r++; + + if(r > 10){ + return 10; + } + + return r; + + +} \ No newline at end of file diff --git a/src/main/c/Stefan/blackjack.h b/src/main/c/Stefan/blackjack.h index 8f7fe1e..bfc4f73 100644 --- a/src/main/c/Stefan/blackjack.h +++ b/src/main/c/Stefan/blackjack.h @@ -6,5 +6,6 @@ int blackjack(); int getBalanceBJ(); int getBetBJ(int balance); int userInputBJ(); +int getRandCard(); #endif // BLACKJACK_H \ No newline at end of file From d5a649ab4022822627cf30db10f1649bbce0ae8a Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 16:23:05 +0100 Subject: [PATCH 07/46] Spiel wiederholt sich, solange man noch Guthaben hat + random Computer Card wird generiert --- src/main/c/Stefan/blackjack.c | 24 ++++++++++++++++++++++-- src/main/c/Stefan/blackjack.h | 1 + 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 5a0a77d..9d99762 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -10,10 +10,16 @@ int blackjack(){ printf("Welcome to Blackjack \n"); int balance = getBalanceBJ(); - int bet = getBetBJ(balance); - printf("bet: %d\n", bet); + printf("rand card: %d\n", getRandCard()); + while(balance > 0){ + int bet = getBetBJ(balance); + balance -= bet; + + playBJ(bet, balance); + } + return 0; } @@ -63,4 +69,18 @@ int getRandCard(){ return r; +} + +int playBJ(int bet, int balance){ + + int *computerCardArray = (int*) malloc(10 * sizeof(int)); + + if(computerCardArray){ + computerCardArray[0] = getRandCard(); + printf("computer rand card: %d\n", computerCardArray[0]); + + free(computerCardArray); + } + + return balance; } \ No newline at end of file diff --git a/src/main/c/Stefan/blackjack.h b/src/main/c/Stefan/blackjack.h index bfc4f73..af00859 100644 --- a/src/main/c/Stefan/blackjack.h +++ b/src/main/c/Stefan/blackjack.h @@ -7,5 +7,6 @@ int getBalanceBJ(); int getBetBJ(int balance); int userInputBJ(); int getRandCard(); +int playBJ(int bet, int balance); #endif // BLACKJACK_H \ No newline at end of file From 984a921ed6f9f4ff830d38d638d2580614326786 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 17:20:28 +0100 Subject: [PATCH 08/46] generiert 2 random Karten fuer den User --- src/main/c/Stefan/blackjack.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 9d99762..a4878e7 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -11,7 +11,6 @@ int blackjack(){ int balance = getBalanceBJ(); - printf("rand card: %d\n", getRandCard()); while(balance > 0){ int bet = getBetBJ(balance); @@ -57,11 +56,10 @@ int userInputBJ(){ // 1 = Ass int getRandCard(){ - srand(time(NULL)); - int r = rand() % 13; r++; + if(r > 10){ return 10; } @@ -71,6 +69,7 @@ int getRandCard(){ } + int playBJ(int bet, int balance){ int *computerCardArray = (int*) malloc(10 * sizeof(int)); @@ -79,6 +78,19 @@ int playBJ(int bet, int balance){ computerCardArray[0] = getRandCard(); printf("computer rand card: %d\n", computerCardArray[0]); + int *userCardArray = (int*) malloc(10 * sizeof(int)); + + if(userCardArray){ + + userCardArray[0] = getRandCard(); + printf("user rand card: %d\n", userCardArray[0]); + userCardArray[1] = getRandCard(); + printf("user rand card: %d\n", userCardArray[1]); + + + free(userCardArray); + } + free(computerCardArray); } From 2d844a36944ae304b45e3415afe0923eac21182a Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 17:37:57 +0100 Subject: [PATCH 09/46] Implementierung CheckForBlackjack + Tests --- src/main/c/Stefan/blackjack.c | 15 +++++++++++++-- src/main/c/Stefan/blackjack.h | 2 ++ src/test/c/Stefan/test_blackjack.c | 12 +++++++++--- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index a4878e7..2268373 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -1,8 +1,7 @@ #include "blackjack.h" #include #include -#include - +#include int blackjack(){ @@ -62,6 +61,8 @@ int getRandCard(){ if(r > 10){ return 10; + } else if(r == 1){ + return 11; } return r; @@ -87,6 +88,8 @@ int playBJ(int bet, int balance){ userCardArray[1] = getRandCard(); printf("user rand card: %d\n", userCardArray[1]); + checkForBlackjack(userCardArray); + free(userCardArray); } @@ -95,4 +98,12 @@ int playBJ(int bet, int balance){ } return balance; +} + +bool checkForBlackjack(int userCardArray[]){ + if(userCardArray[0] + userCardArray[1] == 21){ + printf("BLACKJACK!\n"); + return true; + } + return false; } \ No newline at end of file diff --git a/src/main/c/Stefan/blackjack.h b/src/main/c/Stefan/blackjack.h index af00859..1388b18 100644 --- a/src/main/c/Stefan/blackjack.h +++ b/src/main/c/Stefan/blackjack.h @@ -1,6 +1,7 @@ #ifndef BLACKJACK_H #define BLACKJACK_H +#include int blackjack(); int getBalanceBJ(); @@ -8,5 +9,6 @@ int getBetBJ(int balance); int userInputBJ(); int getRandCard(); int playBJ(int bet, int balance); +bool checkForBlackjack(int userCards[]); #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 092557c..6217651 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -6,22 +6,28 @@ void setUp() {} void tearDown() {} -void test_1() { +void test_blackjack() { //arrange + int userCards[] = {10, 11}; //act + bool result = checkForBlackjack(userCards); //assert + TEST_ASSERT_TRUE(result); } -void test_2() { +void test_no_blackjack() { //arrange + int userCards[] = {10, 10}; //act + bool result = checkForBlackjack(userCards); //assert - + TEST_ASSERT_FALSE(result); + } \ No newline at end of file From f114cab6167d4ac4e097a8d2a15725788780234d Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 17:46:52 +0100 Subject: [PATCH 10/46] berechnet Gesamtwert zweier Karten + Test --- src/main/c/Stefan/blackjack.c | 8 ++++++++ src/main/c/Stefan/blackjack.h | 1 + src/test/c/Stefan/test_blackjack.c | 14 ++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 2268373..3c2a20c 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -106,4 +106,12 @@ bool checkForBlackjack(int userCardArray[]){ return true; } return false; +} + +int calculateCardsTotal(int cardsArray[], int len){ + int sum = 0; + for(int i = 0; i < len; i++){ + sum += cardsArray[i]; + } + return sum; } \ No newline at end of file diff --git a/src/main/c/Stefan/blackjack.h b/src/main/c/Stefan/blackjack.h index 1388b18..be79c7b 100644 --- a/src/main/c/Stefan/blackjack.h +++ b/src/main/c/Stefan/blackjack.h @@ -10,5 +10,6 @@ int userInputBJ(); int getRandCard(); int playBJ(int bet, int balance); bool checkForBlackjack(int userCards[]); +int calculateCardsTotal(int cardsArray[], int len); #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 6217651..2d6b01c 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -30,4 +30,18 @@ void test_no_blackjack() { //assert TEST_ASSERT_FALSE(result); +} + +void test_2_cards_total_equals_20() { + + //arrange + int userCards[] = {10, 10}; + int expected_result = 20; + + //act + int actual_result = calculateCardsTotal(userCards, 2); + + //assert + TEST_ASSERT_EQUAL_INT(expected_result, actual_result); + } \ No newline at end of file From ff65ed4d40eb44fd91bab2c31fbde9d9e99d4bd7 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 18:00:26 +0100 Subject: [PATCH 11/46] 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 From b155dc4909447f7338f2b480223efb58f3f3b805 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 18:13:40 +0100 Subject: [PATCH 12/46] Implemenitierung calculateWinnings + Tests --- src/main/c/Stefan/blackjack.c | 14 +++++++++++++- src/main/c/Stefan/blackjack.h | 1 + src/test/c/Stefan/test_blackjack.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 06a765a..5e995bd 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -73,6 +73,8 @@ int getRandCard(){ int playBJ(int bet, int balance){ + int winnings = 0; + int *computerCardArray = (int*) malloc(10 * sizeof(int)); if(computerCardArray){ @@ -88,7 +90,10 @@ int playBJ(int bet, int balance){ userCardArray[1] = getRandCard(); printf("user rand card: %d\n", userCardArray[1]); - checkForBlackjack(userCardArray); + if(checkForBlackjack(userCardArray)){ + winnings = calculateWinnings(true, bet); + return balance + winnings; + } free(userCardArray); @@ -121,4 +126,11 @@ int calculateCardsTotal(int cardsArray[], int len){ sum += cardsArray[i]; } return sum; +} + +int calculateWinnings(bool blackjack, int bet){ + if(blackjack){ + return 3 * 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 be79c7b..71656e0 100644 --- a/src/main/c/Stefan/blackjack.h +++ b/src/main/c/Stefan/blackjack.h @@ -11,5 +11,6 @@ int getRandCard(); int playBJ(int bet, int balance); bool checkForBlackjack(int userCards[]); int calculateCardsTotal(int cardsArray[], int len); +int calculateWinnings(bool blackjack, int bet); #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 334affa..5fee88e 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -100,4 +100,32 @@ void test_cards_ace_10_total_equals_21() { //assert 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); + } \ No newline at end of file From 03c6dc3fa68ab3ed9f3acc3569b1215ca0b8c091 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 19:34:17 +0100 Subject: [PATCH 13/46] Berechnung, wer gewinnt + Tests + Guthaben aktualisiert nach jeder Runde --- src/main/c/Stefan/blackjack.c | 83 +++++++++++++++++++++++++++++- src/main/c/Stefan/blackjack.h | 2 + src/test/c/Stefan/test_blackjack.c | 28 ++++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) 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 From 0f3a5e811f1288f6950eb8a536227628b578e7a4 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 19:40:33 +0100 Subject: [PATCH 14/46] =?UTF-8?q?refactoring:=20Funktion=20getBalanceBJ=20?= =?UTF-8?q?verst=C3=A4ndlicher=20umgeschrieben?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Stefan/blackjack.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index bb649ec..9c40be9 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -6,13 +6,12 @@ int blackjack(){ - printf("Welcome to Blackjack \n"); + printf("Welcome to Blackjack \n\n"); int balance = getBalanceBJ(); - while(balance > 0){ - printf("Guthaben: %d\n", balance); + printf("Aktuelles Guthaben: %d\n", balance); int bet = getBetBJ(balance); balance -= bet; @@ -23,9 +22,8 @@ int blackjack(){ } int getBalanceBJ(){ - int balance = 0; printf("Guthaben einwerfen: "); - scanf("%d", &balance); + int balance = userInputBJ(); printf("Dein Guthaben: %d\n", balance); return balance; } From 4dca4bb034ff4309508e4582e0aaad3117111b82 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 19:59:11 +0100 Subject: [PATCH 15/46] Spiel verlassen, wenn du 0 eingibst + noch ein Test --- src/main/c/Stefan/blackjack.c | 17 +++++++++++++---- src/main/c/Stefan/blackjack.h | 2 +- src/test/c/Stefan/test_blackjack.c | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 9c40be9..4af7f15 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -8,14 +8,20 @@ int blackjack(){ printf("Welcome to Blackjack \n\n"); + bool exit = false; + bool *exitPtr = &exit; int balance = getBalanceBJ(); while(balance > 0){ printf("Aktuelles Guthaben: %d\n", balance); - int bet = getBetBJ(balance); + int bet = getBetBJ(balance, exitPtr); + if(exit) { + return -1; + } + balance -= bet; - playBJ(bet, balance); + balance = playBJ(bet, balance); } return 0; @@ -24,18 +30,21 @@ int blackjack(){ int getBalanceBJ(){ printf("Guthaben einwerfen: "); int balance = userInputBJ(); - printf("Dein Guthaben: %d\n", balance); return balance; } -int getBetBJ(int balance){ +int getBetBJ(int balance, bool *exitPtr){ while(1){ + printf("\nGib (0) ein, um Blackjack zu verlassen.\n"); printf("Wetteinsatz: "); int bet = userInputBJ(); if(bet <= balance && bet > 0){ return bet; + } else if(bet == 0) { + *exitPtr = true; + break; } else { printf("Ungueltige Eingabe!\n"); } diff --git a/src/main/c/Stefan/blackjack.h b/src/main/c/Stefan/blackjack.h index adc9a15..5b2bc79 100644 --- a/src/main/c/Stefan/blackjack.h +++ b/src/main/c/Stefan/blackjack.h @@ -5,7 +5,7 @@ int blackjack(); int getBalanceBJ(); -int getBetBJ(int balance); +int getBetBJ(int balance, bool *exitPtr); int userInputBJ(); int getRandCard(); int playBJ(int bet, int balance); diff --git a/src/test/c/Stefan/test_blackjack.c b/src/test/c/Stefan/test_blackjack.c index 25d5957..a31db25 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -102,6 +102,20 @@ void test_cards_ace_10_total_equals_21() { } +void test_cards_ace_ace_9_total_equals_21() { + + //arrange + int userCards[] = { 11, 11, 9}; + int expected_result = 21; + + //act + int actual_result = calculateCardsTotal(userCards, 3); + + //assert + TEST_ASSERT_EQUAL_INT(expected_result, actual_result); + +} + void test_blackjack_bet_equals_10() { //arrange From 217bd7cc4844af1010516d3ffb1b5f87d7cfe06e Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 20:08:48 +0100 Subject: [PATCH 16/46] 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; From 7e078d20c6127f56145c003ba99fb23c1edf162c Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 20:17:37 +0100 Subject: [PATCH 17/46] =?UTF-8?q?zufaellig=20generierte=20Karten=20sind=20?= =?UTF-8?q?jetzt=20von=20der=20Zeit=20abh=C3=A4ngig=20+=20weiterr=20Test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Stefan/blackjack.c | 14 ++++++++------ src/main/c/Stefan/blackjack.h | 2 +- src/test/c/Stefan/test_blackjack.c | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index b39ce29..8fc8042 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -2,6 +2,7 @@ #include #include #include +#include int blackjack(){ @@ -65,7 +66,8 @@ int userInputBJ(){ // Liefert eine Zufallszahl zwischen 1 und 10 // Chance für 10 ist 4 Mal höher als für den Rest // 1 = Ass -int getRandCard(){ +int getRandCard(int offset){ + srand(time(NULL) + offset); int r = rand() % 13; r++; @@ -91,16 +93,16 @@ int playBJ(int bet, int balance){ int *dealerCardsArray = (int*) malloc(10 * sizeof(int)); if(dealerCardsArray){ - dealerCardsArray[0] = getRandCard(); + dealerCardsArray[0] = getRandCard(0); printf("computer rand card: %d\n", dealerCardsArray[0]); int *userCardsArray = (int*) malloc(10 * sizeof(int)); if(userCardsArray){ - userCardsArray[0] = getRandCard(); + userCardsArray[0] = getRandCard(1); printf("user rand card: %d\n", userCardsArray[0]); - userCardsArray[1] = getRandCard(); + userCardsArray[1] = getRandCard(2); printf("user rand card: %d\n", userCardsArray[1]); if(checkForBlackjack(userCardsArray)){ @@ -125,7 +127,7 @@ int playBJ(int bet, int balance){ } if(hit()){ - userCardsArray[i] = getRandCard(); + userCardsArray[i] = getRandCard(i); printf("new card: %d\n", userCardsArray[i]); } else{ break; @@ -138,7 +140,7 @@ int playBJ(int bet, int balance){ dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i); if(dealerCardsTotal < 17){ - dealerCardsArray[i] = getRandCard(); + dealerCardsArray[i] = getRandCard(i); printf("Computer Card: %d\n", dealerCardsArray[i]); dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i + 1); } diff --git a/src/main/c/Stefan/blackjack.h b/src/main/c/Stefan/blackjack.h index 5b2bc79..ca3e08b 100644 --- a/src/main/c/Stefan/blackjack.h +++ b/src/main/c/Stefan/blackjack.h @@ -7,7 +7,7 @@ int blackjack(); int getBalanceBJ(); int getBetBJ(int balance, bool *exitPtr); int userInputBJ(); -int getRandCard(); +int getRandCard(int offset); int playBJ(int bet, int balance); bool checkForBlackjack(int userCards[]); int calculateCardsTotal(int cardsArray[], int len); diff --git a/src/test/c/Stefan/test_blackjack.c b/src/test/c/Stefan/test_blackjack.c index a31db25..cd067cf 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -116,6 +116,20 @@ void test_cards_ace_ace_9_total_equals_21() { } +void test_cards_10_10_ace_total_equals_21() { + + //arrange + int userCards[] = { 10, 10, 11}; + int expected_result = 21; + + //act + int actual_result = calculateCardsTotal(userCards, 3); + + //assert + TEST_ASSERT_EQUAL_INT(expected_result, actual_result); + +} + void test_blackjack_bet_equals_10() { //arrange From 3f003c2f9b1ae3b0d4a2f0a4d51119cbec44ea8c Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 20:24:06 +0100 Subject: [PATCH 18/46] refactoring: bessere Willkommensnachricht --- src/main/c/Stefan/blackjack.c | 16 ++++++++++++++-- src/main/c/Stefan/blackjack.h | 1 + 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 8fc8042..112c91b 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -6,8 +6,7 @@ int blackjack(){ - - printf("Welcome to Blackjack \n\n"); + welcomeMessageBJ(); bool exit = false; bool *exitPtr = &exit; @@ -227,4 +226,17 @@ int calculateWinnings(bool blackjack, int bet){ } printf("Du hast %d gewonnen!\n", bet); return 2 * bet; +} + +void welcomeMessageBJ(){ + + printf("Willkommen zu..\n\n" + " _ _ _ _ _ \n" + "| | | | | | (_) | | \n" + "| |__ | | __ _ ___| | ___ __ _ ___| | __\n" + "| '_ \\| |/ _` |/ __| |/ / |/ _` |/ __| |/ /\n" + "| |_) | | (_| | (__| <| | (_| | (__| < \n" + "|_.__/|_|\\__,_|\\___|_|\\_\\ |\\__,_|\\___|_|\\_\\\n" + " _/ | \n" + " |__/ \n\n"); } \ No newline at end of file diff --git a/src/main/c/Stefan/blackjack.h b/src/main/c/Stefan/blackjack.h index ca3e08b..3723164 100644 --- a/src/main/c/Stefan/blackjack.h +++ b/src/main/c/Stefan/blackjack.h @@ -4,6 +4,7 @@ #include int blackjack(); +void welcomeMessageBJ(); int getBalanceBJ(); int getBetBJ(int balance, bool *exitPtr); int userInputBJ(); From aa64b5526b23055f12b26603996a778728ec45ed Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 20:29:48 +0100 Subject: [PATCH 19/46] refactoring: konstanten Wert durch konstante Variable ersetzt --- src/main/c/Stefan/blackjack.c | 10 +++++----- src/main/c/Stefan/blackjack.h | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 112c91b..deadcdf 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -68,19 +68,19 @@ int userInputBJ(){ int getRandCard(int offset){ srand(time(NULL) + offset); - int r = rand() % 13; + int r = rand() % ANZ_MOEGLICHE_KARTEN; r++; - + // 11, 12, 13 = Bube, Dame, König haben den Wert 10 if(r > 10){ return 10; - } else if(r == 1){ + } + // Ass (1) hat erstmal den Wert 11 + else if(r == 1){ return 11; } return r; - - } diff --git a/src/main/c/Stefan/blackjack.h b/src/main/c/Stefan/blackjack.h index 3723164..f20062f 100644 --- a/src/main/c/Stefan/blackjack.h +++ b/src/main/c/Stefan/blackjack.h @@ -3,6 +3,8 @@ #include +#define ANZ_MOEGLICHE_KARTEN 13 + int blackjack(); void welcomeMessageBJ(); int getBalanceBJ(); From 8f26a6f8a43a9716e5104c2d520c15f787193aaa Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Tue, 6 Feb 2024 12:39:24 +0100 Subject: [PATCH 20/46] refactoring: neue konstante Variable MAX_CARDS definiert --- src/main/c/Stefan/blackjack.c | 10 ++++++---- src/main/c/Stefan/blackjack.h | 1 + 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index deadcdf..e7db0af 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -89,13 +89,15 @@ int playBJ(int bet, int balance){ int winnings = 0; int userCardsTotal = 0, dealerCardsTotal = 0; - int *dealerCardsArray = (int*) malloc(10 * sizeof(int)); + // Array mit max 10 Karten anlegen + int *dealerCardsArray = (int*) malloc(MAX_CARDS * sizeof(int)); if(dealerCardsArray){ dealerCardsArray[0] = getRandCard(0); printf("computer rand card: %d\n", dealerCardsArray[0]); - int *userCardsArray = (int*) malloc(10 * sizeof(int)); + // Array mit max 10 Karten anlegen + int *userCardsArray = (int*) malloc(MAX_CARDS * sizeof(int)); if(userCardsArray){ @@ -111,7 +113,7 @@ int playBJ(int bet, int balance){ - for(int i = 2; i < 10; i++){ + for(int i = 2; i < MAX_CARDS; i++){ userCardsTotal = calculateCardsTotal(userCardsArray, i); @@ -134,7 +136,7 @@ int playBJ(int bet, int balance){ } - for(int i = 1; i < 10; i++){ + for(int i = 1; i < MAX_CARDS; i++){ dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i); diff --git a/src/main/c/Stefan/blackjack.h b/src/main/c/Stefan/blackjack.h index f20062f..c55c744 100644 --- a/src/main/c/Stefan/blackjack.h +++ b/src/main/c/Stefan/blackjack.h @@ -4,6 +4,7 @@ #include #define ANZ_MOEGLICHE_KARTEN 13 +#define MAX_CARDS 10 int blackjack(); void welcomeMessageBJ(); From d7a0fa451148e92fcb3be8eb22fbadb04ef3426b Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Tue, 6 Feb 2024 12:45:25 +0100 Subject: [PATCH 21/46] refactoring: Konsolenausgaben etwas schoener geschrieben/formatiert --- src/main/c/Stefan/blackjack.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index e7db0af..4935dc3 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -117,10 +117,10 @@ int playBJ(int bet, int balance){ userCardsTotal = calculateCardsTotal(userCardsArray, i); - printf("Your Cards Total : %d\n", userCardsTotal); + printf("Your Cards Total: %d\n\n", userCardsTotal); if(userCardsTotal > 21){ - printf("BUST\n"); + printf("YOU BUST\n\n"); free(userCardsArray); free(dealerCardsArray); @@ -129,7 +129,7 @@ int playBJ(int bet, int balance){ if(hit()){ userCardsArray[i] = getRandCard(i); - printf("new card: %d\n", userCardsArray[i]); + printf("Your new card: %d\n", userCardsArray[i]); } else{ break; } @@ -142,10 +142,10 @@ int playBJ(int bet, int balance){ if(dealerCardsTotal < 17){ dealerCardsArray[i] = getRandCard(i); - printf("Computer Card: %d\n", dealerCardsArray[i]); + printf("Dealer Card: %d\n", dealerCardsArray[i]); dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i + 1); } - printf("Computer Cards Total: %d\n", dealerCardsTotal); + printf("Dealer Cards Total: %d\n", dealerCardsTotal); if(dealerCardsTotal >= 17 && dealerCardsTotal < 21){ if(calculateIfYouWon(userCardsTotal, dealerCardsTotal)){ @@ -155,7 +155,7 @@ int playBJ(int bet, int balance){ } if(dealerCardsTotal > 21){ - printf("COMPUTER BUST\n"); + printf("DEALER BUST\n\n"); winnings = calculateWinnings(false, bet); break; } @@ -178,7 +178,7 @@ int playBJ(int bet, int balance){ bool checkForBlackjack(int userCardArray[]){ if(userCardArray[0] + userCardArray[1] == 21){ - printf("BLACKJACK!\n"); + printf("BLACKJACK!\n\n"); return true; } return false; @@ -191,10 +191,10 @@ bool hit(){ scanf("%d", &answer); if(answer != 1){ - printf("stand.\n"); + printf("You stand.\n\n"); return false; } - printf("hit.\n"); + printf("You hit.\n\n"); return true; } From 54dd487313b5423303d582bc4fc9adb0a2c9083e Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Tue, 6 Feb 2024 12:52:47 +0100 Subject: [PATCH 22/46] refactoring: variable umbennant: r zu randomCard --- src/main/c/Stefan/blackjack.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 4935dc3..ff54699 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -68,19 +68,19 @@ int userInputBJ(){ int getRandCard(int offset){ srand(time(NULL) + offset); - int r = rand() % ANZ_MOEGLICHE_KARTEN; - r++; + int randomCard = rand() % ANZ_MOEGLICHE_KARTEN; + randomCard++; // 11, 12, 13 = Bube, Dame, König haben den Wert 10 - if(r > 10){ + if(randomCard > 10){ return 10; } // Ass (1) hat erstmal den Wert 11 - else if(r == 1){ + else if(randomCard == 1){ return 11; } - return r; + return randomCard; } From bdfdea9f467b3ae6ec1d7a1ce5ef3d17346c4aa7 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Tue, 6 Feb 2024 13:04:07 +0100 Subject: [PATCH 23/46] getRandomCard mehr random gemacht + bugfix: dealer zieht bei 21 keine neue Karte --- src/main/c/Stefan/blackjack.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index ff54699..b648de5 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -66,10 +66,13 @@ int userInputBJ(){ // Chance für 10 ist 4 Mal höher als für den Rest // 1 = Ass int getRandCard(int offset){ - srand(time(NULL) + offset); + int randomCard; + srand(time(NULL)); - int randomCard = rand() % ANZ_MOEGLICHE_KARTEN; - randomCard++; + for(int i = 0; i < offset; i++){ + + randomCard = (rand() % ANZ_MOEGLICHE_KARTEN) + 1; + } // 11, 12, 13 = Bube, Dame, König haben den Wert 10 if(randomCard > 10){ @@ -147,7 +150,7 @@ int playBJ(int bet, int balance){ } printf("Dealer Cards Total: %d\n", dealerCardsTotal); - if(dealerCardsTotal >= 17 && dealerCardsTotal < 21){ + if(dealerCardsTotal >= 17 && dealerCardsTotal <= 21){ if(calculateIfYouWon(userCardsTotal, dealerCardsTotal)){ winnings = calculateWinnings(false, bet); } @@ -215,6 +218,7 @@ int calculateCardsTotal(int cardsArray[], int len){ } if(aceFound && sum + cardsArray[i] > 21){ sum -= 10; + aceFound = false; } sum += cardsArray[i]; } From a3e2b5a9e273694dafe0923b852e612f39d6c732 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Tue, 6 Feb 2024 14:14:05 +0100 Subject: [PATCH 24/46] Implementierung Karte zu ASCII art konvertieren + Test --- src/main/c/Stefan/blackjack.c | 30 ++++++++++++++++++++++++++++++ src/main/c/Stefan/blackjack.h | 1 + src/test/c/Stefan/test_blackjack.c | 16 +++++++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index b648de5..52cdba1 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -245,4 +245,34 @@ void welcomeMessageBJ(){ "|_.__/|_|\\__,_|\\___|_|\\_\\ |\\__,_|\\___|_|\\_\\\n" " _/ | \n" " |__/ \n\n"); +} + +char generateCardAscii(int cardValue){ + + char cardValueAsChar, possibleZero = ' '; + + if(cardValue == 11){ + cardValueAsChar = 'A'; + } else if(cardValue == 10){ + srand(time(NULL)); + int randomValue = rand()%4; + + switch(randomValue){ + case(0): cardValueAsChar = 'J'; break; + case(1): cardValueAsChar = 'Q'; break; + case(2): cardValueAsChar = 'K'; break; + case(4): cardValueAsChar = '1'; + possibleZero = '0'; break; + default: cardValueAsChar = 'E'; break; // ERROR + } + } else { + cardValueAsChar = cardValue + '0'; + } + + printf(" ___\n" + " |%c%c |\n" + " | |\n" + " |___| \n\n", cardValueAsChar, possibleZero); + + return cardValueAsChar; } \ No newline at end of file diff --git a/src/main/c/Stefan/blackjack.h b/src/main/c/Stefan/blackjack.h index c55c744..2e610b2 100644 --- a/src/main/c/Stefan/blackjack.h +++ b/src/main/c/Stefan/blackjack.h @@ -18,5 +18,6 @@ int calculateCardsTotal(int cardsArray[], int len); int calculateWinnings(bool blackjack, int bet); bool hit(); bool calculateIfYouWon(int userCardsTotal, int computerCardsTotal); +char generateCardAscii(int cardValue); #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 cd067cf..675acac 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -184,4 +184,18 @@ void test_calculate_if_you_won_20_vs_21() { //assert TEST_ASSERT_FALSE(result); -} \ No newline at end of file +} + +void test_generate_char_for_card_2() { + + //arrange + int cardValue = 2; + char expected_result = '2'; + + //act + char actual_result = generateCardAscii(cardValue); + + //assert + TEST_ASSERT_EQUAL_CHAR(expected_result, actual_result); + +} From c8ec59c392cbd30421be77e2f4f4f4deee628af8 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Tue, 6 Feb 2024 14:19:50 +0100 Subject: [PATCH 25/46] Karte ASCII wird jedes mal angezeigt, wenn eine neue Karte gezogen wird + Test --- src/main/c/Stefan/blackjack.c | 6 ++++++ src/test/c/Stefan/test_blackjack.c | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 52cdba1..c12ad2b 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -98,6 +98,7 @@ int playBJ(int bet, int balance){ if(dealerCardsArray){ dealerCardsArray[0] = getRandCard(0); printf("computer rand card: %d\n", dealerCardsArray[0]); + generateCardAscii(dealerCardsArray[0]); // Array mit max 10 Karten anlegen int *userCardsArray = (int*) malloc(MAX_CARDS * sizeof(int)); @@ -106,8 +107,11 @@ int playBJ(int bet, int balance){ userCardsArray[0] = getRandCard(1); printf("user rand card: %d\n", userCardsArray[0]); + generateCardAscii(userCardsArray[0]); userCardsArray[1] = getRandCard(2); printf("user rand card: %d\n", userCardsArray[1]); + generateCardAscii(userCardsArray[1]); + if(checkForBlackjack(userCardsArray)){ winnings = calculateWinnings(true, bet); @@ -133,6 +137,7 @@ int playBJ(int bet, int balance){ if(hit()){ userCardsArray[i] = getRandCard(i); printf("Your new card: %d\n", userCardsArray[i]); + generateCardAscii(userCardsArray[i]); } else{ break; } @@ -146,6 +151,7 @@ int playBJ(int bet, int balance){ if(dealerCardsTotal < 17){ dealerCardsArray[i] = getRandCard(i); printf("Dealer Card: %d\n", dealerCardsArray[i]); + generateCardAscii(dealerCardsArray[i]); dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i + 1); } printf("Dealer Cards Total: %d\n", dealerCardsTotal); diff --git a/src/test/c/Stefan/test_blackjack.c b/src/test/c/Stefan/test_blackjack.c index 675acac..203978c 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -199,3 +199,17 @@ void test_generate_char_for_card_2() { TEST_ASSERT_EQUAL_CHAR(expected_result, actual_result); } + +void test_generate_char_for_card_ace() { + + //arrange + int cardValue = 11; + char expected_result = 'A'; + + //act + char actual_result = generateCardAscii(cardValue); + + //assert + TEST_ASSERT_EQUAL_CHAR(expected_result, actual_result); + +} From 618a1ca2299618123a32d5b8d564ac392d655c28 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Tue, 6 Feb 2024 14:27:19 +0100 Subject: [PATCH 26/46] Test zu ASCII art generierung geschrieben --- src/test/c/Stefan/test_blackjack.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/test/c/Stefan/test_blackjack.c b/src/test/c/Stefan/test_blackjack.c index 203978c..f65013b 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -213,3 +213,17 @@ void test_generate_char_for_card_ace() { TEST_ASSERT_EQUAL_CHAR(expected_result, actual_result); } + +void test_generate_char_for_card_9() { + + //arrange + int cardValue = 9; + char expected_result = '9'; + + //act + char actual_result = generateCardAscii(cardValue); + + //assert + TEST_ASSERT_EQUAL_CHAR(expected_result, actual_result); + +} From 8de1b3e47f4d358b5e62eb7f02138102c1181961 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Tue, 6 Feb 2024 14:38:16 +0100 Subject: [PATCH 27/46] refactoring: Konsolenausgabe der Karten nur noch via ASCII art --- src/main/c/Stefan/blackjack.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index c12ad2b..c358caf 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -97,7 +97,7 @@ int playBJ(int bet, int balance){ if(dealerCardsArray){ dealerCardsArray[0] = getRandCard(0); - printf("computer rand card: %d\n", dealerCardsArray[0]); + printf("\nDealer's first card: \n"); generateCardAscii(dealerCardsArray[0]); // Array mit max 10 Karten anlegen @@ -106,10 +106,9 @@ int playBJ(int bet, int balance){ if(userCardsArray){ userCardsArray[0] = getRandCard(1); - printf("user rand card: %d\n", userCardsArray[0]); + printf("\nYour first two cards: \n"); generateCardAscii(userCardsArray[0]); userCardsArray[1] = getRandCard(2); - printf("user rand card: %d\n", userCardsArray[1]); generateCardAscii(userCardsArray[1]); From 28ff8b9ff058e4b3bf42bd475b394d673e607388 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Tue, 6 Feb 2024 14:43:34 +0100 Subject: [PATCH 28/46] bugfix: Karte mit Wert 10 kann auch Koenig sein + Test --- src/main/c/Stefan/blackjack.c | 2 +- src/test/c/Stefan/test_blackjack.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index c358caf..0b376f2 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -266,7 +266,7 @@ char generateCardAscii(int cardValue){ case(0): cardValueAsChar = 'J'; break; case(1): cardValueAsChar = 'Q'; break; case(2): cardValueAsChar = 'K'; break; - case(4): cardValueAsChar = '1'; + case(3): cardValueAsChar = '1'; possibleZero = '0'; break; default: cardValueAsChar = 'E'; break; // ERROR } diff --git a/src/test/c/Stefan/test_blackjack.c b/src/test/c/Stefan/test_blackjack.c index f65013b..9497d5e 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -227,3 +227,17 @@ void test_generate_char_for_card_9() { TEST_ASSERT_EQUAL_CHAR(expected_result, actual_result); } + +void test_generate_char_for_card_5() { + + //arrange + int cardValue = 5; + char expected_result = '5'; + + //act + char actual_result = generateCardAscii(cardValue); + + //assert + TEST_ASSERT_EQUAL_CHAR(expected_result, actual_result); + +} \ No newline at end of file From 62a8c6be3d66fa865a19596bef393fa9d334d010 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Tue, 6 Feb 2024 14:54:14 +0100 Subject: [PATCH 29/46] refactoring: Jede Runde wird per Konsolenausgabe strukturiert --- src/main/c/Stefan/blackjack.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 0b376f2..4849073 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -14,6 +14,7 @@ int blackjack(){ while(balance > 0){ printf("Aktuelles Guthaben: %d\n", balance); + printf("-----------------------------\n"); int bet = getBetBJ(balance, exitPtr); // Abbruchbedingung, um das Blackjack zu verlassen if(exit) { @@ -200,6 +201,7 @@ bool hit(){ if(answer != 1){ printf("You stand.\n\n"); + printf("-----------------------------\n"); return false; } printf("You hit.\n\n"); From fe959d46cacd2a38153e8f657df005b3002c3a1e Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Wed, 7 Feb 2024 09:53:40 +0100 Subject: [PATCH 30/46] refactoring: Code cleaner formatiert + Kommentare geschrieben --- src/main/c/Stefan/blackjack.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 4849073..842ec03 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -97,6 +97,7 @@ int playBJ(int bet, int balance){ int *dealerCardsArray = (int*) malloc(MAX_CARDS * sizeof(int)); if(dealerCardsArray){ + // Erste Karte vom Dealer ziehen und anzeigen dealerCardsArray[0] = getRandCard(0); printf("\nDealer's first card: \n"); generateCardAscii(dealerCardsArray[0]); @@ -105,45 +106,50 @@ int playBJ(int bet, int balance){ int *userCardsArray = (int*) malloc(MAX_CARDS * sizeof(int)); if(userCardsArray){ - - userCardsArray[0] = getRandCard(1); + // Ersten 2 Karten vom Spieler ziehen und anzeigen printf("\nYour first two cards: \n"); + userCardsArray[0] = getRandCard(1); generateCardAscii(userCardsArray[0]); userCardsArray[1] = getRandCard(2); generateCardAscii(userCardsArray[1]); - + // Ersten zwei Karten auf Blackjack überprüfen if(checkForBlackjack(userCardsArray)){ + // Wenn Blackjack, dann Gewinn berechnen und Guthaben aktualisieren winnings = calculateWinnings(true, bet); return balance + winnings; } - - + // Spieler kann maximal 10 Karten ziehen for(int i = 2; i < MAX_CARDS; i++){ + // Gesamtwert vom Spieler berechnen und anzeigen userCardsTotal = calculateCardsTotal(userCardsArray, i); - printf("Your Cards Total: %d\n\n", userCardsTotal); + // Gesamtwert > 21 -> Verloren if(userCardsTotal > 21){ printf("YOU BUST\n\n"); + // Spieler und Dealer Karten zurücksetzen free(userCardsArray); free(dealerCardsArray); return balance; } + // Spieler fragen, ob hit oder stand if(hit()){ + // Wenn hit, neue Karte ziehen und anzeigen userCardsArray[i] = getRandCard(i); printf("Your new card: %d\n", userCardsArray[i]); generateCardAscii(userCardsArray[i]); } else{ + // Wenn stand, Schleife abbrechen, nicht mehr ziehen break; } - } + // Dealer kann maximal 10 Karten ziehen for(int i = 1; i < MAX_CARDS; i++){ dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i); From 7619ce3c06b44751b6c22ed3b04a0d15a68e069f Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Wed, 7 Feb 2024 10:08:07 +0100 Subject: [PATCH 31/46] refactoring: input innerhalb hit() nach userInputBJ() verlagert + Kommentare --- src/main/c/Stefan/blackjack.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 842ec03..bc03803 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -89,7 +89,6 @@ int getRandCard(int offset){ int playBJ(int bet, int balance){ - int winnings = 0; int userCardsTotal = 0, dealerCardsTotal = 0; @@ -154,6 +153,7 @@ int playBJ(int bet, int balance){ dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i); + // Wenn Dealer total < 17, muss er noch eine Karte ziehen if(dealerCardsTotal < 17){ dealerCardsArray[i] = getRandCard(i); printf("Dealer Card: %d\n", dealerCardsArray[i]); @@ -162,35 +162,36 @@ int playBJ(int bet, int balance){ } printf("Dealer Cards Total: %d\n", dealerCardsTotal); + // Wenn Dealer total > 17 und <= 21, dann keine Karte mehr ziehen if(dealerCardsTotal >= 17 && dealerCardsTotal <= 21){ + // Überprüfen, wer gewonnen hat und entsprechend Gewinn berechnen if(calculateIfYouWon(userCardsTotal, dealerCardsTotal)){ winnings = calculateWinnings(false, bet); } break; } + // Wenn dealer total > 21, dann hat der Spieler gewonnen + Gewinn berechnen if(dealerCardsTotal > 21){ printf("DEALER BUST\n\n"); winnings = calculateWinnings(false, bet); break; } - - } - - - + // Speicher wieder freigeben/zurücksetzen free(userCardsArray); } + // Speicher wieder freigeben/zurücksetzen free(dealerCardsArray); } return balance + winnings; } +// Wenn die ersten beiden Karten in Summe den Wert 21 haben, heißt das Blackjack bool checkForBlackjack(int userCardArray[]){ if(userCardArray[0] + userCardArray[1] == 21){ printf("BLACKJACK!\n\n"); @@ -200,10 +201,8 @@ bool checkForBlackjack(int userCardArray[]){ } bool hit(){ - - int answer; printf("Hit (1) or stand (2): "); - scanf("%d", &answer); + int answer = userInputBJ(); if(answer != 1){ printf("You stand.\n\n"); From 14262404e26485661214505eae5f579f6cc8e201 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Wed, 7 Feb 2024 10:16:51 +0100 Subject: [PATCH 32/46] refactoring: Variablen in calculateCardsTotal() umbennant + Kommentare --- src/main/c/Stefan/blackjack.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index bc03803..465a57f 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -222,19 +222,21 @@ bool calculateIfYouWon(int userCardsTotal, int computerCardsTotal){ } int calculateCardsTotal(int cardsArray[], int len){ - int sum = 0; - bool aceFound = false; + int total = 0; + bool acesFound = false; for(int i = 0; i < len; i++){ if(cardsArray[i] == 11){ - aceFound = true; + acesFound = true; } - if(aceFound && sum + cardsArray[i] > 21){ - sum -= 10; - aceFound = false; + // Wenn der total > 21, aber man hat ein Ass, dann zählt hat das Ass den Wert 1, nicht 11 + if(acesFound && total + cardsArray[i] > 21){ + total -= 10; + // Das gleiche Ass kann nicht mehrmals den total um 10 senken + acesFound = false; } - sum += cardsArray[i]; + total += cardsArray[i]; } - return sum; + return total; } int calculateWinnings(bool blackjack, int bet){ From ec637ee097996c2b29acaa180efaab60480337b3 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Wed, 7 Feb 2024 10:21:56 +0100 Subject: [PATCH 33/46] mehrere Asse koennen jetzt den Wert 1 haben + Test --- src/main/c/Stefan/blackjack.c | 8 ++++---- src/test/c/Stefan/test_blackjack.c | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 465a57f..838b676 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -223,16 +223,16 @@ bool calculateIfYouWon(int userCardsTotal, int computerCardsTotal){ int calculateCardsTotal(int cardsArray[], int len){ int total = 0; - bool acesFound = false; + int acesFound = 0; for(int i = 0; i < len; i++){ if(cardsArray[i] == 11){ - acesFound = true; + acesFound++; } // Wenn der total > 21, aber man hat ein Ass, dann zählt hat das Ass den Wert 1, nicht 11 - if(acesFound && total + cardsArray[i] > 21){ + if(acesFound > 0 && total + cardsArray[i] > 21){ total -= 10; // Das gleiche Ass kann nicht mehrmals den total um 10 senken - acesFound = false; + acesFound--; } total += cardsArray[i]; } diff --git a/src/test/c/Stefan/test_blackjack.c b/src/test/c/Stefan/test_blackjack.c index 9497d5e..bd10d9e 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -130,6 +130,20 @@ void test_cards_10_10_ace_total_equals_21() { } +void test_cards_ace_ace_10_8_total_equals_20() { + + //arrange + int userCards[] = { 11, 11, 10, 8}; + int expected_result = 20; + + //act + int actual_result = calculateCardsTotal(userCards, 4); + + //assert + TEST_ASSERT_EQUAL_INT(expected_result, actual_result); + +} + void test_blackjack_bet_equals_10() { //arrange From d8e49a936c5ac03abba30f0bb0a83cea6753a200 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Wed, 7 Feb 2024 10:24:00 +0100 Subject: [PATCH 34/46] weitere Tests: mehrere Asse korrekt berechnen --- src/test/c/Stefan/test_blackjack.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/test/c/Stefan/test_blackjack.c b/src/test/c/Stefan/test_blackjack.c index bd10d9e..feab3b0 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -144,6 +144,34 @@ void test_cards_ace_ace_10_8_total_equals_20() { } +void test_cards_ace_ace_10_9_total_equals_21() { + + //arrange + int userCards[] = { 11, 11, 10, 9}; + int expected_result = 21; + + //act + int actual_result = calculateCardsTotal(userCards, 4); + + //assert + TEST_ASSERT_EQUAL_INT(expected_result, actual_result); + +} + +void test_cards_ace_ace_ace_10_total_equals_21() { + + //arrange + int userCards[] = { 11, 11, 11, 10}; + int expected_result = 13; + + //act + int actual_result = calculateCardsTotal(userCards, 4); + + //assert + TEST_ASSERT_EQUAL_INT(expected_result, actual_result); + +} + void test_blackjack_bet_equals_10() { //arrange From 77d4338a759e6ca35517665e5ec439af749bdfab Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Wed, 7 Feb 2024 10:28:38 +0100 Subject: [PATCH 35/46] Implementierung Funktion, die Wetteinsatz vom Guthaben abzieht + Test --- src/main/c/Stefan/blackjack.c | 6 +++++- src/main/c/Stefan/blackjack.h | 1 + src/test/c/Stefan/test_blackjack.c | 17 ++++++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 838b676..c22ae30 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -22,7 +22,7 @@ int blackjack(){ } // Bei jeder Runde wird der Wetteinsatz vom Guthaben abgezogen - balance -= bet; + balance = subtractBetFromBalanceBJ(bet, balance); // Nach jeder Runde wird das Guthaben aktualisiert balance = playBJ(bet, balance); @@ -31,6 +31,10 @@ int blackjack(){ return 0; } +int subtractBetFromBalanceBJ(int bet, int balance){ + return balance - bet; +} + int getBalanceBJ(){ printf("Guthaben einwerfen: "); int balance = userInputBJ(); diff --git a/src/main/c/Stefan/blackjack.h b/src/main/c/Stefan/blackjack.h index 2e610b2..bb70cd7 100644 --- a/src/main/c/Stefan/blackjack.h +++ b/src/main/c/Stefan/blackjack.h @@ -19,5 +19,6 @@ int calculateWinnings(bool blackjack, int bet); bool hit(); bool calculateIfYouWon(int userCardsTotal, int computerCardsTotal); char generateCardAscii(int cardValue); +int subtractBetFromBalanceBJ(int bet, int balance); #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 feab3b0..7790cd9 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -282,4 +282,19 @@ void test_generate_char_for_card_5() { //assert TEST_ASSERT_EQUAL_CHAR(expected_result, actual_result); -} \ No newline at end of file +} + +void test_subtract_bet_10_from_balance_100() { + + //arrange + int bet = 10; + int balance = 100; + int expected_result = 90; + + //act + int actual_result = subtractBetFromBalanceBJ(bet, balance); + + //assert + TEST_ASSERT_EQUAL_INT(expected_result, actual_result); + +} From 929d83003c6b30c3f8149c921d89dcfec256141d Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Wed, 7 Feb 2024 10:30:40 +0100 Subject: [PATCH 36/46] weitere Tests zur Berechnung Guthaben --- src/test/c/Stefan/test_blackjack.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/test/c/Stefan/test_blackjack.c b/src/test/c/Stefan/test_blackjack.c index 7790cd9..6a9dcd6 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -298,3 +298,33 @@ void test_subtract_bet_10_from_balance_100() { TEST_ASSERT_EQUAL_INT(expected_result, actual_result); } + +void test_subtract_bet_100_from_balance_100() { + + //arrange + int bet = 100; + int balance = 100; + int expected_result = 0; + + //act + int actual_result = subtractBetFromBalanceBJ(bet, balance); + + //assert + TEST_ASSERT_EQUAL_INT(expected_result, actual_result); + +} + +void test_subtract_bet_1_from_balance_100000() { + + //arrange + int bet = 1; + int balance = 100000; + int expected_result = 99999; + + //act + int actual_result = subtractBetFromBalanceBJ(bet, balance); + + //assert + TEST_ASSERT_EQUAL_INT(expected_result, actual_result); + +} \ No newline at end of file From 582d5b2e4bb9983cb0419c112f7f2948a34efdb7 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Wed, 7 Feb 2024 11:13:26 +0100 Subject: [PATCH 37/46] Tests fuer slot machine Gewinnberechnung --- src/test/c/Stefan/test_slot_machine.c | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/test/c/Stefan/test_slot_machine.c b/src/test/c/Stefan/test_slot_machine.c index f2a17c9..ff44d97 100644 --- a/src/test/c/Stefan/test_slot_machine.c +++ b/src/test/c/Stefan/test_slot_machine.c @@ -219,6 +219,34 @@ void test_3_B_3_C_3_D_won_bet_equals_10() { //act int actualResult = getWinnings(testSymbols, 10); + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_3_A_3_C_3_D_won_bet_equals_10() { + + //arrange + char testSymbols[] = {"AAACCCDDD"}; + + int expectedResult = 330; + + //act + int actualResult = getWinnings(testSymbols, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_3_A_3_B_3_C_won_bet_equals_10() { + + //arrange + char testSymbols[] = {"AAABBBCCC"}; + + int expectedResult = 180; + + //act + int actualResult = getWinnings(testSymbols, 10); + //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } \ No newline at end of file From 2618917dd70dc2e33125c9c0053598d714e74b68 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Wed, 7 Feb 2024 11:24:27 +0100 Subject: [PATCH 38/46] Implementierung Max Win bei slot machine + Test --- src/main/c/Stefan/slot_machine.c | 17 +++++++++++++++++ src/main/c/Stefan/slot_machine.h | 2 ++ src/test/c/Stefan/test_slot_machine.c | 4 ++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index c3dbd65..d959827 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -3,6 +3,7 @@ #include #include #include +#include void slotMachine(){ @@ -117,6 +118,11 @@ int getWinnings(char symbols[], int bet){ } } } + + if(checkForMaxWin(symbols)){ + winnings *= 10; + } + if (winnings > 0) { printf("\nDu hast %d gewonnen!\n", winnings - bet); } @@ -127,6 +133,17 @@ int getWinnings(char symbols[], int bet){ return winnings; } +bool checkForMaxWin(char symbols[]){ + bool maxWin = true; + for(int i = 1; i < NUM_OF_SYMBOLS; i++){ + if(symbols[0] != symbols[i]){ + maxWin = false; + return maxWin; + } + } + return maxWin; +} + void showResult(char symbols[], int winnings){ char winnerMessage[10] = {""}; diff --git a/src/main/c/Stefan/slot_machine.h b/src/main/c/Stefan/slot_machine.h index c08b4bb..0e02b9f 100644 --- a/src/main/c/Stefan/slot_machine.h +++ b/src/main/c/Stefan/slot_machine.h @@ -1,6 +1,7 @@ #ifndef SLOT_MACHINE_H #define SLOT_MACHINE_H +#include #define NUM_OF_SYMBOLS 9 #define ROWS 3 @@ -15,5 +16,6 @@ void randomizeSymbols(char symbols[]); int spin(char symbols[], int bet, int balance); int getWinnings(char symbols[], int bet); void showResult(char symbols[], int winnings); +bool checkForMaxWin(char symbols[]); #endif // SLOT_MACHINE_H \ No newline at end of file diff --git a/src/test/c/Stefan/test_slot_machine.c b/src/test/c/Stefan/test_slot_machine.c index ff44d97..1fd8b1d 100644 --- a/src/test/c/Stefan/test_slot_machine.c +++ b/src/test/c/Stefan/test_slot_machine.c @@ -195,12 +195,12 @@ void test_3_C_3_D_won_bet_equals_10() { TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } -void test_9_A_won_bet_equals_10() { +void test_9_A_max_win_bet_equals_10() { //arrange char testSymbols[] = {"AAAAAAAAA"}; - int expectedResult = 90; + int expectedResult = 900; //act int actualResult = getWinnings(testSymbols, 10); From 81e7376303dd39cd1fafc4c96b71dfafc85c71a7 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Wed, 7 Feb 2024 11:28:29 +0100 Subject: [PATCH 39/46] weitere Tests zu Gewinnberechnung bei max win --- src/test/c/Stefan/test_slot_machine.c | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/test/c/Stefan/test_slot_machine.c b/src/test/c/Stefan/test_slot_machine.c index 1fd8b1d..b3e2e94 100644 --- a/src/test/c/Stefan/test_slot_machine.c +++ b/src/test/c/Stefan/test_slot_machine.c @@ -209,6 +209,48 @@ void test_9_A_max_win_bet_equals_10() { TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } +void test_9_B_max_win_bet_equals_10() { + + //arrange + char testSymbols[] = {"BBBBBBBBB"}; + + int expectedResult = 1500; + + //act + int actualResult = getWinnings(testSymbols, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_9_C_max_win_bet_equals_10() { + + //arrange + char testSymbols[] = {"CCCCCCCCC"}; + + int expectedResult = 3000; + + //act + int actualResult = getWinnings(testSymbols, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_9_D_max_win_bet_equals_10() { + + //arrange + char testSymbols[] = {"DDDDDDDDD"}; + + int expectedResult = 6000; + + //act + int actualResult = getWinnings(testSymbols, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + void test_3_B_3_C_3_D_won_bet_equals_10() { //arrange From 930ecb25a8ec887e29f50dc4a17e8741b0fa4498 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Wed, 7 Feb 2024 11:37:20 +0100 Subject: [PATCH 40/46] Test, bei Gleichstand verliert Spieler bei Blackjack --- src/test/c/Stefan/test_blackjack.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/test/c/Stefan/test_blackjack.c b/src/test/c/Stefan/test_blackjack.c index 6a9dcd6..6974abd 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -228,6 +228,20 @@ void test_calculate_if_you_won_20_vs_21() { } +void test_calculate_if_you_won_21_vs_21() { + + //arrange + int userCardsTotal = 21; + int dealerCardsTotal = 21; + + //act + bool result = calculateIfYouWon(userCardsTotal, dealerCardsTotal); + + //assert + TEST_ASSERT_FALSE(result); + +} + void test_generate_char_for_card_2() { //arrange From da998963a0358834093799e7e64d9cf1610b9647 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Wed, 7 Feb 2024 11:46:57 +0100 Subject: [PATCH 41/46] refactoring: alle Kosolenausgaben jetzt auf deutsch --- src/main/c/Stefan/blackjack.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index c22ae30..33d96cc 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -102,7 +102,7 @@ int playBJ(int bet, int balance){ if(dealerCardsArray){ // Erste Karte vom Dealer ziehen und anzeigen dealerCardsArray[0] = getRandCard(0); - printf("\nDealer's first card: \n"); + printf("\nDealers erste Karte: \n"); generateCardAscii(dealerCardsArray[0]); // Array mit max 10 Karten anlegen @@ -110,7 +110,7 @@ int playBJ(int bet, int balance){ if(userCardsArray){ // Ersten 2 Karten vom Spieler ziehen und anzeigen - printf("\nYour first two cards: \n"); + printf("\ndsDeine Karten: \n"); userCardsArray[0] = getRandCard(1); generateCardAscii(userCardsArray[0]); userCardsArray[1] = getRandCard(2); @@ -128,11 +128,11 @@ int playBJ(int bet, int balance){ // Gesamtwert vom Spieler berechnen und anzeigen userCardsTotal = calculateCardsTotal(userCardsArray, i); - printf("Your Cards Total: %d\n\n", userCardsTotal); + printf("Deine Summe: %d\n\n", userCardsTotal); // Gesamtwert > 21 -> Verloren if(userCardsTotal > 21){ - printf("YOU BUST\n\n"); + printf("BUST!\n\n"); // Spieler und Dealer Karten zurücksetzen free(userCardsArray); @@ -144,7 +144,7 @@ int playBJ(int bet, int balance){ if(hit()){ // Wenn hit, neue Karte ziehen und anzeigen userCardsArray[i] = getRandCard(i); - printf("Your new card: %d\n", userCardsArray[i]); + printf("Deine neue Karte: %d\n", userCardsArray[i]); generateCardAscii(userCardsArray[i]); } else{ // Wenn stand, Schleife abbrechen, nicht mehr ziehen @@ -160,11 +160,11 @@ int playBJ(int bet, int balance){ // Wenn Dealer total < 17, muss er noch eine Karte ziehen if(dealerCardsTotal < 17){ dealerCardsArray[i] = getRandCard(i); - printf("Dealer Card: %d\n", dealerCardsArray[i]); + printf("Dealers neue Karte: \n"); generateCardAscii(dealerCardsArray[i]); dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i + 1); } - printf("Dealer Cards Total: %d\n", dealerCardsTotal); + printf("Dealers Summe: %d\n", dealerCardsTotal); // Wenn Dealer total > 17 und <= 21, dann keine Karte mehr ziehen if(dealerCardsTotal >= 17 && dealerCardsTotal <= 21){ @@ -205,15 +205,14 @@ bool checkForBlackjack(int userCardArray[]){ } bool hit(){ - printf("Hit (1) or stand (2): "); + printf("Hit (1) oder stand (2): "); int answer = userInputBJ(); if(answer != 1){ - printf("You stand.\n\n"); - printf("-----------------------------\n"); + printf("\n-----------------------------\n"); return false; } - printf("You hit.\n\n"); + printf("\n"); return true; } From 2c2de61d070cb0023e18a09136d5e865a2854cca Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Wed, 7 Feb 2024 11:47:32 +0100 Subject: [PATCH 42/46] refactoring: fixed typo --- src/main/c/Stefan/blackjack.c | 2 +- src/test/c/Stefan/test_blackjack.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 33d96cc..74936f9 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -110,7 +110,7 @@ int playBJ(int bet, int balance){ if(userCardsArray){ // Ersten 2 Karten vom Spieler ziehen und anzeigen - printf("\ndsDeine Karten: \n"); + printf("\nDeine Karten: \n"); userCardsArray[0] = getRandCard(1); generateCardAscii(userCardsArray[0]); userCardsArray[1] = getRandCard(2); diff --git a/src/test/c/Stefan/test_blackjack.c b/src/test/c/Stefan/test_blackjack.c index 6974abd..731db6e 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -240,7 +240,7 @@ void test_calculate_if_you_won_21_vs_21() { //assert TEST_ASSERT_FALSE(result); -} +} void test_generate_char_for_card_2() { From ebe0a2dc501eb059eff2093dc33af848553fdd23 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Wed, 7 Feb 2024 11:53:58 +0100 Subject: [PATCH 43/46] refactoring: Konsolenausgaben besser formuliert --- src/main/c/Stefan/blackjack.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 74936f9..96687c6 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -102,7 +102,7 @@ int playBJ(int bet, int balance){ if(dealerCardsArray){ // Erste Karte vom Dealer ziehen und anzeigen dealerCardsArray[0] = getRandCard(0); - printf("\nDealers erste Karte: \n"); + printf("\nDealers Karte: \n"); generateCardAscii(dealerCardsArray[0]); // Array mit max 10 Karten anlegen @@ -144,7 +144,7 @@ int playBJ(int bet, int balance){ if(hit()){ // Wenn hit, neue Karte ziehen und anzeigen userCardsArray[i] = getRandCard(i); - printf("Deine neue Karte: %d\n", userCardsArray[i]); + printf("Du ziehst die Karte: %d\n", userCardsArray[i]); generateCardAscii(userCardsArray[i]); } else{ // Wenn stand, Schleife abbrechen, nicht mehr ziehen @@ -160,7 +160,7 @@ int playBJ(int bet, int balance){ // Wenn Dealer total < 17, muss er noch eine Karte ziehen if(dealerCardsTotal < 17){ dealerCardsArray[i] = getRandCard(i); - printf("Dealers neue Karte: \n"); + printf("Dealer zieht die Karte: \n"); generateCardAscii(dealerCardsArray[i]); dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i + 1); } From fced68bd07fb41d431a6f6ef9115d816b1fa660f Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Wed, 7 Feb 2024 12:03:14 +0100 Subject: [PATCH 44/46] Test fuer generiertes ASCII art der Karte 10/J/Q/K --- src/test/c/Stefan/test_blackjack.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/test/c/Stefan/test_blackjack.c b/src/test/c/Stefan/test_blackjack.c index 731db6e..a106eed 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -1,5 +1,6 @@ #include "blackjack.h" #include "unity.h" +#include void setUp() {} @@ -298,6 +299,26 @@ void test_generate_char_for_card_5() { } +void test_generate_char_for_card_10() { + + //arrange + int cardValue = 10; + + //act + bool result = false; + switch(generateCardAscii(cardValue)){ + case('1'): result = true; break; + case('J'): result = true; break; + case('Q'): result = true; break; + case('K'): result = true; break; + default: result = false; break; + } + + //assert + TEST_ASSERT_TRUE(result); + +} + void test_subtract_bet_10_from_balance_100() { //arrange From d07abc5eeaebdad83e4f88af65825902e805e27e Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Wed, 7 Feb 2024 12:05:38 +0100 Subject: [PATCH 45/46] refactoring: Funktion checkForMaxWin() vereinfacht --- src/main/c/Stefan/slot_machine.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index d959827..3819894 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -134,14 +134,12 @@ int getWinnings(char symbols[], int bet){ } bool checkForMaxWin(char symbols[]){ - bool maxWin = true; for(int i = 1; i < NUM_OF_SYMBOLS; i++){ if(symbols[0] != symbols[i]){ - maxWin = false; - return maxWin; + return false; } } - return maxWin; + return true; } void showResult(char symbols[], int winnings){ From d418ea5135094b38345c3afc4cbbb82881fc65ba Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Wed, 7 Feb 2024 13:53:55 +0100 Subject: [PATCH 46/46] Test, falls Wetteinsatz > guthaben ist --- src/main/c/Stefan/blackjack.c | 3 +++ src/test/c/Stefan/test_blackjack.c | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c index 96687c6..397eea0 100644 --- a/src/main/c/Stefan/blackjack.c +++ b/src/main/c/Stefan/blackjack.c @@ -32,6 +32,9 @@ int blackjack(){ } int subtractBetFromBalanceBJ(int bet, int balance){ + if(balance - bet < 0){ + return 0; + } return balance - bet; } diff --git a/src/test/c/Stefan/test_blackjack.c b/src/test/c/Stefan/test_blackjack.c index a106eed..26f0412 100644 --- a/src/test/c/Stefan/test_blackjack.c +++ b/src/test/c/Stefan/test_blackjack.c @@ -362,4 +362,19 @@ void test_subtract_bet_1_from_balance_100000() { //assert TEST_ASSERT_EQUAL_INT(expected_result, actual_result); +} + +void test_subtract_bet_10_from_balance_1() { + + //arrange + int bet = 10; + int balance = 1; + int expected_result = 0; + + //act + int actual_result = subtractBetFromBalanceBJ(bet, balance); + + //assert + TEST_ASSERT_EQUAL_INT(expected_result, actual_result); + } \ No newline at end of file