From 78f23e4622eec07f204b8764d78912fc74943a68 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Thu, 25 Jan 2024 22:22:10 +0100 Subject: [PATCH 01/25] Anlegen der Dateien slot_machine.c, slot_machine.h, test_slot_machine.c --- src/main/c/Stefan/place_your_code_here.txt | 0 src/main/c/Stefan/slot_machine.c | 3 +++ src/main/c/Stefan/slot_machine.h | 6 ++++++ src/test/c/Stefan/place_your_tests_here.txt | 0 src/test/c/Stefan/test_slot_machine.c | 8 ++++++++ 5 files changed, 17 insertions(+) delete mode 100644 src/main/c/Stefan/place_your_code_here.txt create mode 100644 src/main/c/Stefan/slot_machine.c create mode 100644 src/main/c/Stefan/slot_machine.h delete mode 100644 src/test/c/Stefan/place_your_tests_here.txt create mode 100644 src/test/c/Stefan/test_slot_machine.c diff --git a/src/main/c/Stefan/place_your_code_here.txt b/src/main/c/Stefan/place_your_code_here.txt deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c new file mode 100644 index 0000000..0489155 --- /dev/null +++ b/src/main/c/Stefan/slot_machine.c @@ -0,0 +1,3 @@ +#include "slot_machine.h" + +void slotMachine(){} \ No newline at end of file diff --git a/src/main/c/Stefan/slot_machine.h b/src/main/c/Stefan/slot_machine.h new file mode 100644 index 0000000..802a09f --- /dev/null +++ b/src/main/c/Stefan/slot_machine.h @@ -0,0 +1,6 @@ +#ifndef SLOT_MACHINE_H +#define SLOT_MACHINE_H + +void slotMachine(); + +#endif // SLOT_MACHINE_H \ No newline at end of file diff --git a/src/test/c/Stefan/place_your_tests_here.txt b/src/test/c/Stefan/place_your_tests_here.txt deleted file mode 100644 index e69de29..0000000 diff --git a/src/test/c/Stefan/test_slot_machine.c b/src/test/c/Stefan/test_slot_machine.c new file mode 100644 index 0000000..b327568 --- /dev/null +++ b/src/test/c/Stefan/test_slot_machine.c @@ -0,0 +1,8 @@ +#include "slot_machine.h" +#include "unity.h" + +void setUp() {} + +void tearDown() {} + +void test_test(){} \ No newline at end of file From a2b67996497e3992efc3575abb430c774457614d Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 09:44:29 +0100 Subject: [PATCH 02/25] Slot Machine im main included und im switch angefuegt --- src/main/c/main.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/c/main.c b/src/main/c/main.c index 4dc37a9..49f4cdd 100644 --- a/src/main/c/main.c +++ b/src/main/c/main.c @@ -14,6 +14,7 @@ #include "SchereSteinPapier.h" #include "hangman.h" #include "tictactoe.h" +#include "slot_machine.h" void openInterface(); @@ -35,7 +36,8 @@ void openInterface() "Du hast folgende Spiele zur Auswahl:\n\n" "1: Schere-Stein-Papier\n" "2: Hangman\n" - "3: TicTacToe\n"); + "3: TicTacToe\n" + "4: Slot Machine\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); @@ -54,10 +56,10 @@ void openInterface() case(3): startTicTacToe(); break; - /*case(4): - //Spiel() + case(4): + slotMachine(); break; - case(5): + /*case(5): //Spiel() break;*/ default: From 7aebe2a9dc0fb96365149fdde3b095e7bd66eb51 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 15:43:49 +0100 Subject: [PATCH 03/25] Welcome Message added --- src/main/c/Stefan/slot_machine.c | 23 ++++++++++++++++++++++- src/main/c/Stefan/slot_machine.h | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index 0489155..b5ad747 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -1,3 +1,24 @@ #include "slot_machine.h" +#include -void slotMachine(){} \ No newline at end of file +void slotMachine(){ + + welcomeMessage(); + + + + + + + +} + +void welcomeMessage(){ + printf("Herzlich Willkommen zur \n\n" + " _ _ _ _ \n" + " | | | | | | (_) \n" + " ___| | ___ | |_ _ __ ___ __ _ ___| |__ _ _ __ ___ \n" + "/ __| |/ _ \\| __| | '_ ` _ \\ / _` |/ __| '_ \\| | '_ \\ / _ \\\n" + "\\__ \\ | (_) | |_ | | | | | (_| | (__| | | | | | | | __/\n" + "|___/_|\\___/ \\__| |_| |_| |_|\\__,_|\\___|_| |_|_|_| |_|\\___|\n\n"); +} \ No newline at end of file diff --git a/src/main/c/Stefan/slot_machine.h b/src/main/c/Stefan/slot_machine.h index 802a09f..588b6ea 100644 --- a/src/main/c/Stefan/slot_machine.h +++ b/src/main/c/Stefan/slot_machine.h @@ -2,5 +2,6 @@ #define SLOT_MACHINE_H void slotMachine(); +void welcomeMessage(); #endif // SLOT_MACHINE_H \ No newline at end of file From 8b185a3cdcc14b64f25a7577db3d993bd2acd63c Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 15:48:06 +0100 Subject: [PATCH 04/25] getBalance() added --- src/main/c/Stefan/slot_machine.c | 8 ++++++++ src/main/c/Stefan/slot_machine.h | 2 ++ 2 files changed, 10 insertions(+) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index b5ad747..f0ee169 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -5,12 +5,20 @@ void slotMachine(){ welcomeMessage(); + int balance = getBalance(); +} + +int getBalance(){ + int balance; + printf("Guthaben einwerfen: "); + scanf_s("%d", &balance); + return balance; } void welcomeMessage(){ diff --git a/src/main/c/Stefan/slot_machine.h b/src/main/c/Stefan/slot_machine.h index 588b6ea..3da8ff8 100644 --- a/src/main/c/Stefan/slot_machine.h +++ b/src/main/c/Stefan/slot_machine.h @@ -3,5 +3,7 @@ void slotMachine(); void welcomeMessage(); +int getBalance(); + #endif // SLOT_MACHINE_H \ No newline at end of file From d3880981729b54656e3bd33dda9b2696515221e4 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 15:53:07 +0100 Subject: [PATCH 05/25] getBalance erweitert mit userInput --- src/main/c/Stefan/slot_machine.c | 9 ++++++++- src/main/c/Stefan/slot_machine.h | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index f0ee169..ff07038 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -17,10 +17,17 @@ void slotMachine(){ int getBalance(){ int balance; printf("Guthaben einwerfen: "); - scanf_s("%d", &balance); + balance = userInput(); + printf("Dein Guthaben: %d", balance); return balance; } +int userInput(){ + int input; + scanf_s("%d", &input); + return input; +} + void welcomeMessage(){ printf("Herzlich Willkommen zur \n\n" " _ _ _ _ \n" diff --git a/src/main/c/Stefan/slot_machine.h b/src/main/c/Stefan/slot_machine.h index 3da8ff8..56cebac 100644 --- a/src/main/c/Stefan/slot_machine.h +++ b/src/main/c/Stefan/slot_machine.h @@ -4,6 +4,6 @@ void slotMachine(); void welcomeMessage(); int getBalance(); - +int userInput(); #endif // SLOT_MACHINE_H \ No newline at end of file From f87ef43a2c84b147cc0b586f74381f95020f92c6 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 16:03:07 +0100 Subject: [PATCH 06/25] Eingabe Wetteinsatz added --- src/main/c/Stefan/slot_machine.c | 15 ++++++++++++++- src/main/c/Stefan/slot_machine.h | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index ff07038..dfddd13 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -7,7 +7,12 @@ void slotMachine(){ int balance = getBalance(); + while(balance > 0){ + int bet = getBet(balance); + balance -= bet; + printf("%d\n", balance); + } @@ -18,7 +23,7 @@ int getBalance(){ int balance; printf("Guthaben einwerfen: "); balance = userInput(); - printf("Dein Guthaben: %d", balance); + printf("Dein Guthaben: %d\n", balance); return balance; } @@ -28,6 +33,14 @@ int userInput(){ return input; } +int getBet(int balance){ + int bet; + printf("Wetteinsatz: "); + bet = userInput(); + + return bet; +} + void welcomeMessage(){ printf("Herzlich Willkommen zur \n\n" " _ _ _ _ \n" diff --git a/src/main/c/Stefan/slot_machine.h b/src/main/c/Stefan/slot_machine.h index 56cebac..14a3217 100644 --- a/src/main/c/Stefan/slot_machine.h +++ b/src/main/c/Stefan/slot_machine.h @@ -5,5 +5,6 @@ void slotMachine(); void welcomeMessage(); int getBalance(); int userInput(); +int getBet(int balance); #endif // SLOT_MACHINE_H \ No newline at end of file From 6b9011afcb5740a2e9edd834f877e5bd62ad9c83 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 16:09:03 +0100 Subject: [PATCH 07/25] Ungueltige Wetteinsaetze werden abgefangen --- src/main/c/Stefan/slot_machine.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index dfddd13..4d1e33f 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -34,11 +34,17 @@ int userInput(){ } int getBet(int balance){ - int bet; - printf("Wetteinsatz: "); - bet = userInput(); - return bet; + while(1){ + printf("Wetteinsatz: "); + int bet = userInput(); + + if(bet <= balance && bet >= 0){ + return bet; + } else { + printf("Ungueltige Eingabe!\n"); + } + } } void welcomeMessage(){ From d15d3aa55bcc743dc31688beffbd3aa87518a84b Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 16:17:02 +0100 Subject: [PATCH 08/25] Wetteinsatz vom Guthaben abziehen + erster Test dazu --- src/main/c/Stefan/slot_machine.c | 7 +++++-- src/main/c/Stefan/slot_machine.h | 1 + src/test/c/Stefan/test_slot_machine.c | 12 +++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index 4d1e33f..ef586f5 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -10,8 +10,7 @@ void slotMachine(){ while(balance > 0){ int bet = getBet(balance); - balance -= bet; - printf("%d\n", balance); + balance = subtractBetFromBalance(bet, balance); } @@ -47,6 +46,10 @@ int getBet(int balance){ } } +int subtractBetFromBalance(int bet, int balance){ + return balance - bet; +} + void welcomeMessage(){ printf("Herzlich Willkommen zur \n\n" " _ _ _ _ \n" diff --git a/src/main/c/Stefan/slot_machine.h b/src/main/c/Stefan/slot_machine.h index 14a3217..b0119f4 100644 --- a/src/main/c/Stefan/slot_machine.h +++ b/src/main/c/Stefan/slot_machine.h @@ -6,5 +6,6 @@ void welcomeMessage(); int getBalance(); int userInput(); int getBet(int balance); +int subtractBetFromBalance(int bet, int balance); #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 b327568..4afa387 100644 --- a/src/test/c/Stefan/test_slot_machine.c +++ b/src/test/c/Stefan/test_slot_machine.c @@ -5,4 +5,14 @@ void setUp() {} void tearDown() {} -void test_test(){} \ No newline at end of file +void test_subtract_bet_10_from_balance_10() { + + //arrange + int expectedResult = 0; + + //act + int actualResult = subtractBetFromBalance(10, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} \ No newline at end of file From a8c5b73431720cac5a4a7a5479c5441345f13b99 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 16:19:46 +0100 Subject: [PATCH 09/25] weitere Tests Wetteinsatz vom Guthaben abziehen --- src/test/c/Stefan/test_slot_machine.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/test/c/Stefan/test_slot_machine.c b/src/test/c/Stefan/test_slot_machine.c index 4afa387..9781e62 100644 --- a/src/test/c/Stefan/test_slot_machine.c +++ b/src/test/c/Stefan/test_slot_machine.c @@ -13,6 +13,29 @@ void test_subtract_bet_10_from_balance_10() { //act int actualResult = subtractBetFromBalance(10, 10); + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} +void test_subtract_bet_0_from_balance_10() { + + //arrange + int expectedResult = 10; + + //act + int actualResult = subtractBetFromBalance(0, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_subtract_bet_5_from_balance_10() { + + //arrange + int expectedResult = 5; + + //act + int actualResult = subtractBetFromBalance(5, 10); + //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } \ No newline at end of file From e87430bfc9454b1768b5dff4853dc9298dfef036 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 16:28:04 +0100 Subject: [PATCH 10/25] Symbole (A,B,C,D) mit bestimmten Wahrscheinlichkeiten und Werten erstellen --- src/main/c/Stefan/slot_machine.c | 44 +++++++++++++++++++++++++++++--- src/main/c/Stefan/slot_machine.h | 8 ++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index ef586f5..a554065 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -1,20 +1,23 @@ #include "slot_machine.h" #include +#include +#include void slotMachine(){ welcomeMessage(); + Symbol symbols[NUM_OF_SYMBOLS]; + int balance = getBalance(); while(balance > 0){ int bet = getBet(balance); balance = subtractBetFromBalance(bet, balance); - } - - + createSymbols(symbols); + } } @@ -50,6 +53,41 @@ int subtractBetFromBalance(int bet, int balance){ return balance - bet; } +void createSymbols(Symbol symbols[]){ + + char possibleLetter[] = "ABCD"; + int possibleValue[] = { 3,5,10,20 }; + + srand(time(NULL)); + + for (int i = 0; i < NUM_OF_SYMBOLS; i++) { + + int r = rand() % 100; + + // 35% chance für A + if (r < 35) { + symbols[i].letter = possibleLetter[0]; + symbols[i].value = possibleValue[0]; + } + // 30% chance für B + else if (r < 65) { + symbols[i].letter = possibleLetter[1]; + symbols[i].value = possibleValue[1]; + } + // 20% chance für C + else if (r < 85) { + symbols[i].letter = possibleLetter[2]; + symbols[i].value = possibleValue[2]; + } + // 15% chance für D + else { + symbols[i].letter = possibleLetter[3]; + symbols[i].value = possibleValue[3]; + } + } + +} + void welcomeMessage(){ printf("Herzlich Willkommen zur \n\n" " _ _ _ _ \n" diff --git a/src/main/c/Stefan/slot_machine.h b/src/main/c/Stefan/slot_machine.h index b0119f4..5cf2295 100644 --- a/src/main/c/Stefan/slot_machine.h +++ b/src/main/c/Stefan/slot_machine.h @@ -1,11 +1,19 @@ #ifndef SLOT_MACHINE_H #define SLOT_MACHINE_H +typedef struct { + char letter; + int value; +} Symbol; + +#define NUM_OF_SYMBOLS 9 + void slotMachine(); void welcomeMessage(); int getBalance(); int userInput(); int getBet(int balance); int subtractBetFromBalance(int bet, int balance); +void createSymbols(Symbol symbols[]); #endif // SLOT_MACHINE_H \ No newline at end of file From 5038d8194aed26fc86577f3422820c44e8a37a07 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 16:36:59 +0100 Subject: [PATCH 11/25] refactoring: Funktion createSymbols zu randomizeSymbols umbenannt --- src/main/c/Stefan/slot_machine.c | 4 ++-- src/main/c/Stefan/slot_machine.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index a554065..f57a38e 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -16,7 +16,7 @@ void slotMachine(){ int bet = getBet(balance); balance = subtractBetFromBalance(bet, balance); - createSymbols(symbols); + randomizeSymbols(symbols); } } @@ -53,7 +53,7 @@ int subtractBetFromBalance(int bet, int balance){ return balance - bet; } -void createSymbols(Symbol symbols[]){ +void randomizeSymbols(Symbol symbols[]){ char possibleLetter[] = "ABCD"; int possibleValue[] = { 3,5,10,20 }; diff --git a/src/main/c/Stefan/slot_machine.h b/src/main/c/Stefan/slot_machine.h index 5cf2295..bebd77b 100644 --- a/src/main/c/Stefan/slot_machine.h +++ b/src/main/c/Stefan/slot_machine.h @@ -14,6 +14,6 @@ int getBalance(); int userInput(); int getBet(int balance); int subtractBetFromBalance(int bet, int balance); -void createSymbols(Symbol symbols[]); +void randomizeSymbols(Symbol symbols[]); #endif // SLOT_MACHINE_H \ No newline at end of file From ac710a45782b67b65668d134def99acb54a96cd5 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 16:44:02 +0100 Subject: [PATCH 12/25] Symbole in einer Reihe vergleichen und Gewinn berechnen --- src/main/c/Stefan/slot_machine.c | 26 ++++++++++++++++++++++++++ src/main/c/Stefan/slot_machine.h | 3 +++ 2 files changed, 29 insertions(+) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index f57a38e..1bb050e 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -17,6 +17,7 @@ void slotMachine(){ balance = subtractBetFromBalance(bet, balance); randomizeSymbols(symbols); + spin(symbols, bet, balance); } } @@ -88,6 +89,31 @@ void randomizeSymbols(Symbol symbols[]){ } +int spin(Symbol symbols[], int bet, int balance){ + + int winnings = getWinnings(symbols, bet); + + return balance; +} + +int getWinnings(Symbol symbols[], int bet){ + + int winnings = 0; + + // 0 | 1 | 2 + // 3 | 4 | 5 + // 6 | 7 | 8 + + // 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; + } + } + + return winnings; +} + void welcomeMessage(){ printf("Herzlich Willkommen zur \n\n" " _ _ _ _ \n" diff --git a/src/main/c/Stefan/slot_machine.h b/src/main/c/Stefan/slot_machine.h index bebd77b..1ed2f00 100644 --- a/src/main/c/Stefan/slot_machine.h +++ b/src/main/c/Stefan/slot_machine.h @@ -7,6 +7,7 @@ typedef struct { } Symbol; #define NUM_OF_SYMBOLS 9 +#define ROWS 9 void slotMachine(); void welcomeMessage(); @@ -15,5 +16,7 @@ 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); #endif // SLOT_MACHINE_H \ No newline at end of file From 4b24734142dd37e12a0de794137e8745e4b52533 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 17:42:34 +0100 Subject: [PATCH 13/25] 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 From a34f5abef406274b84df7004b5e89f602bef4715 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 17:49:58 +0100 Subject: [PATCH 14/25] Ergebnis anzeigen implementiert + typo in .h korrigiert ROWS 9 -> ROWS 3 --- src/main/c/Stefan/slot_machine.c | 8 ++++++++ src/main/c/Stefan/slot_machine.h | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index 2a9b689..54ac3e9 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -89,6 +89,7 @@ void randomizeSymbols(char symbols[]){ int spin(char symbols[], int bet, int balance){ int winnings = getWinnings(symbols, bet); + showResult(symbols); return balance; } @@ -119,6 +120,13 @@ int getWinnings(char symbols[], int bet){ return winnings; } +void showResult(char symbols[]){ + + for (int i = 0; i < ROWS; i++){ + printf(" %c | %c | %c\n", symbols[i * ROWS], symbols[i * ROWS + 1], symbols[i * ROWS + 2]); + } +} + void welcomeMessage(){ printf("Herzlich Willkommen zur \n\n" " _ _ _ _ \n" diff --git a/src/main/c/Stefan/slot_machine.h b/src/main/c/Stefan/slot_machine.h index dc37dae..3639e7e 100644 --- a/src/main/c/Stefan/slot_machine.h +++ b/src/main/c/Stefan/slot_machine.h @@ -3,7 +3,7 @@ #define NUM_OF_SYMBOLS 9 -#define ROWS 9 +#define ROWS 3 void slotMachine(); void welcomeMessage(); @@ -14,5 +14,6 @@ int subtractBetFromBalance(int bet, int balance); void randomizeSymbols(char symbols[]); int spin(char symbols[], int bet, int balance); int getWinnings(char symbols[], int bet); +void showResult(char symbols[]); #endif // SLOT_MACHINE_H \ No newline at end of file From 607f656bed28be3b4450f0e54b77e5645533b769 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 17:54:00 +0100 Subject: [PATCH 15/25] Gewinne werden jetzt angezeigt --- src/main/c/Stefan/slot_machine.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index 54ac3e9..a779776 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -88,8 +88,8 @@ void randomizeSymbols(char symbols[]){ int spin(char symbols[], int bet, int balance){ - int winnings = getWinnings(symbols, bet); showResult(symbols); + int winnings = getWinnings(symbols, bet); return balance; } @@ -113,15 +113,20 @@ int getWinnings(char symbols[], int bet){ case 'C': winnings = bet * 10; break; case 'D': winnings = bet * 20; break; } - } } + if (winnings > 0) { + printf("\nDu hast %d gewonnen!\n", winnings - bet); + } + else { + printf("\nLeider verloren.\n"); + } return winnings; } void showResult(char symbols[]){ - + for (int i = 0; i < ROWS; i++){ printf(" %c | %c | %c\n", symbols[i * ROWS], symbols[i * ROWS + 1], symbols[i * ROWS + 2]); } From a0f27247c4ac765c43b06b382a070c130e919166 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 18:00:51 +0100 Subject: [PATCH 16/25] Guthaben wird nach jeder Runde angezeigt --- src/main/c/Stefan/slot_machine.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index a779776..114583d 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -15,8 +15,6 @@ void slotMachine(){ int bet = getBet(balance); balance = subtractBetFromBalance(bet, balance); - printf("Guthaben: %d\n", balance); - randomizeSymbols(symbols); spin(symbols, bet, balance); @@ -89,8 +87,10 @@ void randomizeSymbols(char symbols[]){ int spin(char symbols[], int bet, int balance){ showResult(symbols); - int winnings = getWinnings(symbols, bet); + int winnings = getWinnings(symbols, bet); + balance += winnings; + printf("Aktuelles Guthaben: %d\n-------------------------\n", balance); return balance; } From c8faaa8ba57710efbf577c08c25c6079e5c91ebf Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 18:09:06 +0100 Subject: [PATCH 17/25] Test, falls keine Reihe gewinnt --- src/main/c/Stefan/slot_machine.c | 2 +- src/test/c/Stefan/test_slot_machine.c | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index 114583d..5acc8bf 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -107,7 +107,7 @@ int getWinnings(char symbols[], int bet){ if (symbols[i * ROWS] == symbols[i * ROWS + 1] && symbols[i * ROWS] == symbols[i * ROWS + 2]) { - switch(symbols[i]){ + switch(symbols[i * ROWS]){ case 'A': winnings = bet * 3; break; case 'B': winnings = bet * 5; break; case 'C': winnings = bet * 10; break; diff --git a/src/test/c/Stefan/test_slot_machine.c b/src/test/c/Stefan/test_slot_machine.c index 9781e62..76f8258 100644 --- a/src/test/c/Stefan/test_slot_machine.c +++ b/src/test/c/Stefan/test_slot_machine.c @@ -38,4 +38,19 @@ void test_subtract_bet_5_from_balance_10() { //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); -} \ No newline at end of file +} + +void test_no_rows_won_bet_equals_10() { + + //arrange + char testSymbols[] = {"AABAACAAD"}; + + + int expectedResult = 0; + + //act + int actualResult = getWinnings(testSymbols, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} From 289293d5ffc3d165e6963c94022613589d83dfee Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 18:24:54 +0100 Subject: [PATCH 18/25] Tests, falls eine Reihe gewinnt --- src/test/c/Stefan/test_slot_machine.c | 57 ++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/src/test/c/Stefan/test_slot_machine.c b/src/test/c/Stefan/test_slot_machine.c index 76f8258..8a05f53 100644 --- a/src/test/c/Stefan/test_slot_machine.c +++ b/src/test/c/Stefan/test_slot_machine.c @@ -45,7 +45,6 @@ void test_no_rows_won_bet_equals_10() { //arrange char testSymbols[] = {"AABAACAAD"}; - int expectedResult = 0; //act @@ -54,3 +53,59 @@ void test_no_rows_won_bet_equals_10() { //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } + +void test_3_A_won_bet_equals_10() { + + //arrange + char testSymbols[] = {"AAAAACAAD"}; + + int expectedResult = 30; + + //act + int actualResult = getWinnings(testSymbols, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_3_B_won_bet_equals_10() { + + //arrange + char testSymbols[] = {"BBBAACAAD"}; + + int expectedResult = 50; + + //act + int actualResult = getWinnings(testSymbols, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_3_C_won_bet_equals_10() { + + //arrange + char testSymbols[] = {"BABAACCCC"}; + + int expectedResult = 100; + + //act + int actualResult = getWinnings(testSymbols, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_3_D_won_bet_equals_10() { + + //arrange + char testSymbols[] = {"BABDDDCAC"}; + + int expectedResult = 200; + + //act + int actualResult = getWinnings(testSymbols, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} From cb6b8471df336ddaf482acb45d527209b5415105 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 18:31:17 +0100 Subject: [PATCH 19/25] Tests, falls 2 Reihen gewinnen + Fehler bei Gewinnberechnung behoben --- src/main/c/Stefan/slot_machine.c | 8 +-- src/test/c/Stefan/test_slot_machine.c | 85 +++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 4 deletions(-) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index 5acc8bf..e6395ba 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -108,10 +108,10 @@ int getWinnings(char symbols[], int bet){ if (symbols[i * ROWS] == symbols[i * ROWS + 1] && symbols[i * ROWS] == symbols[i * ROWS + 2]) { switch(symbols[i * ROWS]){ - case 'A': winnings = bet * 3; break; - case 'B': winnings = bet * 5; break; - case 'C': winnings = bet * 10; break; - case 'D': winnings = bet * 20; break; + 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/test/c/Stefan/test_slot_machine.c b/src/test/c/Stefan/test_slot_machine.c index 8a05f53..3739660 100644 --- a/src/test/c/Stefan/test_slot_machine.c +++ b/src/test/c/Stefan/test_slot_machine.c @@ -16,6 +16,7 @@ void test_subtract_bet_10_from_balance_10() { //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } + void test_subtract_bet_0_from_balance_10() { //arrange @@ -109,3 +110,87 @@ void test_3_D_won_bet_equals_10() { //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } + +void test_6_A_won_bet_equals_10() { + + //arrange + char testSymbols[] = {"AAAAAACAC"}; + + int expectedResult = 60; + + //act + int actualResult = getWinnings(testSymbols, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_6_B_won_bet_equals_10() { + + //arrange + char testSymbols[] = {"BBBBBBCAC"}; + + int expectedResult = 100; + + //act + int actualResult = getWinnings(testSymbols, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_6_C_won_bet_equals_10() { + + //arrange + char testSymbols[] = {"CCCCCCCAC"}; + + int expectedResult = 200; + + //act + int actualResult = getWinnings(testSymbols, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_6_D_won_bet_equals_10() { + + //arrange + char testSymbols[] = {"DDDDDDCAC"}; + + int expectedResult = 400; + + //act + int actualResult = getWinnings(testSymbols, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_3_A_3_B_won_bet_equals_10() { + + //arrange + char testSymbols[] = {"AAABBBCAC"}; + + int expectedResult = 80; + + //act + int actualResult = getWinnings(testSymbols, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_3_C_3_D_won_bet_equals_10() { + + //arrange + char testSymbols[] = {"BAACCCDDD"}; + + int expectedResult = 300; + + //act + int actualResult = getWinnings(testSymbols, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} From 4e72b760f002ecebad718f162cbcdf34ec07f4c5 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 18:33:31 +0100 Subject: [PATCH 20/25] Tests, falls 3 Reihen gewinnen --- 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 3739660..f2a17c9 100644 --- a/src/test/c/Stefan/test_slot_machine.c +++ b/src/test/c/Stefan/test_slot_machine.c @@ -194,3 +194,31 @@ void test_3_C_3_D_won_bet_equals_10() { //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } + +void test_9_A_won_bet_equals_10() { + + //arrange + char testSymbols[] = {"AAAAAAAAA"}; + + int expectedResult = 90; + + //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 + char testSymbols[] = {"BBBCCCDDD"}; + + int expectedResult = 350; + + //act + int actualResult = getWinnings(testSymbols, 10); + + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} \ No newline at end of file From 12ff8cbc056949898d3b347b87875228d832374c Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 18:37:22 +0100 Subject: [PATCH 21/25] refactoring: Konsolenausgabe etwas schoener formatiert --- src/main/c/Stefan/slot_machine.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index e6395ba..d97ff48 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -126,6 +126,7 @@ int getWinnings(char symbols[], int bet){ } void showResult(char symbols[]){ + printf("\n"); for (int i = 0; i < ROWS; i++){ printf(" %c | %c | %c\n", symbols[i * ROWS], symbols[i * ROWS + 1], symbols[i * ROWS + 2]); From ad5d29af775a87b4b50f5049c2cb0d56cf2ed4f1 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 18:38:36 +0100 Subject: [PATCH 22/25] bugfix: Wetteinsatz von 0 nicht mehr moeglich --- 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 d97ff48..a3731e6 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -42,7 +42,7 @@ int getBet(int balance){ printf("Wetteinsatz: "); int bet = userInput(); - if(bet <= balance && bet >= 0){ + if(bet <= balance && bet > 0){ return bet; } else { printf("Ungueltige Eingabe!\n"); From a35238d83f2afb679494a8634608d564f4c94903 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 19:03:24 +0100 Subject: [PATCH 23/25] Ergebnis wird mit coolem ascii art angezeigt --- src/main/c/Stefan/slot_machine.c | 32 ++++++++++++++++++++++++++------ src/main/c/Stefan/slot_machine.h | 2 +- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/main/c/Stefan/slot_machine.c b/src/main/c/Stefan/slot_machine.c index a3731e6..c7bda9e 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -2,6 +2,7 @@ #include #include #include +#include void slotMachine(){ @@ -86,9 +87,10 @@ void randomizeSymbols(char symbols[]){ int spin(char symbols[], int bet, int balance){ - showResult(symbols); - int winnings = getWinnings(symbols, bet); + + showResult(symbols, winnings); + balance += winnings; printf("Aktuelles Guthaben: %d\n-------------------------\n", balance); return balance; @@ -125,12 +127,30 @@ int getWinnings(char symbols[], int bet){ return winnings; } -void showResult(char symbols[]){ - printf("\n"); +void showResult(char symbols[], int winnings){ - for (int i = 0; i < ROWS; i++){ - printf(" %c | %c | %c\n", symbols[i * ROWS], symbols[i * ROWS + 1], symbols[i * ROWS + 2]); + char winnerMessage[] = {""}; + + if(winnings > 0){ + strcpy(winnerMessage, " WINNER "); + } else { + strcpy(winnerMessage, " LOSER "); } + + printf("\n" + " .-------.\n" + " {-%s-} \n" + " .=============.\n" + " | | __\n" + " | [%c] [%c] [%c] |( )\n" + " | [%c] [%c] [%c] | ||\n" + " | [%c] [%c] [%c] | ||\n" + " | |_||\n" + " | xxx ::::::: |--'\n" + " | ooo ::::::: |\n" + " | $$$ ::::::: |\n" + " | __ |\n" + " |_____/__\\____|\n\n", winnerMessage, symbols[0], symbols[1], symbols[2], symbols[3], symbols[4], symbols[5], symbols[6], symbols[7], symbols[8]); } void welcomeMessage(){ diff --git a/src/main/c/Stefan/slot_machine.h b/src/main/c/Stefan/slot_machine.h index 3639e7e..c08b4bb 100644 --- a/src/main/c/Stefan/slot_machine.h +++ b/src/main/c/Stefan/slot_machine.h @@ -14,6 +14,6 @@ int subtractBetFromBalance(int bet, int balance); void randomizeSymbols(char symbols[]); int spin(char symbols[], int bet, int balance); int getWinnings(char symbols[], int bet); -void showResult(char symbols[]); +void showResult(char symbols[], int winnings); #endif // SLOT_MACHINE_H \ No newline at end of file From 28f34d7c9ffaa76dfd6423fa610954d05a9037a3 Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Mon, 29 Jan 2024 19:06:40 +0100 Subject: [PATCH 24/25] bugfix: Guthaben wird nach einem win richtig aktualisiert --- 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 c7bda9e..3a34c10 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -18,7 +18,7 @@ void slotMachine(){ balance = subtractBetFromBalance(bet, balance); randomizeSymbols(symbols); - spin(symbols, bet, balance); + balance = spin(symbols, bet, balance); } } From d52d1233c4f22a09419cf861f790decbc1a032fc Mon Sep 17 00:00:00 2001 From: fdai7472 Date: Thu, 1 Feb 2024 18:00:44 +0100 Subject: [PATCH 25/25] refactoring: scanf_s zu scanf geaendert --- 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 3a34c10..f659eab 100644 --- a/src/main/c/Stefan/slot_machine.c +++ b/src/main/c/Stefan/slot_machine.c @@ -33,7 +33,7 @@ int getBalance(){ int userInput(){ int input; - scanf_s("%d", &input); + scanf("%d", &input); return input; }