From 4b24734142dd37e12a0de794137e8745e4b52533 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 17:42:34 +0100 Subject: [PATCH] Struct symbols durch einfaches String ersetzt und Code dementsprechend angepasst --- src/main/c/Stefan/slot_machine.c | 39 ++++++++++++++++++-------------- src/main/c/Stefan/slot_machine.h | 10 +++----- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index 1bb050e..2a9b689 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -7,7 +7,7 @@ void slotMachine(){ welcomeMessage(); - Symbol symbols[NUM_OF_SYMBOLS]; + char symbols[NUM_OF_SYMBOLS]; int balance = getBalance(); @@ -15,6 +15,8 @@ void slotMachine(){ int bet = getBet(balance); balance = subtractBetFromBalance(bet, balance); + printf("Guthaben: %d\n", balance); + randomizeSymbols(symbols); spin(symbols, bet, balance); @@ -47,17 +49,16 @@ int getBet(int balance){ } else { printf("Ungueltige Eingabe!\n"); } + } + } int subtractBetFromBalance(int bet, int balance){ return balance - bet; } -void randomizeSymbols(Symbol symbols[]){ - - char possibleLetter[] = "ABCD"; - int possibleValue[] = { 3,5,10,20 }; +void randomizeSymbols(char symbols[]){ srand(time(NULL)); @@ -67,36 +68,32 @@ void randomizeSymbols(Symbol symbols[]){ // 35% chance für A if (r < 35) { - symbols[i].letter = possibleLetter[0]; - symbols[i].value = possibleValue[0]; + symbols[i] = 'A'; } // 30% chance für B else if (r < 65) { - symbols[i].letter = possibleLetter[1]; - symbols[i].value = possibleValue[1]; + symbols[i] = 'B'; } // 20% chance für C else if (r < 85) { - symbols[i].letter = possibleLetter[2]; - symbols[i].value = possibleValue[2]; + symbols[i] = 'C'; } // 15% chance für D else { - symbols[i].letter = possibleLetter[3]; - symbols[i].value = possibleValue[3]; + symbols[i] = 'D'; } } } -int spin(Symbol symbols[], int bet, int balance){ +int spin(char symbols[], int bet, int balance){ int winnings = getWinnings(symbols, bet); return balance; } -int getWinnings(Symbol symbols[], int bet){ +int getWinnings(char symbols[], int bet){ int winnings = 0; @@ -106,8 +103,16 @@ int getWinnings(Symbol symbols[], int bet){ // 3 gleiche Symbole in einer Reihe gewinnen, Gewinn berechnen for (int i = 0; i < ROWS; i++){ - if (symbols[i * ROWS].letter == symbols[i * ROWS + 1].letter && symbols[i * ROWS].letter == symbols[i * ROWS + 2].letter) { - winnings += bet * symbols[i * ROWS].value; + + if (symbols[i * ROWS] == symbols[i * ROWS + 1] && symbols[i * ROWS] == symbols[i * ROWS + 2]) { + + switch(symbols[i]){ + case 'A': winnings = bet * 3; break; + case 'B': winnings = bet * 5; break; + case 'C': winnings = bet * 10; break; + case 'D': winnings = bet * 20; break; + } + } } diff --git a/src/main/c/Stefan/slot_machine.h b/src/main/c/Stefan/slot_machine.h index 1ed2f00..dc37dae 100644 --- a/src/main/c/Stefan/slot_machine.h +++ b/src/main/c/Stefan/slot_machine.h @@ -1,10 +1,6 @@ #ifndef SLOT_MACHINE_H #define SLOT_MACHINE_H -typedef struct { - char letter; - int value; -} Symbol; #define NUM_OF_SYMBOLS 9 #define ROWS 9 @@ -15,8 +11,8 @@ int getBalance(); int userInput(); int getBet(int balance); int subtractBetFromBalance(int bet, int balance); -void randomizeSymbols(Symbol symbols[]); -int spin(Symbol symbols[], int bet, int balance); -int getWinnings(Symbol symbols[], int bet); +void randomizeSymbols(char symbols[]); +int spin(char symbols[], int bet, int balance); +int getWinnings(char symbols[], int bet); #endif // SLOT_MACHINE_H \ No newline at end of file