From 4548b58832fc54a770cbfb27b83ad720d57aa397 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 14:47:12 +0100 Subject: [PATCH 01/46] Anlegen der Team main.c-Datei --- src/main/c/main.c | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/c/main.c diff --git a/src/main/c/main.c b/src/main/c/main.c new file mode 100644 index 0000000..4a198ce --- /dev/null +++ b/src/main/c/main.c @@ -0,0 +1,7 @@ +#include + +int main() +{ + printf("Hello World!"); + return 0; +} \ No newline at end of file From a24ec8913db4e57c4ac0d2f538015f7210134b20 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 14:48:15 +0100 Subject: [PATCH 02/46] Implementation des Interfaces in der main-Datei --- src/main/c/main.c | 58 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/src/main/c/main.c b/src/main/c/main.c index 4a198ce..b053718 100644 --- a/src/main/c/main.c +++ b/src/main/c/main.c @@ -1,7 +1,61 @@ +//todo +// Abfolge, um die main-Funktion mit eurem Spiel zu erweitern: +// 1.Includiert eure .h-Datei +// 2.Nehmt folgende Veränderungen in der Funktion openInterface() vor +// 2.1:Fügt dem ersten printf euren Spielnamen mit der nächsten Ziffer der Liste hinzu +// 2.2:Ruft in der switch-Funktion eure Spiel-Funktion in dem nächstfreien Case auf und entkommentiert diesen Case + + + + +//todo +// Includiert hier euer .h-Datei für das entsprechende Spiel mit '#include "datei.h"' #include + +void openInterface(); + int main() { - printf("Hello World!"); + openInterface(); return 0; -} \ No newline at end of file +} + +void openInterface() +{ + int selection; + do { + // todo + // In diesem printf bitte euer Spiel mit der nächsten Ziffer und seinem entsprechendem Namen auflisten + // Vergesst das \n am Ende des Namens nicht!!! + printf("\n\nHallo und willkommen bei unserer Spielesammlung!!!\n" + "Du hast folgende Spiele zur Auswahl:\n\n" + "1: \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); + + + //todo + // In die Switch bitte beim Case mit der oben eingetragenen Ziffer zu eurem Spiel eure Spielefunktion vor dem break; aufrufen + // und die entsprechende case entkommentieren (Verschieben vom /* vor das nächste Case). + switch (selection) + { + /*case(1): + break; + case(2): + //Spiel() + break; + case(3): + //Spiel() + break; + case(4): + //Spiel() + break; + case(5): + //Spiel() + break;*/ + default: + break; + } + } while (selection != 0); +} From 646f5ad0683b2eb0d580377099c72414fc9a5a46 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 15:03:07 +0100 Subject: [PATCH 03/46] Anlegen der Dateistruktur von Schere Stein Papier --- src/main/c/Tim/SchereSteinPapier.c | 9 ++++++++ src/main/c/Tim/SchereSteinPapier.h | 6 +++++ src/main/c/main.c | 30 +++++++++++++------------ src/test/c/Tim/test_SchereSteinPapier.c | 21 +++++++++++++++++ 4 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 src/main/c/Tim/SchereSteinPapier.c create mode 100644 src/main/c/Tim/SchereSteinPapier.h create mode 100644 src/test/c/Tim/test_SchereSteinPapier.c diff --git a/src/main/c/Tim/SchereSteinPapier.c b/src/main/c/Tim/SchereSteinPapier.c new file mode 100644 index 0000000..4d485f8 --- /dev/null +++ b/src/main/c/Tim/SchereSteinPapier.c @@ -0,0 +1,9 @@ +#include "SchereSteinPapier.h" +#include + + +int schereSteinPapier() +{ + printf("Test"); + return 1; +} diff --git a/src/main/c/Tim/SchereSteinPapier.h b/src/main/c/Tim/SchereSteinPapier.h new file mode 100644 index 0000000..8b695c2 --- /dev/null +++ b/src/main/c/Tim/SchereSteinPapier.h @@ -0,0 +1,6 @@ +#ifndef SCHERESTEINPAPIER_H +#define SCHERESTEINPAPIER_H + +int schereSteinPapier(); + +#endif diff --git a/src/main/c/main.c b/src/main/c/main.c index b053718..6af2d92 100644 --- a/src/main/c/main.c +++ b/src/main/c/main.c @@ -11,6 +11,7 @@ //todo // Includiert hier euer .h-Datei für das entsprechende Spiel mit '#include "datei.h"' #include +#include "Tim/SchereSteinPapier.h" void openInterface(); @@ -30,7 +31,7 @@ void openInterface() // Vergesst das \n am Ende des Namens nicht!!! printf("\n\nHallo und willkommen bei unserer Spielesammlung!!!\n" "Du hast folgende Spiele zur Auswahl:\n\n" - "1: \n"); + "1: Schere-Stein-Papier\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); @@ -40,20 +41,21 @@ void openInterface() // und die entsprechende case entkommentieren (Verschieben vom /* vor das nächste Case). switch (selection) { - /*case(1): + case(1): + schereSteinPapier(); break; - case(2): - //Spiel() - break; - case(3): - //Spiel() - break; - case(4): - //Spiel() - break; - case(5): - //Spiel() - break;*/ + /*case(2): + //Spiel() + break; + case(3): + //Spiel() + break; + case(4): + //Spiel() + break; + case(5): + //Spiel() + break;*/ default: break; } diff --git a/src/test/c/Tim/test_SchereSteinPapier.c b/src/test/c/Tim/test_SchereSteinPapier.c new file mode 100644 index 0000000..9c41029 --- /dev/null +++ b/src/test/c/Tim/test_SchereSteinPapier.c @@ -0,0 +1,21 @@ +#include "SchereSteinPapier.h" +#include "unity.h" + +void setUp(void) +{ + +} + +void tearDown(void) +{ + +} + + +void testingCeedlingFunctionality() +{ + int expectedResult = 1; + + int actualResult = schereSteinPapier(); + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} \ No newline at end of file From a19c2caa67b1f266404c1f50797aa93641d7076e Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 15:21:56 +0100 Subject: [PATCH 04/46] =?UTF-8?q?Schreiben=20der=20Empfangsnachricht=20und?= =?UTF-8?q?=20Implementation=20des=20GameLoops=20+=20Ab=C3=A4nderung=20der?= =?UTF-8?q?=20Funktion=20zu=20void?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/SchereSteinPapier.c | 26 ++++++++++++++++++++++--- src/main/c/Tim/SchereSteinPapier.h | 2 +- src/test/c/Tim/test_SchereSteinPapier.c | 2 +- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/c/Tim/SchereSteinPapier.c b/src/main/c/Tim/SchereSteinPapier.c index 4d485f8..d71250a 100644 --- a/src/main/c/Tim/SchereSteinPapier.c +++ b/src/main/c/Tim/SchereSteinPapier.c @@ -2,8 +2,28 @@ #include -int schereSteinPapier() +void schereSteinPapier() { - printf("Test"); - return 1; + printf("\nHallo und Willkommen zu Schere-Stein-Papier!\n\nIn diesem Spiel spielst du gegen einen COM Schere-Stein-Papier!\n" + "Waehle, sobald dich die Konsole dazu auffordert, deine 'Waffe' aus, indem du die entsprechende Zahl eintippst.\n" + "Gibst du bei der Aufforderung 0 ein, gelangst du zurueck ins Hauptmenue!\n\n"); + while(1) + { + int com, user; + printf("Bitte treffe deine Wahl!\n" + "1: Schere \n2: Stein \n3: Papier\n0: Spiel verlassen\n"); + scanf("%d", &user); + if(user == 0) + { + break; + } + else if(user == 1 || user == 2 || user == 3) + { + //do something + } + else + { + printf("Deine eingegebene Wahl ist ungueltig\n"); + } + } } diff --git a/src/main/c/Tim/SchereSteinPapier.h b/src/main/c/Tim/SchereSteinPapier.h index 8b695c2..35c884b 100644 --- a/src/main/c/Tim/SchereSteinPapier.h +++ b/src/main/c/Tim/SchereSteinPapier.h @@ -1,6 +1,6 @@ #ifndef SCHERESTEINPAPIER_H #define SCHERESTEINPAPIER_H -int schereSteinPapier(); +void schereSteinPapier(); #endif diff --git a/src/test/c/Tim/test_SchereSteinPapier.c b/src/test/c/Tim/test_SchereSteinPapier.c index 9c41029..afe264c 100644 --- a/src/test/c/Tim/test_SchereSteinPapier.c +++ b/src/test/c/Tim/test_SchereSteinPapier.c @@ -16,6 +16,6 @@ void testingCeedlingFunctionality() { int expectedResult = 1; - int actualResult = schereSteinPapier(); + int actualResult = 1; TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } \ No newline at end of file From edaeb98d0a6f80b4e40fd8a64a475baef40eb2cc Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 15:29:47 +0100 Subject: [PATCH 05/46] =?UTF-8?q?Implementation=20der=20zuf=C3=A4lligen=20?= =?UTF-8?q?Computerauswahl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/SchereSteinPapier.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/c/Tim/SchereSteinPapier.c b/src/main/c/Tim/SchereSteinPapier.c index d71250a..72d76fc 100644 --- a/src/main/c/Tim/SchereSteinPapier.c +++ b/src/main/c/Tim/SchereSteinPapier.c @@ -1,5 +1,7 @@ #include "SchereSteinPapier.h" #include +#include +#include void schereSteinPapier() @@ -19,7 +21,9 @@ void schereSteinPapier() } else if(user == 1 || user == 2 || user == 3) { - //do something + srand(time(NULL)); + com = rand() % 3 + 1; + printf("%d\n",com); } else { From 58e6b3018bd42428bc3fdd8f8991c9e4e562fe9e Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 18:47:09 +0100 Subject: [PATCH 06/46] Implementation der Berechnung des Gewinners --- src/main/c/Tim/SchereSteinPapier.c | 51 +++++++++++++++++++++++++++++- src/main/c/Tim/SchereSteinPapier.h | 1 + 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/main/c/Tim/SchereSteinPapier.c b/src/main/c/Tim/SchereSteinPapier.c index 72d76fc..6862522 100644 --- a/src/main/c/Tim/SchereSteinPapier.c +++ b/src/main/c/Tim/SchereSteinPapier.c @@ -17,13 +17,24 @@ void schereSteinPapier() scanf("%d", &user); if(user == 0) { + printf("Vielen Dank fürs Spielen! Tschau!\n"); break; } else if(user == 1 || user == 2 || user == 3) { srand(time(NULL)); com = rand() % 3 + 1; - printf("%d\n",com); + int win = calculateWinner(user, com); + switch (win) + { + case(-1):printf("Der Computer hat %d gewaehlt, Du hast verloren!!!\n\n", com); + break; + case(0): printf("Der Computer hat %d gewaehlt, Es steht unentschieden!!!\n\n", com); + break; + case(1): printf("Der Computer hat %d gewaehlt, Du hast gewonnen!!!\n\n", com); + break; + default: printf("Error!"); + } } else { @@ -31,3 +42,41 @@ void schereSteinPapier() } } } + + +int calculateWinner(int x, int y) +{ + switch (x) + { + case(1): + switch (y) { + case(1): return 0; + + case(2): return -1; + + case(3): return 1; + + default: return 3; + } + case(2): + switch (y) { + case(1): return 1; + + case(2): return 0; + + case(3): return -1; + + default: return 3; + } + case(3): + switch (y) { + case(1): return 1; + + case(2): return -1; + + case(3): return 0; + + default: return 3; + } + } +} \ No newline at end of file diff --git a/src/main/c/Tim/SchereSteinPapier.h b/src/main/c/Tim/SchereSteinPapier.h index 35c884b..a96c9f9 100644 --- a/src/main/c/Tim/SchereSteinPapier.h +++ b/src/main/c/Tim/SchereSteinPapier.h @@ -2,5 +2,6 @@ #define SCHERESTEINPAPIER_H void schereSteinPapier(); +int calculateWinner(int, int); #endif From feab52722beb1e39400bf6cda3b0d3af5c37b861 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 18:49:16 +0100 Subject: [PATCH 07/46] =?UTF-8?q?Implementation=20der=20Tests=20f=C3=BCr?= =?UTF-8?q?=20Testfall=20Unentschieden?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/c/Tim/test_SchereSteinPapier.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/test/c/Tim/test_SchereSteinPapier.c b/src/test/c/Tim/test_SchereSteinPapier.c index afe264c..5a65733 100644 --- a/src/test/c/Tim/test_SchereSteinPapier.c +++ b/src/test/c/Tim/test_SchereSteinPapier.c @@ -12,10 +12,28 @@ void tearDown(void) } -void testingCeedlingFunctionality() +void test_drawScissorScissor() { - int expectedResult = 1; + int expectedResult = 0; + + int actualResult = calculateWinner(1,1); + + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_drawRockRock() +{ + int expectedResult = 0; + + int actualResult = calculateWinner(2,2); + + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} +void test_drawPaperPaper() +{ + int expectedResult = 0; + + int actualResult = calculateWinner(3,3); - int actualResult = 1; TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } \ No newline at end of file From bc7ba1d0a6a908e426e91b52bea930e80309cf29 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 18:51:52 +0100 Subject: [PATCH 08/46] =?UTF-8?q?Implementation=20der=20Tests=20f=C3=BCr?= =?UTF-8?q?=20den=20Testfall=20'Verloren'=20+=20Korrektur=20im=20Produktiv?= =?UTF-8?q?code=20bei=20der=20Ergebnisberechnung?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/SchereSteinPapier.c | 4 ++-- src/test/c/Tim/test_SchereSteinPapier.c | 27 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main/c/Tim/SchereSteinPapier.c b/src/main/c/Tim/SchereSteinPapier.c index 6862522..3b71014 100644 --- a/src/main/c/Tim/SchereSteinPapier.c +++ b/src/main/c/Tim/SchereSteinPapier.c @@ -70,9 +70,9 @@ int calculateWinner(int x, int y) } case(3): switch (y) { - case(1): return 1; + case(1): return -1; - case(2): return -1; + case(2): return 1; case(3): return 0; diff --git a/src/test/c/Tim/test_SchereSteinPapier.c b/src/test/c/Tim/test_SchereSteinPapier.c index 5a65733..6707100 100644 --- a/src/test/c/Tim/test_SchereSteinPapier.c +++ b/src/test/c/Tim/test_SchereSteinPapier.c @@ -35,5 +35,32 @@ void test_drawPaperPaper() int actualResult = calculateWinner(3,3); + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_lose_USER_Scissor_COM_Rock() +{ + int expectedResult = -1; + + int actualResult = calculateWinner(1,2); + + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_lose_USER_Paper_COM_Scissor() +{ + int expectedResult = -1; + + int actualResult = calculateWinner(3,1); + + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_lose_USER_Rock_COM_Paper() +{ + int expectedResult = -1; + + int actualResult = calculateWinner(2,3); + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } \ No newline at end of file From 87b5a7d42a7f71349b0dc3f9c3b622e5418cfae2 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 18:53:31 +0100 Subject: [PATCH 09/46] =?UTF-8?q?Implementation=20der=20Test=20f=C3=BCr=20?= =?UTF-8?q?den=20Testfall=20'Sieg'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/c/Tim/test_SchereSteinPapier.c | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/test/c/Tim/test_SchereSteinPapier.c b/src/test/c/Tim/test_SchereSteinPapier.c index 6707100..bbe81fe 100644 --- a/src/test/c/Tim/test_SchereSteinPapier.c +++ b/src/test/c/Tim/test_SchereSteinPapier.c @@ -62,5 +62,31 @@ void test_lose_USER_Rock_COM_Paper() int actualResult = calculateWinner(2,3); + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} +void test_Win_USER_Scissor_COM_Paper() +{ + int expectedResult = 1; + + int actualResult = calculateWinner(1,3); + + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_Win_USER_Rock_COM_Scissor() +{ + int expectedResult = 1; + + int actualResult = calculateWinner(2,1); + + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_Win_USER_Paper_COM_Rock() +{ + int expectedResult = 1; + + int actualResult = calculateWinner(3,2); + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } \ No newline at end of file From 73943582ed2fef7c42d788acef3df763ca71de5f Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 18:55:44 +0100 Subject: [PATCH 10/46] =?UTF-8?q?Implementation=20der=20Tests=20f=C3=BCr?= =?UTF-8?q?=20den=20Testfall=20ungueltige=20Parameter=C3=BCbergabe=20bei?= =?UTF-8?q?=20calculateWinner()=20+=20Korrektur=20im=20Prodktivcode=20(Ein?= =?UTF-8?q?f=C3=BCgen=20von=20Default=20bei=20=C3=A4u=C3=9Ferer=20Switch)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/SchereSteinPapier.c | 6 +++- src/test/c/Tim/test_SchereSteinPapier.c | 38 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/main/c/Tim/SchereSteinPapier.c b/src/main/c/Tim/SchereSteinPapier.c index 3b71014..12b9735 100644 --- a/src/main/c/Tim/SchereSteinPapier.c +++ b/src/main/c/Tim/SchereSteinPapier.c @@ -43,7 +43,9 @@ void schereSteinPapier() } } - +//Berechnung,welche Auswahl gewinnt. +//@return: 0 = unentschieden; 1 = gewonnen; -1 = verloren; 3 = Fehler bei der Wertübergabe +//@param x = UserChoice; y = COMChoice int calculateWinner(int x, int y) { switch (x) @@ -78,5 +80,7 @@ int calculateWinner(int x, int y) default: return 3; } + default: return 3; } + } \ No newline at end of file diff --git a/src/test/c/Tim/test_SchereSteinPapier.c b/src/test/c/Tim/test_SchereSteinPapier.c index bbe81fe..2b1d348 100644 --- a/src/test/c/Tim/test_SchereSteinPapier.c +++ b/src/test/c/Tim/test_SchereSteinPapier.c @@ -1,5 +1,6 @@ #include "SchereSteinPapier.h" #include "unity.h" +#include void setUp(void) { @@ -88,5 +89,42 @@ void test_Win_USER_Paper_COM_Rock() int actualResult = calculateWinner(3,2); + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + + +void test_USER_unknownParameter_Rock_randomChoice() +{ + int expectedResult = 3; + + int actualResult = calculateWinner(5,rand() % 3 + 1); + + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_USER_randomChoice_Rock_unknownParameter() +{ + int expectedResult = 3; + + int actualResult = calculateWinner(rand() % 3 + 1, 5); + + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_USER_randomChoice_Rock_zero() +{ + int expectedResult = 3; + + int actualResult = calculateWinner(rand() % 3 + 1, 0); + + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + +void test_USER_zero_Rock_randomChoice() +{ + int expectedResult = 3; + + int actualResult = calculateWinner(0, rand() % 3 + 1); + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } \ No newline at end of file From 8c90131f903ef33f23bcc684afc1da7439abc4f8 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 18:57:45 +0100 Subject: [PATCH 11/46] =?UTF-8?q?refactoring:=20Einheitliche=20Benennung?= =?UTF-8?q?=20der=20Test,=20Strukturierung=20nach=20Testf=C3=A4llen=20und?= =?UTF-8?q?=20Kommentierung=20der=20Tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/c/Tim/test_SchereSteinPapier.c | 118 +++++++++++++++--------- 1 file changed, 73 insertions(+), 45 deletions(-) diff --git a/src/test/c/Tim/test_SchereSteinPapier.c b/src/test/c/Tim/test_SchereSteinPapier.c index 2b1d348..23e6143 100644 --- a/src/test/c/Tim/test_SchereSteinPapier.c +++ b/src/test/c/Tim/test_SchereSteinPapier.c @@ -12,119 +12,147 @@ void tearDown(void) } - -void test_drawScissorScissor() +//Testfälle für Unentschieden +//1. Beide wählen Schere +void test_draw_USER_Scissor_COM_Scissor() { + //arrange int expectedResult = 0; - + //act int actualResult = calculateWinner(1,1); - + //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } - -void test_drawRockRock() +//2. Beide wählen Stein +void test_draw_USER_Rock_COM_Rock() { + //arrange int expectedResult = 0; - + //act int actualResult = calculateWinner(2,2); - + //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } -void test_drawPaperPaper() +//3. Beide wählen Papier +void test_draw_USER_Paper_COM_Paper() { + //arrange int expectedResult = 0; - + //act int actualResult = calculateWinner(3,3); - + //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } + +//Testfälle für Niederlage +//1. User wählt Schere, Computer wählt Stein void test_lose_USER_Scissor_COM_Rock() { + //arrange int expectedResult = -1; - + //act int actualResult = calculateWinner(1,2); - + //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } - +//2. User wählt Papier, Computer wählt Schere void test_lose_USER_Paper_COM_Scissor() { + //arrange int expectedResult = -1; - + //act int actualResult = calculateWinner(3,1); - + //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } - +//3. User wählt Stein, Computer wählt Papier void test_lose_USER_Rock_COM_Paper() { + //arrange int expectedResult = -1; - + //act int actualResult = calculateWinner(2,3); - + //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } -void test_Win_USER_Scissor_COM_Paper() + + +//Testfälle für Sieg +//1. User wählt Schere, Computer wählt Papier +void test_win_USER_Scissor_COM_Paper() { + //arrange int expectedResult = 1; - + //act int actualResult = calculateWinner(1,3); - + //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } - -void test_Win_USER_Rock_COM_Scissor() +//2. User wählt Stein, Computer wählt Schere +void test_win_USER_Rock_COM_Scissor() { + //arrange int expectedResult = 1; - + //act int actualResult = calculateWinner(2,1); - + //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } - -void test_Win_USER_Paper_COM_Rock() +//3. User wählt Papier, Computer wählt Stein +void test_win_USER_Paper_COM_Rock() { + //arrange int expectedResult = 1; - + //act int actualResult = calculateWinner(3,2); - + //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } -void test_USER_unknownParameter_Rock_randomChoice() +//Testfälle für ungültige Parameterübergabe +//1. Durch den User wurde ein ungültiges Parameter übergeben +// Der Computer wählt eine Zahl zwischen 1 und 3 +void test_error_USER_unknownParameter_Rock_randomChoice() { + //arrange int expectedResult = 3; - + //act int actualResult = calculateWinner(5,rand() % 3 + 1); - + //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } - -void test_USER_randomChoice_Rock_unknownParameter() +//2. Der User wählt eine Zahl zwischen 1 und 3 +// Durch den Computer wurde ein ungültiges Parameter übergeben +void test_error_USER_randomChoice_Rock_unknownParameter() { + //arrange int expectedResult = 3; - + //act int actualResult = calculateWinner(rand() % 3 + 1, 5); - + //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } - -void test_USER_randomChoice_Rock_zero() +//3. Der User wählt eine Zahl zwischen 1 und 3 +// Durch den Computer wurde das ungültige Parameter 0 übergeben +void test_error_USER_randomChoice_Rock_zero() { + //arrange int expectedResult = 3; - + //act int actualResult = calculateWinner(rand() % 3 + 1, 0); - + //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } - -void test_USER_zero_Rock_randomChoice() +//4. Durch den User wurde das ungültige Parameter 0 übergeben +// Der Computer wählt eine Zahl zwischen 1 und 3 +void test_error_USER_zero_Rock_randomChoice() { + //arrange int expectedResult = 3; - + //act int actualResult = calculateWinner(0, rand() % 3 + 1); - + //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } \ No newline at end of file From 6021ee8e0ab116e3a94923a7d1ea84fd28245a53 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 18:59:29 +0100 Subject: [PATCH 12/46] =?UTF-8?q?refactoring:=20Einf=C3=BChren=20der=20kon?= =?UTF-8?q?stanten=20Variablen=20scissor,=20rock,=20paper=20f=C3=BCr=20die?= =?UTF-8?q?=20Nutzung=20in=20den=20Tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/c/Tim/test_SchereSteinPapier.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/test/c/Tim/test_SchereSteinPapier.c b/src/test/c/Tim/test_SchereSteinPapier.c index 23e6143..485a07b 100644 --- a/src/test/c/Tim/test_SchereSteinPapier.c +++ b/src/test/c/Tim/test_SchereSteinPapier.c @@ -2,6 +2,10 @@ #include "unity.h" #include +const int scissor = 1; +const int rock = 2; +const int paper = 3; + void setUp(void) { @@ -19,7 +23,7 @@ void test_draw_USER_Scissor_COM_Scissor() //arrange int expectedResult = 0; //act - int actualResult = calculateWinner(1,1); + int actualResult = calculateWinner(scissor,scissor); //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } @@ -29,7 +33,7 @@ void test_draw_USER_Rock_COM_Rock() //arrange int expectedResult = 0; //act - int actualResult = calculateWinner(2,2); + int actualResult = calculateWinner(rock,rock); //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } @@ -39,7 +43,7 @@ void test_draw_USER_Paper_COM_Paper() //arrange int expectedResult = 0; //act - int actualResult = calculateWinner(3,3); + int actualResult = calculateWinner(paper,paper); //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } @@ -52,7 +56,7 @@ void test_lose_USER_Scissor_COM_Rock() //arrange int expectedResult = -1; //act - int actualResult = calculateWinner(1,2); + int actualResult = calculateWinner(scissor,rock); //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } @@ -62,7 +66,7 @@ void test_lose_USER_Paper_COM_Scissor() //arrange int expectedResult = -1; //act - int actualResult = calculateWinner(3,1); + int actualResult = calculateWinner(paper,scissor); //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } @@ -72,7 +76,7 @@ void test_lose_USER_Rock_COM_Paper() //arrange int expectedResult = -1; //act - int actualResult = calculateWinner(2,3); + int actualResult = calculateWinner(rock,paper); //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } @@ -85,7 +89,7 @@ void test_win_USER_Scissor_COM_Paper() //arrange int expectedResult = 1; //act - int actualResult = calculateWinner(1,3); + int actualResult = calculateWinner(scissor,paper); //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } @@ -95,7 +99,7 @@ void test_win_USER_Rock_COM_Scissor() //arrange int expectedResult = 1; //act - int actualResult = calculateWinner(2,1); + int actualResult = calculateWinner(rock,scissor); //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } @@ -105,7 +109,7 @@ void test_win_USER_Paper_COM_Rock() //arrange int expectedResult = 1; //act - int actualResult = calculateWinner(3,2); + int actualResult = calculateWinner(paper,rock); //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } From 4411a4b1e66f0074f6768a3d6209ee0145ea1417 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:06:32 +0100 Subject: [PATCH 13/46] =?UTF-8?q?refactoring:=20Verst=C3=A4ndlichere=20Ben?= =?UTF-8?q?ennung=20einiger=20Variablen:=20in=20schereSteinPapier():=20use?= =?UTF-8?q?r->userChoice,=20com->comChoice,=20win->winNum;=20formale=20Var?= =?UTF-8?q?iable=20in=20calculateWinner():=20x->userSelection,=20y->comSel?= =?UTF-8?q?ection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/SchereSteinPapier.c | 36 +++++++++++++++--------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/main/c/Tim/SchereSteinPapier.c b/src/main/c/Tim/SchereSteinPapier.c index 12b9735..a0c9509 100644 --- a/src/main/c/Tim/SchereSteinPapier.c +++ b/src/main/c/Tim/SchereSteinPapier.c @@ -11,27 +11,27 @@ void schereSteinPapier() "Gibst du bei der Aufforderung 0 ein, gelangst du zurueck ins Hauptmenue!\n\n"); while(1) { - int com, user; + int comChoice, userChoice; printf("Bitte treffe deine Wahl!\n" "1: Schere \n2: Stein \n3: Papier\n0: Spiel verlassen\n"); - scanf("%d", &user); - if(user == 0) + scanf("%d", &userChoice); + if(userChoice == 0) { - printf("Vielen Dank fürs Spielen! Tschau!\n"); + printf("Vielen Dank fuers Spielen! Tschau!\n"); break; } - else if(user == 1 || user == 2 || user == 3) + else if(userChoice == 1 || userChoice == 2 || userChoice == 3) { srand(time(NULL)); - com = rand() % 3 + 1; - int win = calculateWinner(user, com); - switch (win) + comChoice = rand() % 3 + 1; + int winNum = calculateWinner(userChoice, comChoice); + switch (winNum) { - case(-1):printf("Der Computer hat %d gewaehlt, Du hast verloren!!!\n\n", com); + case(-1):printf("Der Computer hat %d gewaehlt, Du hast verloren!!!\n\n", comChoice); break; - case(0): printf("Der Computer hat %d gewaehlt, Es steht unentschieden!!!\n\n", com); + case(0): printf("Der Computer hat %d gewaehlt, Es steht unentschieden!!!\n\n", comChoice); break; - case(1): printf("Der Computer hat %d gewaehlt, Du hast gewonnen!!!\n\n", com); + case(1): printf("Der Computer hat %d gewaehlt, Du hast gewonnen!!!\n\n", comChoice); break; default: printf("Error!"); } @@ -44,14 +44,14 @@ void schereSteinPapier() } //Berechnung,welche Auswahl gewinnt. -//@return: 0 = unentschieden; 1 = gewonnen; -1 = verloren; 3 = Fehler bei der Wertübergabe -//@param x = UserChoice; y = COMChoice -int calculateWinner(int x, int y) +//@return: 0 = unentschieden; 1 = gewonnen; -1 = verloren; 3 = Fehler bei der Wertuebergabe +//@param userSelection = UserChoice; comSelection = COMChoice +int calculateWinner(int userSelection, int comSelection) { - switch (x) + switch (userSelection) { case(1): - switch (y) { + switch (comSelection) { case(1): return 0; case(2): return -1; @@ -61,7 +61,7 @@ int calculateWinner(int x, int y) default: return 3; } case(2): - switch (y) { + switch (comSelection) { case(1): return 1; case(2): return 0; @@ -71,7 +71,7 @@ int calculateWinner(int x, int y) default: return 3; } case(3): - switch (y) { + switch (comSelection) { case(1): return -1; case(2): return 1; From 389a40da9bd8976781a425f6195b5bf8b22d9d41 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:07:50 +0100 Subject: [PATCH 14/46] =?UTF-8?q?=C3=84nderung=20Position=20der=20Initalis?= =?UTF-8?q?ierung=20von=20srand()=20um=20die=20Zuf=C3=A4lligkeit=20zu=20ga?= =?UTF-8?q?rantieren?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/SchereSteinPapier.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/c/Tim/SchereSteinPapier.c b/src/main/c/Tim/SchereSteinPapier.c index a0c9509..8c917ae 100644 --- a/src/main/c/Tim/SchereSteinPapier.c +++ b/src/main/c/Tim/SchereSteinPapier.c @@ -6,6 +6,7 @@ void schereSteinPapier() { + srand(time(NULL)); printf("\nHallo und Willkommen zu Schere-Stein-Papier!\n\nIn diesem Spiel spielst du gegen einen COM Schere-Stein-Papier!\n" "Waehle, sobald dich die Konsole dazu auffordert, deine 'Waffe' aus, indem du die entsprechende Zahl eintippst.\n" "Gibst du bei der Aufforderung 0 ein, gelangst du zurueck ins Hauptmenue!\n\n"); @@ -22,7 +23,6 @@ void schereSteinPapier() } else if(userChoice == 1 || userChoice == 2 || userChoice == 3) { - srand(time(NULL)); comChoice = rand() % 3 + 1; int winNum = calculateWinner(userChoice, comChoice); switch (winNum) From 2020e967fc7a4f1af597cd82844ba820c603e681 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:12:12 +0100 Subject: [PATCH 15/46] =?UTF-8?q?refactoring:=20Auslagern=20der=20Funktion?= =?UTF-8?q?alit=C3=A4t=20zu=20den=20Funktionen=20selectCOMChoice(),=20getW?= =?UTF-8?q?elcomeMessage(),=20printResult()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/SchereSteinPapier.c | 46 +++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/src/main/c/Tim/SchereSteinPapier.c b/src/main/c/Tim/SchereSteinPapier.c index 8c917ae..1117fba 100644 --- a/src/main/c/Tim/SchereSteinPapier.c +++ b/src/main/c/Tim/SchereSteinPapier.c @@ -3,13 +3,14 @@ #include #include +char* getWelcomeMessageSSP(); +int selectCOMChoice(); +void printResult(int, int); void schereSteinPapier() { srand(time(NULL)); - printf("\nHallo und Willkommen zu Schere-Stein-Papier!\n\nIn diesem Spiel spielst du gegen einen COM Schere-Stein-Papier!\n" - "Waehle, sobald dich die Konsole dazu auffordert, deine 'Waffe' aus, indem du die entsprechende Zahl eintippst.\n" - "Gibst du bei der Aufforderung 0 ein, gelangst du zurueck ins Hauptmenue!\n\n"); + printf("%s", getWelcomeMessageSSP()); while(1) { int comChoice, userChoice; @@ -23,18 +24,9 @@ void schereSteinPapier() } else if(userChoice == 1 || userChoice == 2 || userChoice == 3) { - comChoice = rand() % 3 + 1; + comChoice = selectCOMChoice(); int winNum = calculateWinner(userChoice, comChoice); - switch (winNum) - { - case(-1):printf("Der Computer hat %d gewaehlt, Du hast verloren!!!\n\n", comChoice); - break; - case(0): printf("Der Computer hat %d gewaehlt, Es steht unentschieden!!!\n\n", comChoice); - break; - case(1): printf("Der Computer hat %d gewaehlt, Du hast gewonnen!!!\n\n", comChoice); - break; - default: printf("Error!"); - } + printResult(winNum, comChoice); } else { @@ -83,4 +75,30 @@ int calculateWinner(int userSelection, int comSelection) default: return 3; } +} + +int selectCOMChoice() +{ + return rand() % 3 + 1; +} + +char* getWelcomeMessageSSP() +{ + return "\nHallo und Willkommen zu Schere-Stein-Papier!\n\nIn diesem Spiel spielst du gegen einen COM Schere-Stein-Papier!\n" + "Waehle, sobald dich die Konsole dazu auffordert, deine 'Waffe' aus, indem du die entsprechende Zahl eintippst.\n" + "Gibst du bei der Aufforderung 0 ein, gelangst du zurueck ins Hauptmenue!\n\n"; +} + +void printResult(int winNumb, int comSelect) +{ + switch (winNumb) + { + case(-1):printf("Der Computer hat %d gewaehlt, Du hast verloren!!!\n\n", comSelect); + break; + case(0): printf("Der Computer hat %d gewaehlt, Es steht unentschieden!!!\n\n", comSelect); + break; + case(1): printf("Der Computer hat %d gewaehlt, Du hast gewonnen!!!\n\n", comSelect); + break; + default: printf("Error!"); + } } \ No newline at end of file From cf943347d9d1df59cade13b0b383bd70de671460 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:16:03 +0100 Subject: [PATCH 16/46] =?UTF-8?q?=C3=9Cberarbeitung=20der=20Funktion=20pri?= =?UTF-8?q?ntResult(),=20damit=20die=20Auswahl=20des=20Computers=20als=20W?= =?UTF-8?q?ort=20statt=20als=20Zahl=20ausgegeben=20wird?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/SchereSteinPapier.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/c/Tim/SchereSteinPapier.c b/src/main/c/Tim/SchereSteinPapier.c index 1117fba..c1d26c9 100644 --- a/src/main/c/Tim/SchereSteinPapier.c +++ b/src/main/c/Tim/SchereSteinPapier.c @@ -2,6 +2,7 @@ #include #include #include +#include char* getWelcomeMessageSSP(); int selectCOMChoice(); @@ -89,16 +90,34 @@ char* getWelcomeMessageSSP() "Gibst du bei der Aufforderung 0 ein, gelangst du zurueck ins Hauptmenue!\n\n"; } + void printResult(int winNumb, int comSelect) { + char comWeapon[10]; + switch(comSelect) + { + case(1): + strcpy(comWeapon, "Schere"); + break; + case(2): + strcpy(comWeapon,"Stein"); + break; + case(3): + strcpy(comWeapon,"Papier"); + break; + default: + strcpy(comWeapon, "Not found"); + break; + } switch (winNumb) { - case(-1):printf("Der Computer hat %d gewaehlt, Du hast verloren!!!\n\n", comSelect); + case(-1):printf("Der Computer hat %s gewaehlt, Du hast verloren!!!\n\n", comWeapon); break; - case(0): printf("Der Computer hat %d gewaehlt, Es steht unentschieden!!!\n\n", comSelect); + case(0): printf("Der Computer hat %s gewaehlt, Es steht unentschieden!!!\n\n", comWeapon); break; - case(1): printf("Der Computer hat %d gewaehlt, Du hast gewonnen!!!\n\n", comSelect); + case(1): printf("Der Computer hat %s gewaehlt, Du hast gewonnen!!!\n\n", comWeapon); break; default: printf("Error!"); + break; } } \ No newline at end of file From 098c67d0def974ad31c04ad262b81bd895d03ac1 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:19:18 +0100 Subject: [PATCH 17/46] Erweiterung der Funktion printResult() mit ASCII-Art --- src/main/c/Tim/SchereSteinPapier.c | 124 +++++++++++++++++++++++++++-- 1 file changed, 119 insertions(+), 5 deletions(-) diff --git a/src/main/c/Tim/SchereSteinPapier.c b/src/main/c/Tim/SchereSteinPapier.c index c1d26c9..dada586 100644 --- a/src/main/c/Tim/SchereSteinPapier.c +++ b/src/main/c/Tim/SchereSteinPapier.c @@ -6,7 +6,9 @@ char* getWelcomeMessageSSP(); int selectCOMChoice(); -void printResult(int, int); +void printResult(int, int, int); + + void schereSteinPapier() { @@ -27,7 +29,7 @@ void schereSteinPapier() { comChoice = selectCOMChoice(); int winNum = calculateWinner(userChoice, comChoice); - printResult(winNum, comChoice); + printResult(winNum, comChoice, userChoice); } else { @@ -85,13 +87,13 @@ int selectCOMChoice() char* getWelcomeMessageSSP() { - return "\nHallo und Willkommen zu Schere-Stein-Papier!\n\nIn diesem Spiel spielst du gegen einen COM Schere-Stein-Papier!\n" + return "\nHallo und Willkommen zu Schere-Stein-Papier\n\n" + "In diesem Spiel spielst du gegen einen COM Schere-Stein-Papier!\n" "Waehle, sobald dich die Konsole dazu auffordert, deine 'Waffe' aus, indem du die entsprechende Zahl eintippst.\n" "Gibst du bei der Aufforderung 0 ein, gelangst du zurueck ins Hauptmenue!\n\n"; } - -void printResult(int winNumb, int comSelect) +void printResult(int winNumb, int comSelect, int userSelect) { char comWeapon[10]; switch(comSelect) @@ -109,6 +111,118 @@ void printResult(int winNumb, int comSelect) strcpy(comWeapon, "Not found"); break; } + if(userSelect == 1) + { + switch (comSelect) + { + case(1): + printf(" _______ _______\n" + "---' ____)____ ____(____ '---\n" + " ______) (______\n" + " __________) (__________\n" + " (____) (____)\n" + "---.__(___) (___)__.---\n" + " Scissor vs Scissor\n"); + break; + case(2): + printf(" _______ _______\n" + "---' ____)____ (____ '---\n" + " ______) (_____)\n" + " __________) (_____)\n" + " (____) (____)\n" + "---.__(___) (___)__.---\n" + " Scissor vs Rock\n"); + break; + case(3): + printf(" _______ _______\n" + "---' ____)____ ____(____ '----\n" + " ______) (______\n" + " __________) (_______\n" + " (____) (_______\n" + "---.__(___) (_________.---\n" + " Scissor vs Paper\n"); + break; + default: + printf("ungültige Eingabe\n"); + break; + } + } + else if(userSelect ==2) + { + switch (comSelect) + { + case(1): + printf(" _______ _______\n" + "---' ____) ____(____ '---\n" + " (_____) (______\n" + " (_____) (__________\n" + " (____) (____)\n" + "---.__(___) (___)__.---\n" + " Rock VS Scissor\n"); + break; + case(2): + printf(" _______ _______\n" + "---' ____) (____ '---\n" + " (_____) (_____)\n" + " (_____) (_____)\n" + " (____) (____)\n" + "---.__(___) (___)__.---\n" + " Rock VS Rock\n"); + break; + case(3): + printf(" _______ _______\n" + "---' ____) ____(____ '----\n" + " (_____) (______\n" + " (_____) (_______\n" + " (____) (_______\n" + "---.__(___) (_________.---\n" + " Rock VS Paper\n"); + break; + default: + printf("ungültige Eingabe\n"); + break; + } + } + else if(userSelect ==3) + { + switch (comSelect) + { + case(1): + printf(" _______ _______\n" + "---' ____)____ ____(____ '---\n" + " ______) (______\n" + " _______) (__________\n" + " _______) (____)\n" + "---.__________) (___)__.---\n" + " Paper VS Scissor\n"); + break; + case(2): + printf(" _______ _______\n" + "---' ____)____ (____ '---\n" + " ______) (_____)\n" + " _______) (_____)\n" + " _______) (____)\n" + "---.__________) (___)__.---\n" + " Paper VS Rock\n"); + break; + case(3): + printf(" _______ _______\n" + "---' ____)____ ____(____ '----\n" + " ______) (______\n" + " _______) (_______\n" + " _______) (_______\n" + "---.__________) (_________.---\n" + " Paper VS Paper\n"); + break; + default: + printf("ungültige Eingabe\n"); + break; + } + } + else + { + printf("ungültige Eingabe"); + } switch (winNumb) { case(-1):printf("Der Computer hat %s gewaehlt, Du hast verloren!!!\n\n", comWeapon); From c1e535f5e2a8b86a5a0e4eeee7565eccd181fdef Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:21:15 +0100 Subject: [PATCH 18/46] Erweiterung der Funktion getWelcomeMessageSSP() mit ASCII-Art --- src/main/c/Tim/SchereSteinPapier.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/c/Tim/SchereSteinPapier.c b/src/main/c/Tim/SchereSteinPapier.c index dada586..59fbf05 100644 --- a/src/main/c/Tim/SchereSteinPapier.c +++ b/src/main/c/Tim/SchereSteinPapier.c @@ -87,7 +87,16 @@ int selectCOMChoice() char* getWelcomeMessageSSP() { - return "\nHallo und Willkommen zu Schere-Stein-Papier\n\n" + return "\nHallo und Willkommen zu\n\n" + " _____ _ _____ _____ _ \r\n" + " | __ \\ | | | __ \\ / ____| (_) \r\n" + " | |__) |___ ___| | __ | |__) |_ _ _ __ ___ _ __ | (___ ___ _ ___ ___ ___ _ __ ___ \r\n" + " | _ // _ \\ / __| |/ / | ___/ _` | '_ \\ / _ \\ '__| \\___ \\ / __| / __/ __|/ _ \\| '__/ __|\r\n" + " | | \\ \\ (_) | (__| < | | | (_| | |_) | __/ | ____) | (__| \\__ \\__ \\ (_) | | \\__ \\\r\n" + " |_| \\_\\___/ \\___|_|\\_\\ |_| \\__,_| .__/ \\___|_| |_____/ \\___|_|___/___/\\___/|_| |___/ \r\n" + " | | \r\n" + " |_| \n" + "credits to wynand1004\n\n" "In diesem Spiel spielst du gegen einen COM Schere-Stein-Papier!\n" "Waehle, sobald dich die Konsole dazu auffordert, deine 'Waffe' aus, indem du die entsprechende Zahl eintippst.\n" "Gibst du bei der Aufforderung 0 ein, gelangst du zurueck ins Hauptmenue!\n\n"; From af0fdc1487d685554eaa5d4e646ad72d18fef5f0 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:27:04 +0100 Subject: [PATCH 19/46] =?UTF-8?q?refactoring:=20Auslagern=20von=20Funktion?= =?UTF-8?q?alit=C3=A4t=20zu=20gameLoop()=20und=20getuserSelection(),=20um?= =?UTF-8?q?=20die=20=C3=9Cbersichtlichkeit=20zu=20verbessern?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/SchereSteinPapier.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/c/Tim/SchereSteinPapier.c b/src/main/c/Tim/SchereSteinPapier.c index 59fbf05..828d337 100644 --- a/src/main/c/Tim/SchereSteinPapier.c +++ b/src/main/c/Tim/SchereSteinPapier.c @@ -4,9 +4,11 @@ #include #include +void gameLoop(); char* getWelcomeMessageSSP(); int selectCOMChoice(); void printResult(int, int, int); +int getuserSelection(); @@ -14,12 +16,15 @@ void schereSteinPapier() { srand(time(NULL)); printf("%s", getWelcomeMessageSSP()); + gameLoop(); +} + +void gameLoop() +{ while(1) { int comChoice, userChoice; - printf("Bitte treffe deine Wahl!\n" - "1: Schere \n2: Stein \n3: Papier\n0: Spiel verlassen\n"); - scanf("%d", &userChoice); + userChoice = getuserSelection(); if(userChoice == 0) { printf("Vielen Dank fuers Spielen! Tschau!\n"); @@ -37,7 +42,6 @@ void schereSteinPapier() } } } - //Berechnung,welche Auswahl gewinnt. //@return: 0 = unentschieden; 1 = gewonnen; -1 = verloren; 3 = Fehler bei der Wertuebergabe //@param userSelection = UserChoice; comSelection = COMChoice @@ -85,6 +89,16 @@ int selectCOMChoice() return rand() % 3 + 1; } +int getuserSelection() +{ + int userSelect; + printf("Bitte treffe deine Wahl!\n" + "1: Schere \n2: Stein \n3: Papier\n0: Spiel verlassen\n"); + scanf("%d", &userSelect); + + return userSelect; +} + char* getWelcomeMessageSSP() { return "\nHallo und Willkommen zu\n\n" From feebb4ebc612a11a708fbf641ebe27ed3afc2719 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:33:32 +0100 Subject: [PATCH 20/46] =?UTF-8?q?Anlegen=20der=20Dateien=20f=C3=BCr=20Hang?= =?UTF-8?q?man=20und=20Einbau=20in=20das=20Interface?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 7 +++++++ src/main/c/Tim/hangman.h | 6 ++++++ src/main/c/main.c | 14 ++++++++------ src/test/c/Tim/test_hangman.c | 24 ++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 src/main/c/Tim/hangman.c create mode 100644 src/main/c/Tim/hangman.h create mode 100644 src/test/c/Tim/test_hangman.c diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c new file mode 100644 index 0000000..d49319b --- /dev/null +++ b/src/main/c/Tim/hangman.c @@ -0,0 +1,7 @@ +#include "hangman.h" +#include + +void hangman() +{ + printf("Hello World!"); +} diff --git a/src/main/c/Tim/hangman.h b/src/main/c/Tim/hangman.h new file mode 100644 index 0000000..cd17d02 --- /dev/null +++ b/src/main/c/Tim/hangman.h @@ -0,0 +1,6 @@ +#ifndef HANGMAN_H +#define HANGMAN_H + +void hangman(); + +#endif diff --git a/src/main/c/main.c b/src/main/c/main.c index 6af2d92..cbaccdc 100644 --- a/src/main/c/main.c +++ b/src/main/c/main.c @@ -11,7 +11,8 @@ //todo // Includiert hier euer .h-Datei für das entsprechende Spiel mit '#include "datei.h"' #include -#include "Tim/SchereSteinPapier.h" +#include "SchereSteinPapier.h" +#include "hangman.h" void openInterface(); @@ -31,7 +32,8 @@ void openInterface() // Vergesst das \n am Ende des Namens nicht!!! printf("\n\nHallo und willkommen bei unserer Spielesammlung!!!\n" "Du hast folgende Spiele zur Auswahl:\n\n" - "1: Schere-Stein-Papier\n"); + "1: Schere-Stein-Papier\n" + "2: Hangman\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); @@ -44,10 +46,10 @@ void openInterface() case(1): schereSteinPapier(); break; - /*case(2): - //Spiel() - break; - case(3): + case(2): + hangman(); + break; + /*case(3): //Spiel() break; case(4): diff --git a/src/test/c/Tim/test_hangman.c b/src/test/c/Tim/test_hangman.c new file mode 100644 index 0000000..2afdf17 --- /dev/null +++ b/src/test/c/Tim/test_hangman.c @@ -0,0 +1,24 @@ +#include "hangman.h" +#include "unity.h" + + + +void setUp(void) +{ + +} + +void tearDown(void) +{ + +} + +void test_ceedling_functionality() +{ + //arrange + int expectedResult = 0; + //act + int actualResult = 0; + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} \ No newline at end of file From 6fdccd7ef13f314b7829b0afecc2e6a4aaa0a10c Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Thu, 25 Jan 2024 21:30:20 +0100 Subject: [PATCH 21/46] add my files --- src/main/c/Georg/tictactoe.c | 4 ++++ src/main/c/Georg/tictactoe.h | 5 +++++ src/test/c/Georg/test_tictactoe.c | 15 +++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 src/main/c/Georg/tictactoe.c create mode 100644 src/main/c/Georg/tictactoe.h create mode 100644 src/test/c/Georg/test_tictactoe.c diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c new file mode 100644 index 0000000..87680a0 --- /dev/null +++ b/src/main/c/Georg/tictactoe.c @@ -0,0 +1,4 @@ +#include +#include + +#include "tictactoe.h" diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h new file mode 100644 index 0000000..ca8f3ae --- /dev/null +++ b/src/main/c/Georg/tictactoe.h @@ -0,0 +1,5 @@ +#ifndef TICTACTOE_H +#define TICTACTOE_H + + +#endif //TICTACTOE_H diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c new file mode 100644 index 0000000..95b2edc --- /dev/null +++ b/src/test/c/Georg/test_tictactoe.c @@ -0,0 +1,15 @@ +#include "tictactoe.h" +#include "unity.h" + +void setup(void){ + +} + +void tearDown(void){ + +} + +void test_compileTest_shutBeAllwaysTrue(void){ + TEST_ASSERT_EQUAL_INT(1,1); +} + From 7ffbb05393e719cd55f1f6d1c012db8d5311044f Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 10:54:39 +0100 Subject: [PATCH 22/46] welcome message added --- src/main/c/Georg/tictactoe.c | 4 ++++ src/main/c/Georg/tictactoe.h | 1 + src/test/c/Georg/test_tictactoe.c | 10 ++++++++++ 3 files changed, 15 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 87680a0..c97a735 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -2,3 +2,7 @@ #include #include "tictactoe.h" + +char* getWelcomeMessage(){ + return "Hallo und willkommen zu unserem TicTacToe Spiel. Anbei die Anleitung:\n"; +} \ No newline at end of file diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index ca8f3ae..9ac6a1f 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -1,5 +1,6 @@ #ifndef TICTACTOE_H #define TICTACTOE_H +char* getWelcomeMessage(); #endif //TICTACTOE_H diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 95b2edc..05fa62c 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -13,3 +13,13 @@ void test_compileTest_shutBeAllwaysTrue(void){ TEST_ASSERT_EQUAL_INT(1,1); } +void test_welcome_message(void){ + // arrange + char* expectedMessage = "Hallo und willkommen zu unserem TicTacToe Spiel. Anbei die Anleitung:\n"; + + // act + char* message = getWelcomeMessage(); + + // aassert + TEST_ASSERT_EQUAL_STRING(expectedMessage, message); +} \ No newline at end of file From e2f9678cbbfa228e45bf3a27fb15fb5767d2bb21 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 10:56:26 +0100 Subject: [PATCH 23/46] rules message added --- src/main/c/Georg/tictactoe.c | 10 +++++++++- src/main/c/Georg/tictactoe.h | 1 + src/test/c/Georg/test_tictactoe.c | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index c97a735..7134783 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -5,4 +5,12 @@ char* getWelcomeMessage(){ return "Hallo und willkommen zu unserem TicTacToe Spiel. Anbei die Anleitung:\n"; -} \ No newline at end of file +} + +char* getRulesMessage(){ + return "Das spiel wird über die Komandozeile gespielt.\n" + "Jeder Spielzug ist eine Eingabe in die Konsole. Die enstsprechenden Befehle stehen jeweils unterhalb des Spielfelds.\n" + "Um ein Zug zu tätigen musst du \"set x,y\" in die Konsole Eingeben. Die Koordinaten stehen dabei für Zeile und Spalte.\n" + "Mit dem Befehl \"start\" startest du das Spiel" + "Mit dem Befehl \"rules\" kannst du diese Nachricht erneut aufrufen."; +} diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 9ac6a1f..72778f5 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -2,5 +2,6 @@ #define TICTACTOE_H char* getWelcomeMessage(); +char* getRulesMessage(); #endif //TICTACTOE_H diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 05fa62c..cc7b353 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -22,4 +22,19 @@ void test_welcome_message(void){ // aassert TEST_ASSERT_EQUAL_STRING(expectedMessage, message); +} + +void test_rules_message(void){ + // arrange + char* expectedMessage = "Das spiel wird über die Komandozeile gespielt.\n" + "Jeder Spielzug ist eine Eingabe in die Konsole. Die enstsprechenden Befehle stehen jeweils unterhalb des Spielfelds.\n" + "Um ein Zug zu tätigen musst du \"set x,y\" in die Konsole Eingeben. Die Koordinaten stehen dabei für Zeile und Spalte.\n" + "Mit dem Befehl \"start\" startest du das Spiel" + "Mit dem Befehl \"rules\" kannst du diese Nachricht erneut aufrufen."; + + // act + char* message = getRulesMessage(); + + // assert + TEST_ASSERT_EQUAL_STRING(expectedMessage, message); } \ No newline at end of file From b2938395d128f41435b3323b21620035419bb9a5 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 10:58:48 +0100 Subject: [PATCH 24/46] add ga structure that holds the game informations --- src/main/c/Georg/tictactoe.c | 2 ++ src/main/c/Georg/tictactoe.h | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 7134783..4b91348 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -3,6 +3,8 @@ #include "tictactoe.h" +struct ticTacToe TICTACTOE; + char* getWelcomeMessage(){ return "Hallo und willkommen zu unserem TicTacToe Spiel. Anbei die Anleitung:\n"; } diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 72778f5..d8b2a20 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -1,6 +1,10 @@ #ifndef TICTACTOE_H #define TICTACTOE_H +struct ticTacToe{ + int currentState; +}; + char* getWelcomeMessage(); char* getRulesMessage(); From f74e8420bb5067c14c62f352485677f92f137e09 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:02:41 +0100 Subject: [PATCH 25/46] add a test for the initialization of the game info structure --- src/main/c/Georg/tictactoe.c | 6 ++++++ src/main/c/Georg/tictactoe.h | 1 + src/test/c/Georg/test_tictactoe.c | 12 ++++++++++++ 3 files changed, 19 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 4b91348..f531dd4 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -16,3 +16,9 @@ char* getRulesMessage(){ "Mit dem Befehl \"start\" startest du das Spiel" "Mit dem Befehl \"rules\" kannst du diese Nachricht erneut aufrufen."; } + +struct ticTacToe createTicTacToe() { + struct ticTacToe newGame; + newGame.currentState = 0; + return newGame; +} \ No newline at end of file diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index d8b2a20..cc05322 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -7,5 +7,6 @@ struct ticTacToe{ char* getWelcomeMessage(); char* getRulesMessage(); +struct ticTacToe createTicTacToe(); #endif //TICTACTOE_H diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index cc7b353..cce0a6f 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -37,4 +37,16 @@ void test_rules_message(void){ // assert TEST_ASSERT_EQUAL_STRING(expectedMessage, message); +} + +void test_initial_state(void){ + // arrange + struct ticTacToe newGame; + int expectedState = 0; + + // act + newGame = createTicTacToe(); + + // assert + TEST_ASSERT_EQUAL_INT( expectedState, newGame.currentState ); } \ No newline at end of file From de794a0f4211f5ab3b48dca7ef8959c7e0293bc9 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:03:53 +0100 Subject: [PATCH 26/46] added a function that evaluates user input --- src/main/c/Georg/tictactoe.c | 7 +++++++ src/main/c/Georg/tictactoe.h | 1 + src/test/c/Georg/test_tictactoe.c | 12 ++++++++++++ 3 files changed, 20 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index f531dd4..961789b 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -1,5 +1,6 @@ #include #include +#include #include "tictactoe.h" @@ -21,4 +22,10 @@ struct ticTacToe createTicTacToe() { struct ticTacToe newGame; newGame.currentState = 0; return newGame; +} + +int handleState( char* input ){ + if( strcmp(input, "start game") == 0 ){ + return 1; + } } \ No newline at end of file diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index cc05322..2091478 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -8,5 +8,6 @@ struct ticTacToe{ char* getWelcomeMessage(); char* getRulesMessage(); struct ticTacToe createTicTacToe(); +int handleState( char* input ); #endif //TICTACTOE_H diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index cce0a6f..cd209a9 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -49,4 +49,16 @@ void test_initial_state(void){ // assert TEST_ASSERT_EQUAL_INT( expectedState, newGame.currentState ); +} + +void test_userInput(void){ + // arrange + int expectedState = 1; + char* input = "start game"; + + // act + int actualState = handleState( input ); + + // assert + TEST_ASSERT_EQUAL_INT( expectedState, actualState ); } \ No newline at end of file From f921aaaf1c1893d1cce3b48aea7023dc6350fa78 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:05:31 +0100 Subject: [PATCH 27/46] refactoring: commandHandler --- src/main/c/Georg/tictactoe.c | 2 +- src/main/c/Georg/tictactoe.h | 2 +- src/test/c/Georg/test_tictactoe.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 961789b..8501c52 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -24,7 +24,7 @@ struct ticTacToe createTicTacToe() { return newGame; } -int handleState( char* input ){ +int handeCommand( char* input ){ if( strcmp(input, "start game") == 0 ){ return 1; } diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 2091478..1500ee2 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -8,6 +8,6 @@ struct ticTacToe{ char* getWelcomeMessage(); char* getRulesMessage(); struct ticTacToe createTicTacToe(); -int handleState( char* input ); +int handeCommand( char* input ); #endif //TICTACTOE_H diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index cd209a9..8984fb8 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -51,13 +51,13 @@ void test_initial_state(void){ TEST_ASSERT_EQUAL_INT( expectedState, newGame.currentState ); } -void test_userInput(void){ +void test_command_startGame(void){ // arrange int expectedState = 1; char* input = "start game"; // act - int actualState = handleState( input ); + int actualState = handeCommand( input ); // assert TEST_ASSERT_EQUAL_INT( expectedState, actualState ); From 17bdcd90a7a6adbeec70d0534e336a7d8e03e016 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:06:08 +0100 Subject: [PATCH 28/46] added another command --- src/main/c/Georg/tictactoe.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 8501c52..de5401b 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -27,5 +27,7 @@ struct ticTacToe createTicTacToe() { int handeCommand( char* input ){ if( strcmp(input, "start game") == 0 ){ return 1; + }else if( strcmp(input, "start menu") == 0 ){ + return 0; } } \ No newline at end of file From 5dc45b9943bb3a8944beacb0c3016663c3c96b03 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:06:36 +0100 Subject: [PATCH 29/46] added a test case for the command --- src/test/c/Georg/test_tictactoe.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 8984fb8..102ef7a 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -59,6 +59,18 @@ void test_command_startGame(void){ // act int actualState = handeCommand( input ); + // assert + TEST_ASSERT_EQUAL_INT( expectedState, actualState ); +} + +void test_command_startMenu(void){ + // arrange + int expectedState = 0; + char* input = "start menu"; + + // act + int actualState = handeCommand( input ); + // assert TEST_ASSERT_EQUAL_INT( expectedState, actualState ); } \ No newline at end of file From e52cfccaa0bd45aa3a955ce57fe21eef3fe83bfa Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:07:11 +0100 Subject: [PATCH 30/46] refactoring: handleCommand --- src/main/c/Georg/tictactoe.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index de5401b..a5955ee 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -25,9 +25,9 @@ struct ticTacToe createTicTacToe() { } int handeCommand( char* input ){ - if( strcmp(input, "start game") == 0 ){ - return 1; - }else if( strcmp(input, "start menu") == 0 ){ + if( strcmp(input, "start menu") == 0 ){ return 0; + }else if( strcmp(input, "start game") == 0 ){ + return 1; } } \ No newline at end of file From 04e8045c7c42366aa21b5e85bfa43a9462718cac Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:08:48 +0100 Subject: [PATCH 31/46] refactoring: typo --- src/main/c/Georg/tictactoe.c | 2 +- src/main/c/Georg/tictactoe.h | 2 +- src/test/c/Georg/test_tictactoe.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index a5955ee..c3b5525 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -24,7 +24,7 @@ struct ticTacToe createTicTacToe() { return newGame; } -int handeCommand( char* input ){ +int handleCommand( char* input ){ if( strcmp(input, "start menu") == 0 ){ return 0; }else if( strcmp(input, "start game") == 0 ){ diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 1500ee2..d054a66 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -8,6 +8,6 @@ struct ticTacToe{ char* getWelcomeMessage(); char* getRulesMessage(); struct ticTacToe createTicTacToe(); -int handeCommand( char* input ); +int handleCommand( char* input ); #endif //TICTACTOE_H diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 102ef7a..546dc88 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -57,7 +57,7 @@ void test_command_startGame(void){ char* input = "start game"; // act - int actualState = handeCommand( input ); + int actualState = handleCommand( input ); // assert TEST_ASSERT_EQUAL_INT( expectedState, actualState ); @@ -69,7 +69,7 @@ void test_command_startMenu(void){ char* input = "start menu"; // act - int actualState = handeCommand( input ); + int actualState = handleCommand( input ); // assert TEST_ASSERT_EQUAL_INT( expectedState, actualState ); From 1d25e840019c6b077685d0ecf3df76f132f4be63 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:31:08 +0100 Subject: [PATCH 32/46] added commands and their functions --- src/main/c/Georg/tictactoe.c | 10 ++++++++++ src/main/c/Georg/tictactoe.h | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index c3b5525..362855a 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -30,4 +30,14 @@ int handleCommand( char* input ){ }else if( strcmp(input, "start game") == 0 ){ return 1; } +} + +int startMenu(){ + + return 0; +} + +int startGame(){ + + return 0; } \ No newline at end of file diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index d054a66..8ce9344 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -5,9 +5,16 @@ struct ticTacToe{ int currentState; }; +// Typdefinition für einen Funktionszeiger +typedef int (*commandFunction)(); + char* getWelcomeMessage(); char* getRulesMessage(); struct ticTacToe createTicTacToe(); int handleCommand( char* input ); +/* commands */ +int startMenu(); +int startGame(); + #endif //TICTACTOE_H From 0516a82cb0ffb47ef0edcb92475b9066f1db892c Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:31:34 +0100 Subject: [PATCH 33/46] added a function that create the command list --- src/main/c/Georg/tictactoe.c | 22 ++++++++++++++++++++-- src/main/c/Georg/tictactoe.h | 5 +++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 362855a..89ea119 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -32,12 +32,30 @@ int handleCommand( char* input ){ } } +struct command* createCommands(){ + struct command commands[] = { + {"\"start menu\" - startet das menu", startMenu} + }; + + size_t numCommands = sizeof(commands) / sizeof(commands[0]); + + // Dynamischen Speicher für das Array von commands reservieren + struct command* ptrCommands = (struct command*)malloc(numCommands * sizeof(struct command)); + + // Über das lokale Array iterieren und Daten kopieren + for (size_t i = 0; i < numCommands; i++) { + ptrCommands[i] = commands[i]; + } + + return ptrCommands; +} + int startMenu(){ - return 0; + return 1; } int startGame(){ - return 0; + return 1; } \ No newline at end of file diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 8ce9344..a0c5870 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -8,6 +8,11 @@ struct ticTacToe{ // Typdefinition für einen Funktionszeiger typedef int (*commandFunction)(); +struct command{ + char* description; + commandFunction fun; +}; + char* getWelcomeMessage(); char* getRulesMessage(); struct ticTacToe createTicTacToe(); From 1ed3ba3ed6f5930095d84466d5cbe4fe815433a8 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:31:57 +0100 Subject: [PATCH 34/46] added a test case for the command list creation --- src/main/c/Georg/tictactoe.h | 1 + src/test/c/Georg/test_tictactoe.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index a0c5870..d8c22f2 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -17,6 +17,7 @@ char* getWelcomeMessage(); char* getRulesMessage(); struct ticTacToe createTicTacToe(); int handleCommand( char* input ); +struct command* createCommands(); /* commands */ int startMenu(); diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 546dc88..fa1e452 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -73,4 +73,17 @@ void test_command_startMenu(void){ // assert TEST_ASSERT_EQUAL_INT( expectedState, actualState ); +} + +void test_createCommandlist(void){ + // arrange + struct command* commands = NULL; + + // act + commands = createCommands(); + + size_t arraySize = sizeof(commands) / sizeof(commands[0]); + for (size_t i = 0; i < arraySize; i++) { + TEST_ASSERT_EQUAL_INT( 1, commands[i].fun() ); + } } \ No newline at end of file From 5eacb3f4f09ed88dd3f67609d02d5e48579b5093 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:32:19 +0100 Subject: [PATCH 35/46] added a onother command --- src/main/c/Georg/tictactoe.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 89ea119..bff2f1c 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -34,7 +34,8 @@ int handleCommand( char* input ){ struct command* createCommands(){ struct command commands[] = { - {"\"start menu\" - startet das menu", startMenu} + {"\"start menu\" - startet das menu", startMenu}, + {"\"start game\" - startet das spiel", startGame} }; size_t numCommands = sizeof(commands) / sizeof(commands[0]); From b7f48f25aa4d5397b2841663ddb17d9f79b64c00 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:32:49 +0100 Subject: [PATCH 36/46] added an id to gameCommands --- src/main/c/Georg/tictactoe.c | 4 ++-- src/main/c/Georg/tictactoe.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index bff2f1c..6eaa000 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -34,8 +34,8 @@ int handleCommand( char* input ){ struct command* createCommands(){ struct command commands[] = { - {"\"start menu\" - startet das menu", startMenu}, - {"\"start game\" - startet das spiel", startGame} + { 1, "\"start menu\" - startet das menu", startMenu}, + { 2, "\"start game\" - startet das spiel", startGame} }; size_t numCommands = sizeof(commands) / sizeof(commands[0]); diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index d8c22f2..077ef48 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -9,6 +9,7 @@ struct ticTacToe{ typedef int (*commandFunction)(); struct command{ + int id; char* description; commandFunction fun; }; From 905f0d21bf285d2172e1f99728302356be414198 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:33:19 +0100 Subject: [PATCH 37/46] added a function that calls commands by their id --- src/main/c/Georg/tictactoe.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 6eaa000..7c09e45 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -51,6 +51,18 @@ struct command* createCommands(){ return ptrCommands; } +commandFunction getCommandById( struct command* commands, int id ){ + commandFunction result = NULL; + size_t arraySize = sizeof(*commands) / sizeof(commands[0]); + for (size_t i = 0; i < arraySize; i++) { + if( commands[i].id == id ){ + result = commands[i].fun; + break; + } + } + return result; +} + int startMenu(){ return 1; From 66aba9dc6824828334ed76d3256d942ff5143fed Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:33:39 +0100 Subject: [PATCH 38/46] added a test case for the command by id caller --- src/main/c/Georg/tictactoe.h | 1 + src/test/c/Georg/test_tictactoe.c | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 077ef48..19f2f6e 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -21,6 +21,7 @@ int handleCommand( char* input ); struct command* createCommands(); /* commands */ +commandFunction getCommandById(struct command* commands, int id); int startMenu(); int startGame(); diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index fa1e452..7e40ecc 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -86,4 +86,16 @@ void test_createCommandlist(void){ for (size_t i = 0; i < arraySize; i++) { TEST_ASSERT_EQUAL_INT( 1, commands[i].fun() ); } +} + +void test_callCommandById(void){ + // arrange + struct command* commands = NULL; + + // act + commands = createCommands(); + commandFunction actualCommand = getCommandById( commands, 1); + + // assert + TEST_ASSERT_EQUAL_PTR( startMenu, actualCommand ); } \ No newline at end of file From b47ef9c1885ce8205577c4bca92311814d8031f3 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:34:09 +0100 Subject: [PATCH 39/46] fixed calculation --- src/test/c/Georg/test_tictactoe.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 7e40ecc..1842e5a 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -82,7 +82,7 @@ void test_createCommandlist(void){ // act commands = createCommands(); - size_t arraySize = sizeof(commands) / sizeof(commands[0]); + size_t arraySize = sizeof(*commands) / sizeof(commands[0]); for (size_t i = 0; i < arraySize; i++) { TEST_ASSERT_EQUAL_INT( 1, commands[i].fun() ); } From 84c21acaa723411a7a1bbb50c30324c503a1a15e Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:34:49 +0100 Subject: [PATCH 40/46] modified the command functions for testing purpose --- src/main/c/Georg/tictactoe.c | 14 ++++++++++---- src/main/c/Georg/tictactoe.h | 4 ++-- src/test/c/Georg/test_tictactoe.c | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 7c09e45..42df92f 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -63,12 +63,18 @@ commandFunction getCommandById( struct command* commands, int id ){ return result; } -int startMenu(){ +int startMenu( int code ){ + if( code == 1 ){ // command test + return 1; + } - return 1; + return 0; } -int startGame(){ +int startGame( int code ){ + if( code == 1 ){ // command test + return 1; + } - return 1; + return 0; } \ No newline at end of file diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 19f2f6e..3fa6555 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -22,7 +22,7 @@ struct command* createCommands(); /* commands */ commandFunction getCommandById(struct command* commands, int id); -int startMenu(); -int startGame(); +int startMenu( int code ); +int startGame( int code ); #endif //TICTACTOE_H diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 1842e5a..849ed61 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -84,7 +84,7 @@ void test_createCommandlist(void){ size_t arraySize = sizeof(*commands) / sizeof(commands[0]); for (size_t i = 0; i < arraySize; i++) { - TEST_ASSERT_EQUAL_INT( 1, commands[i].fun() ); + TEST_ASSERT_EQUAL_INT( 1, commands[i].fun(1) ); } } From 8bf2d9a6073ff41538d40bc78e0f6df4e484e6b5 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:35:14 +0100 Subject: [PATCH 41/46] added a user input in the menu --- src/main/c/Georg/tictactoe.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 42df92f..d01ae9f 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -68,6 +68,11 @@ int startMenu( int code ){ return 1; } + printf("Welcome to the menu!\n"); + char userInput[50]; + printf( ":" ); + scanf( "%s", userInput ); + return 0; } From 4340d64ed35beec428641229b8fc8a445484a848 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:35:40 +0100 Subject: [PATCH 42/46] some bugfix and add global vars --- src/main/c/Georg/tictactoe.c | 24 +++++++++++++++++++----- src/main/c/Georg/tictactoe.h | 3 +++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index d01ae9f..195a85e 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -4,7 +4,9 @@ #include "tictactoe.h" -struct ticTacToe TICTACTOE; +struct ticTacToe GAME; +struct command* COMMANDS; + char* getWelcomeMessage(){ return "Hallo und willkommen zu unserem TicTacToe Spiel. Anbei die Anleitung:\n"; @@ -29,6 +31,8 @@ int handleCommand( char* input ){ return 0; }else if( strcmp(input, "start game") == 0 ){ return 1; + }else{ + return -1; } } @@ -68,10 +72,20 @@ int startMenu( int code ){ return 1; } - printf("Welcome to the menu!\n"); - char userInput[50]; - printf( ":" ); - scanf( "%s", userInput ); + while( GAME.currentState == 0 ){ + char userInput[50]; + printf( ":" ); + scanf( "%s", &userInput ); + + int nextState = 0; + nextState = handleCommand(userInput); + + if( nextState == -1 ){ + printf("command not found!"); + }else{ + GAME.currentState = nextState; + } + } return 0; } diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 3fa6555..f957f9c 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -14,6 +14,9 @@ struct command{ commandFunction fun; }; +extern struct ticTacToe GAME; +extern struct command* COMMANDS; + char* getWelcomeMessage(); char* getRulesMessage(); struct ticTacToe createTicTacToe(); From d9a87d61f7cdabb9b6ca7cca3c328230da3e690b Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:35:57 +0100 Subject: [PATCH 43/46] add function for user input --- src/main/c/Georg/tictactoe.c | 20 ++++++++++++++------ src/main/c/Georg/tictactoe.h | 2 ++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 195a85e..9e12efe 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -67,18 +67,26 @@ commandFunction getCommandById( struct command* commands, int id ){ return result; } +char* getUserInput(){ + static char userInput[MAX_INPUT_LENGTH]; + printf( ":" ); + fgets(userInput, sizeof(userInput), stdin); + + size_t len = strlen(userInput); + if (len > 0 && userInput[len - 1] == '\n') { + userInput[len - 1] = '\0'; + } + + return userInput; +} + int startMenu( int code ){ if( code == 1 ){ // command test return 1; } while( GAME.currentState == 0 ){ - char userInput[50]; - printf( ":" ); - scanf( "%s", &userInput ); - - int nextState = 0; - nextState = handleCommand(userInput); + int nextState = handleCommand( getUserInput() ); if( nextState == -1 ){ printf("command not found!"); diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index f957f9c..0856e7b 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -1,6 +1,8 @@ #ifndef TICTACTOE_H #define TICTACTOE_H +#define MAX_INPUT_LENGTH 20 + struct ticTacToe{ int currentState; }; From c12ef1337f5a11bfbbbbd4f65a8a1b98b41d28b4 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:36:19 +0100 Subject: [PATCH 44/46] changed command test execution --- src/main/c/Georg/tictactoe.c | 4 ++-- src/main/c/Georg/tictactoe.h | 2 +- src/test/c/Georg/test_tictactoe.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 9e12efe..ce02785 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -81,7 +81,7 @@ char* getUserInput(){ } int startMenu( int code ){ - if( code == 1 ){ // command test + if( code == -1 ){ // command test return 1; } @@ -99,7 +99,7 @@ int startMenu( int code ){ } int startGame( int code ){ - if( code == 1 ){ // command test + if( code == -1 ){ // command test return 1; } diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 0856e7b..b5f7ba9 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -8,7 +8,7 @@ struct ticTacToe{ }; // Typdefinition für einen Funktionszeiger -typedef int (*commandFunction)(); +typedef int (*commandFunction)( int ); struct command{ int id; diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 849ed61..e45f18d 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -84,7 +84,7 @@ void test_createCommandlist(void){ size_t arraySize = sizeof(*commands) / sizeof(commands[0]); for (size_t i = 0; i < arraySize; i++) { - TEST_ASSERT_EQUAL_INT( 1, commands[i].fun(1) ); + TEST_ASSERT_EQUAL_INT( 1, commands[i].fun(-1) ); } } From be2cb960ce17707f53f1c11f1f4d97f433e77cd4 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:37:00 +0100 Subject: [PATCH 45/46] changed the command structure and make it global --- src/main/c/Georg/tictactoe.c | 48 +++++++++++++++---------------- src/main/c/Georg/tictactoe.h | 6 ++-- src/test/c/Georg/test_tictactoe.c | 23 +++++++++------ 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index ce02785..b280c07 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -5,7 +5,11 @@ #include "tictactoe.h" struct ticTacToe GAME; -struct command* COMMANDS; + +struct command COMMANDS[MAX_COMMANDS] = { + { 1, "\"start menu\" - startet das menu", startMenu}, + { 2, "\"start game\" - startet das spiel", startGame} +}; char* getWelcomeMessage(){ @@ -36,31 +40,13 @@ int handleCommand( char* input ){ } } -struct command* createCommands(){ - struct command commands[] = { - { 1, "\"start menu\" - startet das menu", startMenu}, - { 2, "\"start game\" - startet das spiel", startGame} - }; - - size_t numCommands = sizeof(commands) / sizeof(commands[0]); - - // Dynamischen Speicher für das Array von commands reservieren - struct command* ptrCommands = (struct command*)malloc(numCommands * sizeof(struct command)); - - // Über das lokale Array iterieren und Daten kopieren - for (size_t i = 0; i < numCommands; i++) { - ptrCommands[i] = commands[i]; - } - - return ptrCommands; -} - -commandFunction getCommandById( struct command* commands, int id ){ +commandFunction getCommandById( int id ){ commandFunction result = NULL; - size_t arraySize = sizeof(*commands) / sizeof(commands[0]); + size_t arraySize = sizeof(COMMANDS) / sizeof(COMMANDS[0]); for (size_t i = 0; i < arraySize; i++) { - if( commands[i].id == id ){ - result = commands[i].fun; + //printf( "%s", COMMANDS[i].description ); + if( COMMANDS[i].id == id ){ + result = COMMANDS[i].fun; break; } } @@ -85,6 +71,8 @@ int startMenu( int code ){ return 1; } + printf("Welcome to the menu!\n"); + while( GAME.currentState == 0 ){ int nextState = handleCommand( getUserInput() ); @@ -103,5 +91,17 @@ int startGame( int code ){ return 1; } + printf("Welcome to the game!\n"); + + while( GAME.currentState == 1 ){ + int nextState = handleCommand( getUserInput() ); + + if( nextState == -1 ){ + printf("command not found!"); + }else{ + GAME.currentState = nextState; + } + } + return 0; } \ No newline at end of file diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index b5f7ba9..32357be 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -2,6 +2,7 @@ #define TICTACTOE_H #define MAX_INPUT_LENGTH 20 +#define MAX_COMMANDS 3 struct ticTacToe{ int currentState; @@ -17,16 +18,15 @@ struct command{ }; extern struct ticTacToe GAME; -extern struct command* COMMANDS; +extern struct command COMMANDS[MAX_COMMANDS]; char* getWelcomeMessage(); char* getRulesMessage(); struct ticTacToe createTicTacToe(); int handleCommand( char* input ); -struct command* createCommands(); /* commands */ -commandFunction getCommandById(struct command* commands, int id); +commandFunction getCommandById(int id); int startMenu( int code ); int startGame( int code ); diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index e45f18d..a78f759 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -75,27 +75,34 @@ void test_command_startMenu(void){ TEST_ASSERT_EQUAL_INT( expectedState, actualState ); } -void test_createCommandlist(void){ +void test_checkCommandlist(void){ // arrange - struct command* commands = NULL; // act - commands = createCommands(); - size_t arraySize = sizeof(*commands) / sizeof(commands[0]); + size_t arraySize = sizeof(*COMMANDS) / sizeof(COMMANDS[0]); for (size_t i = 0; i < arraySize; i++) { - TEST_ASSERT_EQUAL_INT( 1, commands[i].fun(-1) ); + TEST_ASSERT_EQUAL_INT( 1, COMMANDS[i].fun(-1) ); } } + void test_callCommandById(void){ // arrange - struct command* commands = NULL; // act - commands = createCommands(); - commandFunction actualCommand = getCommandById( commands, 1); + commandFunction actualCommand = getCommandById( 1 ); // assert TEST_ASSERT_EQUAL_PTR( startMenu, actualCommand ); +} + +void test_callCommandById_startGame(void){ + // arrange + + // act + commandFunction actualCommand = getCommandById( 2); + + // assert + TEST_ASSERT_EQUAL_PTR( startGame, actualCommand ); } \ No newline at end of file From 2e183d826056e50413d2cee028ac28923b44c00b Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:52:36 +0100 Subject: [PATCH 46/46] add the tictactoe game to the game collection --- src/main/c/Georg/tictactoe.c | 23 +++++++++++++++++++++-- src/main/c/Georg/tictactoe.h | 6 ++++-- src/main/c/main.c | 10 ++++++---- src/test/c/Georg/test_tictactoe.c | 4 ++-- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index b280c07..4e0ee11 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -11,12 +11,31 @@ struct command COMMANDS[MAX_COMMANDS] = { { 2, "\"start game\" - startet das spiel", startGame} }; +void startTicTacToe(){ + printf( "%s\n", getWelcomeMessageTicTacToe() ); + printf( "%s\n\n", getRulesMessageTicTacToe() ); + + GAME = createTicTacToe(); + + while( GAME.currentState != -1 ){ + commandFunction command; + printf("search command!\n"); + command = getCommandById( GAME.currentState + 1); + + if( command != NULL) + command(0); + else{ + printf("command not found"); + return; + } + } +} -char* getWelcomeMessage(){ +char* getWelcomeMessageTicTacToe(){ return "Hallo und willkommen zu unserem TicTacToe Spiel. Anbei die Anleitung:\n"; } -char* getRulesMessage(){ +char* getRulesMessageTicTacToe(){ return "Das spiel wird über die Komandozeile gespielt.\n" "Jeder Spielzug ist eine Eingabe in die Konsole. Die enstsprechenden Befehle stehen jeweils unterhalb des Spielfelds.\n" "Um ein Zug zu tätigen musst du \"set x,y\" in die Konsole Eingeben. Die Koordinaten stehen dabei für Zeile und Spalte.\n" diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 32357be..315b22c 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -20,8 +20,10 @@ struct command{ extern struct ticTacToe GAME; extern struct command COMMANDS[MAX_COMMANDS]; -char* getWelcomeMessage(); -char* getRulesMessage(); + +void startTicTacToe(); +char* getWelcomeMessageTicTacToe(void); +char* getRulesMessageTicTacToe(void); struct ticTacToe createTicTacToe(); int handleCommand( char* input ); diff --git a/src/main/c/main.c b/src/main/c/main.c index cbaccdc..4dc37a9 100644 --- a/src/main/c/main.c +++ b/src/main/c/main.c @@ -13,6 +13,7 @@ #include #include "SchereSteinPapier.h" #include "hangman.h" +#include "tictactoe.h" void openInterface(); @@ -33,7 +34,8 @@ void openInterface() printf("\n\nHallo und willkommen bei unserer Spielesammlung!!!\n" "Du hast folgende Spiele zur Auswahl:\n\n" "1: Schere-Stein-Papier\n" - "2: Hangman\n"); + "2: Hangman\n" + "3: TicTacToe\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); @@ -49,10 +51,10 @@ void openInterface() case(2): hangman(); break; - /*case(3): - //Spiel() + case(3): + startTicTacToe(); break; - case(4): + /*case(4): //Spiel() break; case(5): diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index a78f759..2562299 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -18,7 +18,7 @@ void test_welcome_message(void){ char* expectedMessage = "Hallo und willkommen zu unserem TicTacToe Spiel. Anbei die Anleitung:\n"; // act - char* message = getWelcomeMessage(); + char* message = getWelcomeMessageTicTacToe(); // aassert TEST_ASSERT_EQUAL_STRING(expectedMessage, message); @@ -33,7 +33,7 @@ void test_rules_message(void){ "Mit dem Befehl \"rules\" kannst du diese Nachricht erneut aufrufen."; // act - char* message = getRulesMessage(); + char* message = getRulesMessageTicTacToe(); // assert TEST_ASSERT_EQUAL_STRING(expectedMessage, message);