diff --git a/src/main/c/Stefan/blackjack.c b/src/main/c/Stefan/blackjack.c new file mode 100644 index 0000000..397eea0 --- /dev/null +++ b/src/main/c/Stefan/blackjack.c @@ -0,0 +1,298 @@ +#include "blackjack.h" +#include +#include +#include +#include + +int blackjack(){ + + welcomeMessageBJ(); + + bool exit = false; + bool *exitPtr = &exit; + int balance = getBalanceBJ(); + + while(balance > 0){ + printf("Aktuelles Guthaben: %d\n", balance); + printf("-----------------------------\n"); + 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 = subtractBetFromBalanceBJ(bet, balance); + + // Nach jeder Runde wird das Guthaben aktualisiert + balance = playBJ(bet, balance); + } + + return 0; +} + +int subtractBetFromBalanceBJ(int bet, int balance){ + if(balance - bet < 0){ + return 0; + } + return balance - bet; +} + +int getBalanceBJ(){ + printf("Guthaben einwerfen: "); + int balance = userInputBJ(); + return 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; + } + // Blackjack verlassen, wenn die Eingabe 0 ist + else if(bet == 0) { + *exitPtr = true; + break; + } else { + printf("Ungueltige Eingabe!\n"); + } + } +} + +int userInputBJ(){ + int input; + 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(int offset){ + int randomCard; + srand(time(NULL)); + + 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){ + return 10; + } + // Ass (1) hat erstmal den Wert 11 + else if(randomCard == 1){ + return 11; + } + + return randomCard; +} + + +int playBJ(int bet, int balance){ + int winnings = 0; + int userCardsTotal = 0, dealerCardsTotal = 0; + + // Array mit max 10 Karten anlegen + int *dealerCardsArray = (int*) malloc(MAX_CARDS * sizeof(int)); + + if(dealerCardsArray){ + // Erste Karte vom Dealer ziehen und anzeigen + dealerCardsArray[0] = getRandCard(0); + printf("\nDealers Karte: \n"); + generateCardAscii(dealerCardsArray[0]); + + // Array mit max 10 Karten anlegen + int *userCardsArray = (int*) malloc(MAX_CARDS * sizeof(int)); + + if(userCardsArray){ + // Ersten 2 Karten vom Spieler ziehen und anzeigen + printf("\nDeine Karten: \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("Deine Summe: %d\n\n", userCardsTotal); + + // Gesamtwert > 21 -> Verloren + if(userCardsTotal > 21){ + printf("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("Du ziehst die Karte: %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); + + // Wenn Dealer total < 17, muss er noch eine Karte ziehen + if(dealerCardsTotal < 17){ + dealerCardsArray[i] = getRandCard(i); + printf("Dealer zieht die Karte: \n"); + generateCardAscii(dealerCardsArray[i]); + dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i + 1); + } + printf("Dealers Summe: %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"); + return true; + } + return false; +} + +bool hit(){ + printf("Hit (1) oder stand (2): "); + int answer = userInputBJ(); + + if(answer != 1){ + printf("\n-----------------------------\n"); + return false; + } + printf("\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 total = 0; + int acesFound = 0; + for(int i = 0; i < len; i++){ + if(cardsArray[i] == 11){ + acesFound++; + } + // Wenn der total > 21, aber man hat ein Ass, dann zählt hat das Ass den Wert 1, nicht 11 + if(acesFound > 0 && total + cardsArray[i] > 21){ + total -= 10; + // Das gleiche Ass kann nicht mehrmals den total um 10 senken + acesFound--; + } + total += cardsArray[i]; + } + return total; +} + +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; +} + +void welcomeMessageBJ(){ + + printf("Willkommen zu..\n\n" + " _ _ _ _ _ \n" + "| | | | | | (_) | | \n" + "| |__ | | __ _ ___| | ___ __ _ ___| | __\n" + "| '_ \\| |/ _` |/ __| |/ / |/ _` |/ __| |/ /\n" + "| |_) | | (_| | (__| <| | (_| | (__| < \n" + "|_.__/|_|\\__,_|\\___|_|\\_\\ |\\__,_|\\___|_|\\_\\\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(3): 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 new file mode 100644 index 0000000..bb70cd7 --- /dev/null +++ b/src/main/c/Stefan/blackjack.h @@ -0,0 +1,24 @@ +#ifndef BLACKJACK_H +#define BLACKJACK_H + +#include + +#define ANZ_MOEGLICHE_KARTEN 13 +#define MAX_CARDS 10 + +int blackjack(); +void welcomeMessageBJ(); +int getBalanceBJ(); +int getBetBJ(int balance, bool *exitPtr); +int userInputBJ(); +int getRandCard(int offset); +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); +char generateCardAscii(int cardValue); +int subtractBetFromBalanceBJ(int bet, int balance); + +#endif // BLACKJACK_H \ No newline at end of file diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index f659eab..3819894 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,9 +133,18 @@ int getWinnings(char symbols[], int bet){ return winnings; } +bool checkForMaxWin(char symbols[]){ + for(int i = 1; i < NUM_OF_SYMBOLS; i++){ + if(symbols[0] != symbols[i]){ + return false; + } + } + return true; +} + void showResult(char symbols[], int winnings){ - char winnerMessage[] = {""}; + char winnerMessage[10] = {""}; if(winnings > 0){ strcpy(winnerMessage, " WINNER "); 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/main/c/main.c b/src/main/c/main.c index bda2276..4889a03 100644 --- a/src/main/c/main.c +++ b/src/main/c/main.c @@ -14,8 +14,10 @@ #include "SchereSteinPapier.h" #include "hangman.h" #include "tictactoe.h" +#include "slot_machine.h" +#include "blackjack.h" #include "Jason/ASCII_art.h" -#include "Stefan/slot_machine.h" + void openInterface(); @@ -39,7 +41,8 @@ void openInterface() "2: Hangman\n" "3: TicTacToe\n" "4: Slot Machine\n" - "5: ASCII - Art\n"); + "5: ASCII - Art\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"); #ifdef __APPLE__ scanf("%d", &selection); @@ -72,6 +75,9 @@ void openInterface() case(5): ASCII_Art(); break; + case(6): + blackjack(); + break; default: break; } diff --git a/src/test/c/Stefan/test_blackjack.c b/src/test/c/Stefan/test_blackjack.c new file mode 100644 index 0000000..26f0412 --- /dev/null +++ b/src/test/c/Stefan/test_blackjack.c @@ -0,0 +1,380 @@ +#include "blackjack.h" +#include "unity.h" +#include + +void setUp() {} + +void tearDown() {} + + +void test_blackjack() { + + //arrange + int userCards[] = {10, 11}; + + //act + bool result = checkForBlackjack(userCards); + + //assert + TEST_ASSERT_TRUE(result); + +} + +void test_no_blackjack() { + + //arrange + int userCards[] = {10, 10}; + + //act + bool result = checkForBlackjack(userCards); + + //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); + +} + +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); + +} + +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_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_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_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 + 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); + +} + +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); + +} + +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 + int cardValue = 2; + char expected_result = '2'; + + //act + char actual_result = generateCardAscii(cardValue); + + //assert + 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); + +} + +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); + +} + +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); + +} + +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 + 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); + +} + +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); + +} + +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 diff --git a/src/test/c/Stefan/test_slot_machine.c b/src/test/c/Stefan/test_slot_machine.c index f2a17c9..b3e2e94 100644 --- a/src/test/c/Stefan/test_slot_machine.c +++ b/src/test/c/Stefan/test_slot_machine.c @@ -195,12 +195,54 @@ 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); + + //assert + 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); @@ -219,6 +261,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