From 7e078d20c6127f56145c003ba99fb23c1edf162c Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 5 Feb 2024 20:17:37 +0100 Subject: [PATCH] =?UTF-8?q?zufaellig=20generierte=20Karten=20sind=20jetzt?= =?UTF-8?q?=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