fdai7472
11 months ago
7 changed files with 800 additions and 5 deletions
-
298src/main/c/Stefan/blackjack.c
-
24src/main/c/Stefan/blackjack.h
-
17src/main/c/Stefan/slot_machine.c
-
2src/main/c/Stefan/slot_machine.h
-
10src/main/c/main.c
-
380src/test/c/Stefan/test_blackjack.c
-
74src/test/c/Stefan/test_slot_machine.c
@ -0,0 +1,298 @@ |
|||
#include "blackjack.h" |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <stdbool.h> |
|||
#include <time.h> |
|||
|
|||
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; |
|||
} |
@ -0,0 +1,24 @@ |
|||
#ifndef BLACKJACK_H |
|||
#define BLACKJACK_H |
|||
|
|||
#include <stdbool.h> |
|||
|
|||
#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 |
@ -0,0 +1,380 @@ |
|||
#include "blackjack.h" |
|||
#include "unity.h" |
|||
#include <stdbool.h> |
|||
|
|||
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); |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue