From a19c2caa67b1f266404c1f50797aa93641d7076e Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 15:21:56 +0100 Subject: [PATCH 001/107] =?UTF-8?q?Schreiben=20der=20Empfangsnachricht=20u?= =?UTF-8?q?nd=20Implementation=20des=20GameLoops=20+=20Ab=C3=A4nderung=20d?= =?UTF-8?q?er=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 002/107] =?UTF-8?q?Implementation=20der=20zuf=C3=A4lligen?= =?UTF-8?q?=20Computerauswahl?= 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 003/107] 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 004/107] =?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 005/107] =?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 006/107] =?UTF-8?q?Implementation=20der=20Test=20f=C3=BCr?= =?UTF-8?q?=20den=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 007/107] =?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 008/107] =?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 009/107] =?UTF-8?q?refactoring:=20Einf=C3=BChren=20der=20k?= =?UTF-8?q?onstanten=20Variablen=20scissor,=20rock,=20paper=20f=C3=BCr=20d?= =?UTF-8?q?ie=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 010/107] =?UTF-8?q?refactoring:=20Verst=C3=A4ndlichere=20B?= =?UTF-8?q?enennung=20einiger=20Variablen:=20in=20schereSteinPapier():=20u?= =?UTF-8?q?ser->userChoice,=20com->comChoice,=20win->winNum;=20formale=20V?= =?UTF-8?q?ariable=20in=20calculateWinner():=20x->userSelection,=20y->comS?= =?UTF-8?q?election?= 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 011/107] =?UTF-8?q?=C3=84nderung=20Position=20der=20Inital?= =?UTF-8?q?isierung=20von=20srand()=20um=20die=20Zuf=C3=A4lligkeit=20zu=20?= =?UTF-8?q?garantieren?= 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 012/107] =?UTF-8?q?refactoring:=20Auslagern=20der=20Funkti?= =?UTF-8?q?onalit=C3=A4t=20zu=20den=20Funktionen=20selectCOMChoice(),=20ge?= =?UTF-8?q?tWelcomeMessage(),=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 013/107] =?UTF-8?q?=C3=9Cberarbeitung=20der=20Funktion=20p?= =?UTF-8?q?rintResult(),=20damit=20die=20Auswahl=20des=20Computers=20als?= =?UTF-8?q?=20Wort=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 014/107] 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 015/107] 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 016/107] =?UTF-8?q?refactoring:=20Auslagern=20von=20Funkti?= =?UTF-8?q?onalit=C3=A4t=20zu=20gameLoop()=20und=20getuserSelection(),=20u?= =?UTF-8?q?m=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 017/107] =?UTF-8?q?Anlegen=20der=20Dateien=20f=C3=BCr=20Ha?= =?UTF-8?q?ngman=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 13067a097c463aeba1a07bf328288a25a4405f1d Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:38:22 +0100 Subject: [PATCH 018/107] Empfangsnachricht von Hangman + Implementation des gameloops --- src/main/c/Tim/hangman.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index d49319b..e0417d1 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -1,7 +1,19 @@ #include "hangman.h" #include + void hangman() { - printf("Hello World!"); + char userSelection; + printf("Willkommen bei Hangman!!!\n" + "Per Zufall wird jede Runde ein Wort aus einem Pool ausgewaehlt. Gebe einen Buchstaben ein, von dem du vermutest,\ndass er in dem gesuchten Wort ist, sobald die Konsole dich dazu auffordert.\n" + "Fuer jede falsche Antwort kommst du dem Tod immer naeher, also waehle weise!\n" + "Mit der Auswahl 0 kommst du zurueck ins Hauptmenue\n\n"); + + do + { + printf("Bitte gib einen Buchstaben ein!\n"); + scanf(" %c", &userSelection); + printf("%c\n", userSelection); + }while(userSelection != '0'); } From 3b1b6bfc319bbb34b4386ac18978014aaff9d3ab Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:39:21 +0100 Subject: [PATCH 019/107] =?UTF-8?q?Hinzuf=C3=BCgen=20der=20Liste=20der=20m?= =?UTF-8?q?=C3=B6glichen=20W=C3=B6rtern?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index e0417d1..81fa8ce 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -1,5 +1,12 @@ #include "hangman.h" #include +#include + +char wordlist[10][30] = { + "Kartoffel", "Zigarette", "Haus", "Fenster", "Kartenleseettiketiergerät", + "Kleiderschrank", "Schnee","Wasserhahn", + "Fernbedienung", "Computertastatur" +}; void hangman() From b945065a1a00784d55b5544e510d367aa75e1f1c Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:40:48 +0100 Subject: [PATCH 020/107] Implementation der Zufallsauswahl des gesuchten Wortes aus der Liste --- src/main/c/Tim/hangman.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 81fa8ce..6704dee 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -1,9 +1,11 @@ #include "hangman.h" #include #include +#include +#include char wordlist[10][30] = { - "Kartoffel", "Zigarette", "Haus", "Fenster", "Kartenleseettiketiergerät", + "Kartoffel", "Zigarette", "Haus", "Fenster", "Kartenleseettiketiergeraet", "Kleiderschrank", "Schnee","Wasserhahn", "Fernbedienung", "Computertastatur" }; @@ -11,6 +13,7 @@ char wordlist[10][30] = { void hangman() { + srand(time(NULL)); char userSelection; printf("Willkommen bei Hangman!!!\n" "Per Zufall wird jede Runde ein Wort aus einem Pool ausgewaehlt. Gebe einen Buchstaben ein, von dem du vermutest,\ndass er in dem gesuchten Wort ist, sobald die Konsole dich dazu auffordert.\n" @@ -19,6 +22,13 @@ void hangman() do { + char guessWord[30]; + int length; + strcpy(guessWord,wordlist[rand() % 10]); + length = strlen(guessWord); + printf("%s %d\n",guessWord, length); + + printf("Bitte gib einen Buchstaben ein!\n"); scanf(" %c", &userSelection); printf("%c\n", userSelection); From 0ace76ce85a17b25edcf0863396d33fa43172585 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:42:01 +0100 Subject: [PATCH 021/107] =?UTF-8?q?Hinzuf=C3=BCgen=20des=20Displaywortes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 6704dee..df569e2 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -23,11 +23,23 @@ void hangman() do { char guessWord[30]; + char displayWord[30]; int length; strcpy(guessWord,wordlist[rand() % 10]); length = strlen(guessWord); printf("%s %d\n",guessWord, length); - + for (int i = 0; i <= length; i++) + { + if(i Date: Thu, 25 Jan 2024 19:43:28 +0100 Subject: [PATCH 022/107] refactoring: Definition der konstanten Variablen LISTSIZE MAX_WORD_LENGTH und ersetzen an den jeweiligen Stellen --- src/main/c/Tim/hangman.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index df569e2..0e63451 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -4,7 +4,10 @@ #include #include -char wordlist[10][30] = { +#define LISTSIZE 10 +#define MAX_WORD_LENGTH 30 + +char wordlist[LISTSIZE][MAX_WORD_LENGTH] = { "Kartoffel", "Zigarette", "Haus", "Fenster", "Kartenleseettiketiergeraet", "Kleiderschrank", "Schnee","Wasserhahn", "Fernbedienung", "Computertastatur" @@ -22,10 +25,10 @@ void hangman() do { - char guessWord[30]; - char displayWord[30]; + char guessWord[MAX_WORD_LENGTH]; + char displayWord[MAX_WORD_LENGTH]; int length; - strcpy(guessWord,wordlist[rand() % 10]); + strcpy(guessWord,wordlist[rand() % LISTSIZE]); length = strlen(guessWord); printf("%s %d\n",guessWord, length); for (int i = 0; i <= length; i++) From b97858be05409df3c6e064206652d402c76c5c95 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:44:33 +0100 Subject: [PATCH 023/107] =?UTF-8?q?refactoring:=20Auslagern=20von=20Funkti?= =?UTF-8?q?onalit=C3=A4t=20um=20einen=20return-Wert=20f=C3=BCr=20Test=20zu?= =?UTF-8?q?=20erhalten=20+=20Separieren=20der=20Variablendeklaration=20von?= =?UTF-8?q?=20guessWord=20und=20displayWord=20f=C3=BCr=20erh=C3=B6hte=20?= =?UTF-8?q?=C3=9Cbersichtlichkeit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 0e63451..974f5b6 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -6,6 +6,8 @@ #define LISTSIZE 10 #define MAX_WORD_LENGTH 30 +char* getWordFromList(int); +int getParameters(int, char*); char wordlist[LISTSIZE][MAX_WORD_LENGTH] = { "Kartoffel", "Zigarette", "Haus", "Fenster", "Kartenleseettiketiergeraet", @@ -26,11 +28,12 @@ void hangman() do { char guessWord[MAX_WORD_LENGTH]; - char displayWord[MAX_WORD_LENGTH]; int length; - strcpy(guessWord,wordlist[rand() % LISTSIZE]); + strcpy(guessWord,getWordFromList(rand() % LISTSIZE)); length = strlen(guessWord); printf("%s %d\n",guessWord, length); + + char displayWord[MAX_WORD_LENGTH]; for (int i = 0; i <= length; i++) { if(i Date: Thu, 25 Jan 2024 19:46:22 +0100 Subject: [PATCH 024/107] =?UTF-8?q?Implementation=20der=20Tests=20f=C3=BCr?= =?UTF-8?q?=20ein=20g=C3=BCltiges=20Parameter=20bei=20der=20Funktion=20get?= =?UTF-8?q?WordFromList?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 3 +-- src/main/c/Tim/hangman.h | 1 + src/test/c/Tim/test_hangman.c | 23 +++++++++++++++++++---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 974f5b6..33293be 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -6,8 +6,7 @@ #define LISTSIZE 10 #define MAX_WORD_LENGTH 30 -char* getWordFromList(int); -int getParameters(int, char*); + char wordlist[LISTSIZE][MAX_WORD_LENGTH] = { "Kartoffel", "Zigarette", "Haus", "Fenster", "Kartenleseettiketiergeraet", diff --git a/src/main/c/Tim/hangman.h b/src/main/c/Tim/hangman.h index cd17d02..0995129 100644 --- a/src/main/c/Tim/hangman.h +++ b/src/main/c/Tim/hangman.h @@ -2,5 +2,6 @@ #define HANGMAN_H void hangman(); +char* getWordFromList(int); #endif diff --git a/src/test/c/Tim/test_hangman.c b/src/test/c/Tim/test_hangman.c index 2afdf17..6ae6216 100644 --- a/src/test/c/Tim/test_hangman.c +++ b/src/test/c/Tim/test_hangman.c @@ -1,5 +1,6 @@ #include "hangman.h" #include "unity.h" +#include @@ -13,12 +14,26 @@ void tearDown(void) } -void test_ceedling_functionality() +void test_getWordFromList_Kartoffel_0() { //arrange - int expectedResult = 0; + int pos = 0; + char expectedResult[] = "Kartoffel"; //act - int actualResult = 0; + char actualResult[30]; + strcpy(actualResult,getWordFromList(pos)); //assert - TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); + TEST_ASSERT_EQUAL_STRING(expectedResult, actualResult); +} + +void test_getWordFromList_Kleiderschrank_5() +{ + //arrange + int pos = 5; + char expectedResult[] = "Kleiderschrank"; + //act + char actualResult[30]; + strcpy(actualResult,getWordFromList(pos)); + //assert + TEST_ASSERT_EQUAL_STRING(expectedResult, actualResult); } \ No newline at end of file From 0fb0f028215e0b0539d13a465918bcbf0333fbc9 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:47:53 +0100 Subject: [PATCH 025/107] =?UTF-8?q?Test=20f=C3=BCr=20zu=20gro=C3=9Fe=20Wer?= =?UTF-8?q?t=C3=BCbergabe=20bei=20getWordFromList=20+=20Anpassung=20im=20P?= =?UTF-8?q?roduktivCode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 9 ++++++++- src/test/c/Tim/test_hangman.c | 13 +++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 33293be..0f7c94d 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -55,6 +55,13 @@ void hangman() char* getWordFromList(int x) { - return wordlist[x]; + if(x Date: Thu, 25 Jan 2024 19:49:31 +0100 Subject: [PATCH 026/107] =?UTF-8?q?Einbauen=20Abbruchbedingung,=20dass=20d?= =?UTF-8?q?as=20Wort=20erraten=20wurde=20+=20Hinzuf=C3=BCgen=20der=20Testf?= =?UTF-8?q?=C3=A4lle=20f=C3=BCr=20ungleich=20und=20gleich?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 22 +++++++++++++++++++--- src/main/c/Tim/hangman.h | 2 ++ src/test/c/Tim/test_hangman.c | 24 +++++++++++++++++++++++- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 0f7c94d..9af39d7 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -3,6 +3,7 @@ #include #include #include +#include #define LISTSIZE 10 #define MAX_WORD_LENGTH 30 @@ -46,9 +47,13 @@ void hangman() } printf("%s\n", displayWord); - printf("Bitte gib einen Buchstaben ein!\n"); - scanf(" %c", &userSelection); - printf("%c\n", userSelection); + while(!wordGuessed(guessWord, displayWord)) + { + printf("Bitte gib einen Buchstaben ein!\n"); + scanf(" %c", &userSelection); + printf("%c\n", userSelection); + } + }while(userSelection != '0'); } @@ -65,3 +70,14 @@ char* getWordFromList(int x) } } +bool wordGuessed(char x[], char y[]) +{ + if(strcmp(x,y) == 0) + { + return true; + } + else + { + return false; + } +} \ No newline at end of file diff --git a/src/main/c/Tim/hangman.h b/src/main/c/Tim/hangman.h index 0995129..d3160df 100644 --- a/src/main/c/Tim/hangman.h +++ b/src/main/c/Tim/hangman.h @@ -1,7 +1,9 @@ +#include #ifndef HANGMAN_H #define HANGMAN_H void hangman(); char* getWordFromList(int); +bool wordGuessed(char[],char[]); #endif diff --git a/src/test/c/Tim/test_hangman.c b/src/test/c/Tim/test_hangman.c index 074a1c5..dc449e0 100644 --- a/src/test/c/Tim/test_hangman.c +++ b/src/test/c/Tim/test_hangman.c @@ -1,6 +1,7 @@ #include "hangman.h" #include "unity.h" #include +#include @@ -49,4 +50,25 @@ void test_getWordFromList_IndexOutOfList() strcpy(actualResult,getWordFromList(pos)); //assert TEST_ASSERT_EQUAL_STRING(expectedResult, actualResult); -} \ No newline at end of file +} + +void test_wonGame_wordGuessed() +{ + //arrange + char word1[] ="Kartoffel"; + char word2[] = "Kartoffel"; + //assert + TEST_ASSERT_TRUE(wordGuessed(word1, word2)); +} + +void test_not_wordGuessed() +{ + //arrange + char word1[] ="Kartoffel"; + char word2[] ="Thunfisch"; + //assert + TEST_ASSERT_FALSE(wordGuessed(word1, word2)); +} + + + From dbd68d4a907b944ef5bfa6c29c64cca944833e6a Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:51:28 +0100 Subject: [PATCH 027/107] =?UTF-8?q?Schreiben=20der=20Tests=20zu=20dem=20Te?= =?UTF-8?q?stfall=20Gro=C3=9F-undKleinschreibung=20unterschiedlich=20f?= =?UTF-8?q?=C3=BCr=20wordGuessed()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/c/Tim/test_hangman.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/c/Tim/test_hangman.c b/src/test/c/Tim/test_hangman.c index dc449e0..3701b67 100644 --- a/src/test/c/Tim/test_hangman.c +++ b/src/test/c/Tim/test_hangman.c @@ -70,5 +70,13 @@ void test_not_wordGuessed() TEST_ASSERT_FALSE(wordGuessed(word1, word2)); } +void test_wordGuessed_differentCaps() +{ + //arrange + char word1[] ="Kartoffel"; + char word2[] ="karTOFFel"; + //assert + TEST_ASSERT_FALSE(wordGuessed(word1, word2)); +} From edc3b1f2df9143139d2c0db8cb04919cb76d7e16 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:53:27 +0100 Subject: [PATCH 028/107] Implementation der Funktion, dass bei passenden Buchstaben das displayWord erweitert wird. --- src/main/c/Tim/hangman.c | 27 ++++++++++++++++++++++++++- src/main/c/Tim/hangman.h | 1 + 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 9af39d7..43949e9 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -51,7 +51,9 @@ void hangman() { printf("Bitte gib einen Buchstaben ein!\n"); scanf(" %c", &userSelection); - printf("%c\n", userSelection); + + letterGuessed(userSelection, guessWord, length, displayWord); + printf("%s\n", displayWord); } }while(userSelection != '0'); @@ -74,10 +76,33 @@ bool wordGuessed(char x[], char y[]) { if(strcmp(x,y) == 0) { + printf("Du hast gewonnen!\n"); return true; } else { return false; } +} + + +bool letterGuessed(char x, char y[], int length, char ptr[]) +{ + int counter = 0; + for(int i = 0; i0) + { + return true; + } + else + { + return false; + } + } \ No newline at end of file diff --git a/src/main/c/Tim/hangman.h b/src/main/c/Tim/hangman.h index d3160df..9819b9a 100644 --- a/src/main/c/Tim/hangman.h +++ b/src/main/c/Tim/hangman.h @@ -5,5 +5,6 @@ void hangman(); char* getWordFromList(int); bool wordGuessed(char[],char[]); +bool letterGuessed(char, char[], int, char[]); #endif From c655a092fc2146d62c2e18d8b157737ada97dbb1 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:55:10 +0100 Subject: [PATCH 029/107] =?UTF-8?q?Erweiterung=20der=20Funktion=20letterGu?= =?UTF-8?q?essed(),=20sodass=20Gro=C3=9F-=20und=20Kleinschreibung=20regist?= =?UTF-8?q?riert=20wird?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 43949e9..951e977 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -4,6 +4,7 @@ #include #include #include +#include #define LISTSIZE 10 #define MAX_WORD_LENGTH 30 @@ -90,11 +91,38 @@ bool letterGuessed(char x, char y[], int length, char ptr[]) { int counter = 0; for(int i = 0; i0) { From cb0f5e3d5825b3a85b30a5d92a4bdcbd45b4d07d Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:56:30 +0100 Subject: [PATCH 030/107] =?UTF-8?q?Schreiben=20der=20Tests=20f=C3=BCr=20di?= =?UTF-8?q?e=20Funktion=20letterGuessed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/c/Tim/test_hangman.c | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/test/c/Tim/test_hangman.c b/src/test/c/Tim/test_hangman.c index 3701b67..e2b04da 100644 --- a/src/test/c/Tim/test_hangman.c +++ b/src/test/c/Tim/test_hangman.c @@ -80,3 +80,46 @@ void test_wordGuessed_differentCaps() } +void test_letterGuessed_differentCaps_small_big() +{ + //arrange + char x ='F'; + char y[] ="Kartoffel"; + int length = 9; + char z[] ="_________"; + //assert + TEST_ASSERT_TRUE(letterGuessed(x,y,length,z)); +} + +void test_letterGuessed_differentCaps_big_small() +{ + //arrange + char x ='k'; + char y[] ="Kartoffel"; + int length = 9; + char z[] ="_________"; + //assert + TEST_ASSERT_TRUE(letterGuessed(x,y,length,z)); +} +void test_letterGuessed_sameCaps_small() +{ + //arrange + char x ='f'; + char y[] ="Kartoffel"; + int length = 9; + char z[] ="_________"; + //assert + TEST_ASSERT_TRUE(letterGuessed(x,y,length,z)); +} + +void test_letterGuessed_sameCaps_big() +{ + //arrange + char x ='K'; + char y[] ="Kartoffel"; + int length = 9; + char z[] ="_________"; + //assert + TEST_ASSERT_TRUE(letterGuessed(x,y,length,z)); +} + From 89b622bb643e88503d30108e300dc187cccb106a Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 19:57:28 +0100 Subject: [PATCH 031/107] refactoring: Aufteilen der Funktion letterGuessed zu bool letterGuessed und changeLetter --- src/main/c/Tim/hangman.c | 44 +++++++++++++++++++++++++++++----------- src/main/c/Tim/hangman.h | 1 + 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 951e977..d77f46a 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -54,6 +54,7 @@ void hangman() scanf(" %c", &userSelection); letterGuessed(userSelection, guessWord, length, displayWord); + changeLetter(userSelection, guessWord, length, displayWord); printf("%s\n", displayWord); } @@ -90,6 +91,37 @@ bool wordGuessed(char x[], char y[]) bool letterGuessed(char x, char y[], int length, char ptr[]) { int counter = 0; + for(int i = 0; i0) + { + return true; + } + else + { + return false; + } + +} + + +void changeLetter(char x, char y[], int length, char ptr[]) +{ for(int i = 0; i0) - { - return true; } - else - { - return false; - } - } \ No newline at end of file diff --git a/src/main/c/Tim/hangman.h b/src/main/c/Tim/hangman.h index 9819b9a..ae68d97 100644 --- a/src/main/c/Tim/hangman.h +++ b/src/main/c/Tim/hangman.h @@ -6,5 +6,6 @@ void hangman(); char* getWordFromList(int); bool wordGuessed(char[],char[]); bool letterGuessed(char, char[], int, char[]); +void changeLetter(char, char[], int, char[]); #endif From 90f36edc96324344ee41a44829e6eb275479c8ae Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 20:33:11 +0100 Subject: [PATCH 032/107] =?UTF-8?q?Kleine=20Verbesserung=20des=20Spielverh?= =?UTF-8?q?altens=20(Verlassen=20durch=20Taste=200=20w=C3=A4hrend=20Spiel?= =?UTF-8?q?=20und=20nur=20einmaliges=20ausprinten=20bei=20doppelvorkommend?= =?UTF-8?q?en=20Buchstaben)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index d77f46a..8b7db3b 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -52,6 +52,10 @@ void hangman() { printf("Bitte gib einen Buchstaben ein!\n"); scanf(" %c", &userSelection); + if(userSelection == '0') + { + break; + } letterGuessed(userSelection, guessWord, length, displayWord); changeLetter(userSelection, guessWord, length, displayWord); @@ -110,6 +114,7 @@ bool letterGuessed(char x, char y[], int length, char ptr[]) } if(counter>0) { + printf("Dein gewaehlter Buchstabe %c war ein Treffer!\n", x); return true; } else @@ -135,7 +140,6 @@ void changeLetter(char x, char y[], int length, char ptr[]) { ptr[i] = x+32; } - printf("Dein gewaehlter Buchstabe %c war ein Treffer!\n", x); } } else @@ -149,7 +153,6 @@ void changeLetter(char x, char y[], int length, char ptr[]) else { ptr[i] = x; } - printf("Dein gewaehlter Buchstabe %c war ein Treffer!\n", x); } } } From e4def0a3a258310558eb52fb3f56ee90df92600d Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 20:34:49 +0100 Subject: [PATCH 033/107] =?UTF-8?q?Hinzuf=C3=BCgen=20der=20ASCII-Art=20f?= =?UTF-8?q?=C3=BCr=20die=20Hangmanfigur=20+=20Kontrolle=20durch=20=C3=BCbe?= =?UTF-8?q?rgangsfunktion=20drawHangman?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 63 +++++++++++++++++++++++++++++++++++++++- src/main/c/Tim/hangman.h | 1 + 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 8b7db3b..76b825c 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -16,6 +16,58 @@ char wordlist[LISTSIZE][MAX_WORD_LENGTH] = { "Fernbedienung", "Computertastatur" }; +char hangmanStages[7][100]={ + "+---+\n" + "| |\n" + "|\n" + "|\n" + "|\n" + "|\n" + "=========\n", + "+---+\n" + "| |\n" + "| O\n" + "|\n" + "|\n" + "|\n" + "=========\n", + "+---+\n" + "| |\n" + "| O\n" + "| |\n" + "|\n" + "|\n" + "=========\n", + "+---+\n" + "| |\n" + "| O\n" + "| /|\n" + "|\n" + "|\n" + "=========\n", + "+---+\n" + "| |\n" + "| O\n" + "| /|\\\n" + "|\n" + "|\n" + "=========\n", + "+---+\n" + "| |\n" + "| O\n" + "| /|\\\n" + "| /\n" + "|\n" + "=========\n", + "+---+\n" + "| |\n" + "| O\n" + "| /|\\\n" + "| / \\\n" + "|\n" + "=========\n", +}; + void hangman() { @@ -57,7 +109,10 @@ void hangman() break; } - letterGuessed(userSelection, guessWord, length, displayWord); + if(!letterGuessed(userSelection, guessWord, length, displayWord)) + { + drawHangman(); + } changeLetter(userSelection, guessWord, length, displayWord); printf("%s\n", displayWord); } @@ -156,4 +211,10 @@ void changeLetter(char x, char y[], int length, char ptr[]) } } } +} + +void drawHangman() +{ + for(int i = 0; i<7;i++) + printf("%s", hangmanStages[i]); } \ No newline at end of file diff --git a/src/main/c/Tim/hangman.h b/src/main/c/Tim/hangman.h index ae68d97..1a66ecb 100644 --- a/src/main/c/Tim/hangman.h +++ b/src/main/c/Tim/hangman.h @@ -7,5 +7,6 @@ char* getWordFromList(int); bool wordGuessed(char[],char[]); bool letterGuessed(char, char[], int, char[]); void changeLetter(char, char[], int, char[]); +void drawHangman(); #endif From 9709a768636810cded5b48cec7c8814ee5e41020 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 20:35:55 +0100 Subject: [PATCH 034/107] refactoring: Definieren der Konstanten STAGENUM und ASCII_ART_SIZE --- src/main/c/Tim/hangman.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 76b825c..7db0052 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -8,6 +8,8 @@ #define LISTSIZE 10 #define MAX_WORD_LENGTH 30 +#define STAGENUM 7 +#define ASCII_ART_SIZE 1000 char wordlist[LISTSIZE][MAX_WORD_LENGTH] = { @@ -16,7 +18,7 @@ char wordlist[LISTSIZE][MAX_WORD_LENGTH] = { "Fernbedienung", "Computertastatur" }; -char hangmanStages[7][100]={ +char hangmanStages[STAGENUM][ASCII_ART_SIZE]={ "+---+\n" "| |\n" "|\n" @@ -215,6 +217,6 @@ void changeLetter(char x, char y[], int length, char ptr[]) void drawHangman() { - for(int i = 0; i<7;i++) + for(int i = 0; i Date: Thu, 25 Jan 2024 20:37:18 +0100 Subject: [PATCH 035/107] =?UTF-8?q?Hinzuf=C3=BCgen,=20dass=20der=20Hangman?= =?UTF-8?q?=20bei=20falschen=20Raten=20erweitert=20wird?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 14 ++++++++------ src/main/c/Tim/hangman.h | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 7db0052..799ceba 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -84,11 +84,13 @@ void hangman() { char guessWord[MAX_WORD_LENGTH]; int length; + int countWrongGuess=0; strcpy(guessWord,getWordFromList(rand() % LISTSIZE)); length = strlen(guessWord); printf("%s %d\n",guessWord, length); char displayWord[MAX_WORD_LENGTH]; + drawHangman(countWrongGuess); for (int i = 0; i <= length; i++) { if(i Date: Thu, 25 Jan 2024 20:48:48 +0100 Subject: [PATCH 036/107] =?UTF-8?q?Entfernen=20eines=20ungenutzten=20forma?= =?UTF-8?q?len=20Parameters=20bei=20Funktion=20letterGuessed()=20und=20Hin?= =?UTF-8?q?zuf=C3=BCgen=20eines=20Counters,=20der=20die=20Anzahl=20der=20R?= =?UTF-8?q?estversuche=20angibt?= 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 | 2 +- src/test/c/Tim/test_hangman.c | 12 ++++-------- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 799ceba..1a52a87 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -113,9 +113,10 @@ void hangman() break; } - if(!letterGuessed(userSelection, guessWord, length, displayWord)) + if(!letterGuessed(userSelection, guessWord, length)) { countWrongGuess++; + printf("Der von dir getippte Buchstabe war falsch! Du hast noch %d Versuche.\n", 6-countWrongGuess); } changeLetter(userSelection, guessWord, length, displayWord); drawHangman(countWrongGuess); @@ -152,7 +153,7 @@ bool wordGuessed(char x[], char y[]) } -bool letterGuessed(char x, char y[], int length, char ptr[]) +bool letterGuessed(char x, char y[], int length) { int counter = 0; for(int i = 0; i0) { - printf("Dein gewaehlter Buchstabe %c war ein Treffer!\n", x); + printf("Dein gewaehlter Buchstabe %c war ein Treffer! Du hast noch %d Versuche.\n", x); return true; } else diff --git a/src/main/c/Tim/hangman.h b/src/main/c/Tim/hangman.h index a258422..9a47b14 100644 --- a/src/main/c/Tim/hangman.h +++ b/src/main/c/Tim/hangman.h @@ -5,7 +5,7 @@ void hangman(); char* getWordFromList(int); bool wordGuessed(char[],char[]); -bool letterGuessed(char, char[], int, char[]); +bool letterGuessed(char, char[], int); void changeLetter(char, char[], int, char[]); void drawHangman(int); diff --git a/src/test/c/Tim/test_hangman.c b/src/test/c/Tim/test_hangman.c index e2b04da..757ed29 100644 --- a/src/test/c/Tim/test_hangman.c +++ b/src/test/c/Tim/test_hangman.c @@ -86,9 +86,8 @@ void test_letterGuessed_differentCaps_small_big() char x ='F'; char y[] ="Kartoffel"; int length = 9; - char z[] ="_________"; //assert - TEST_ASSERT_TRUE(letterGuessed(x,y,length,z)); + TEST_ASSERT_TRUE(letterGuessed(x,y,length)); } void test_letterGuessed_differentCaps_big_small() @@ -97,9 +96,8 @@ void test_letterGuessed_differentCaps_big_small() char x ='k'; char y[] ="Kartoffel"; int length = 9; - char z[] ="_________"; //assert - TEST_ASSERT_TRUE(letterGuessed(x,y,length,z)); + TEST_ASSERT_TRUE(letterGuessed(x,y,length)); } void test_letterGuessed_sameCaps_small() { @@ -107,9 +105,8 @@ void test_letterGuessed_sameCaps_small() char x ='f'; char y[] ="Kartoffel"; int length = 9; - char z[] ="_________"; //assert - TEST_ASSERT_TRUE(letterGuessed(x,y,length,z)); + TEST_ASSERT_TRUE(letterGuessed(x,y,length)); } void test_letterGuessed_sameCaps_big() @@ -118,8 +115,7 @@ void test_letterGuessed_sameCaps_big() char x ='K'; char y[] ="Kartoffel"; int length = 9; - char z[] ="_________"; //assert - TEST_ASSERT_TRUE(letterGuessed(x,y,length,z)); + TEST_ASSERT_TRUE(letterGuessed(x,y,length)); } From ccf87e12cf994d48dc2702ef84bf34c4fa7043fc Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 20:52:12 +0100 Subject: [PATCH 037/107] Einbau der Abbruchbedingung bei zu viel falschen Tipps --- src/main/c/Tim/hangman.c | 25 ++++++++++++++++++------- src/main/c/Tim/hangman.h | 1 + 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 1a52a87..ab0255d 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -104,20 +104,20 @@ void hangman() } printf("\n\n%s\n", displayWord); - while(!wordGuessed(guessWord, displayWord)) + while(1) { printf("Bitte gib einen Buchstaben ein!\n"); scanf(" %c", &userSelection); - if(userSelection == '0') - { - break; - } if(!letterGuessed(userSelection, guessWord, length)) { countWrongGuess++; printf("Der von dir getippte Buchstabe war falsch! Du hast noch %d Versuche.\n", 6-countWrongGuess); } + if(userSelection == '0'||wordGuessed(guessWord, displayWord)||noTrysLeft(countWrongGuess)) + { + break; + } changeLetter(userSelection, guessWord, length, displayWord); drawHangman(countWrongGuess); printf("\n\n%s\n", displayWord); @@ -152,7 +152,6 @@ bool wordGuessed(char x[], char y[]) } } - bool letterGuessed(char x, char y[], int length) { int counter = 0; @@ -185,7 +184,6 @@ bool letterGuessed(char x, char y[], int length) } - void changeLetter(char x, char y[], int length, char ptr[]) { for(int i = 0; i Date: Thu, 25 Jan 2024 20:55:17 +0100 Subject: [PATCH 038/107] =?UTF-8?q?refactoring:=20Einf=C3=BChren=20der=20k?= =?UTF-8?q?onstanten=20Variable=20POSSIBLE=5FTRYS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index ab0255d..62f28ff 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -10,6 +10,7 @@ #define MAX_WORD_LENGTH 30 #define STAGENUM 7 #define ASCII_ART_SIZE 1000 +#define POSSIBLE_TRYS 6 char wordlist[LISTSIZE][MAX_WORD_LENGTH] = { @@ -87,7 +88,6 @@ void hangman() int countWrongGuess=0; strcpy(guessWord,getWordFromList(rand() % LISTSIZE)); length = strlen(guessWord); - printf("%s %d\n",guessWord, length); char displayWord[MAX_WORD_LENGTH]; drawHangman(countWrongGuess); @@ -112,7 +112,7 @@ void hangman() if(!letterGuessed(userSelection, guessWord, length)) { countWrongGuess++; - printf("Der von dir getippte Buchstabe war falsch! Du hast noch %d Versuche.\n", 6-countWrongGuess); + printf("Der von dir getippte Buchstabe war falsch! Du hast noch %d Versuche.\n", POSSIBLE_TRYS-countWrongGuess); } if(userSelection == '0'||wordGuessed(guessWord, displayWord)||noTrysLeft(countWrongGuess)) { @@ -224,7 +224,7 @@ void drawHangman(int x) bool noTrysLeft(int x) { - if(x == 6) + if(x == POSSIBLE_TRYS) { printf("Du hast verloren!\n\n"); return true; From 72cd0c6b44831a3749992ba0c7c66f9e543d8210 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 20:57:49 +0100 Subject: [PATCH 039/107] =?UTF-8?q?Einige=20kleine=20Komfortabilit=C3=A4ts?= =?UTF-8?q?=C3=A4nderung?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 14 +++++++------- src/main/c/Tim/hangman.h | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 62f28ff..49b7035 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -114,12 +114,12 @@ void hangman() countWrongGuess++; printf("Der von dir getippte Buchstabe war falsch! Du hast noch %d Versuche.\n", POSSIBLE_TRYS-countWrongGuess); } - if(userSelection == '0'||wordGuessed(guessWord, displayWord)||noTrysLeft(countWrongGuess)) + changeLetter(userSelection, guessWord, length, displayWord); + drawHangman(countWrongGuess); + if(userSelection == '0'||wordGuessed(guessWord, displayWord)||noTrysLeft(countWrongGuess,guessWord)) { break; } - changeLetter(userSelection, guessWord, length, displayWord); - drawHangman(countWrongGuess); printf("\n\n%s\n", displayWord); } @@ -143,7 +143,7 @@ bool wordGuessed(char x[], char y[]) { if(strcmp(x,y) == 0) { - printf("Du hast gewonnen!\n"); + printf("Du hast gewonnen!\nDas gesuchte Wort war \"%s\"\n\nHier hast du ein neues Wort zum erraten.\n\n",x); return true; } else @@ -174,7 +174,7 @@ bool letterGuessed(char x, char y[], int length) } if(counter>0) { - printf("Dein gewaehlter Buchstabe %c war ein Treffer! Du hast noch %d Versuche.\n", x); + printf("Dein gewaehlter Buchstabe %c war ein Treffer!\n", x); return true; } else @@ -222,11 +222,11 @@ void drawHangman(int x) printf("%s", hangmanStages[x]); } -bool noTrysLeft(int x) +bool noTrysLeft(int x, char y[]) { if(x == POSSIBLE_TRYS) { - printf("Du hast verloren!\n\n"); + printf("Du hast verloren!\n\nDas gesuchte Wort war \"%s\"\n\nHier hast du ein neues Wort zum erraten.\n\n",y); return true; } else diff --git a/src/main/c/Tim/hangman.h b/src/main/c/Tim/hangman.h index a2d13ab..380e5ab 100644 --- a/src/main/c/Tim/hangman.h +++ b/src/main/c/Tim/hangman.h @@ -8,6 +8,6 @@ bool wordGuessed(char[],char[]); bool letterGuessed(char, char[], int); void changeLetter(char, char[], int, char[]); void drawHangman(int); -bool noTrysLeft(int); +bool noTrysLeft(int,char[]); #endif From 3a2ef63aba64c4fef00d7f432fbc6f02b2ebfb7e Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 20:59:29 +0100 Subject: [PATCH 040/107] =?UTF-8?q?Erweiterung=20der=20Wortliste=20f=C3=BC?= =?UTF-8?q?r=20Hangman?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 11 ++++++++--- src/main/c/Tim/hangman.h | 1 + src/test/c/Tim/test_hangman.c | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 49b7035..96ecc80 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -6,7 +6,7 @@ #include #include -#define LISTSIZE 10 +#define LISTSIZE 30 #define MAX_WORD_LENGTH 30 #define STAGENUM 7 #define ASCII_ART_SIZE 1000 @@ -15,8 +15,13 @@ char wordlist[LISTSIZE][MAX_WORD_LENGTH] = { "Kartoffel", "Zigarette", "Haus", "Fenster", "Kartenleseettiketiergeraet", - "Kleiderschrank", "Schnee","Wasserhahn", - "Fernbedienung", "Computertastatur" + "Kleiderschrank", "Schnee","Wasserhahn","Fernbedienung", + "Computertastatur", "Verlies","Zucchini","lizenzieren", + "Portemonnaie","brillant","Rückgrat","Toilettenpapier", + "Dachpappe","Hund","Zwiebelsuppe","Zebra", + "Kruzifix","Anschnallgurt","Bügeleisen","Fliesenleger", + "Adventskranz","Weihnachtsbaum","Autoreifen","Waschbecken", + "Busfahrkarte" }; char hangmanStages[STAGENUM][ASCII_ART_SIZE]={ diff --git a/src/main/c/Tim/hangman.h b/src/main/c/Tim/hangman.h index 380e5ab..45feb3e 100644 --- a/src/main/c/Tim/hangman.h +++ b/src/main/c/Tim/hangman.h @@ -2,6 +2,7 @@ #ifndef HANGMAN_H #define HANGMAN_H + void hangman(); char* getWordFromList(int); bool wordGuessed(char[],char[]); diff --git a/src/test/c/Tim/test_hangman.c b/src/test/c/Tim/test_hangman.c index 757ed29..4e6d98e 100644 --- a/src/test/c/Tim/test_hangman.c +++ b/src/test/c/Tim/test_hangman.c @@ -43,7 +43,7 @@ void test_getWordFromList_Kleiderschrank_5() void test_getWordFromList_IndexOutOfList() { //arrange - int pos = 15; + int pos = 60; char expectedResult[] = "Index nicht vorhanden"; //act char actualResult[30]; From 1e643eb08a4ba0548bfc73098af3243d664463be Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 21:00:50 +0100 Subject: [PATCH 041/107] =?UTF-8?q?Verschieben=20der=20define=20Variablen?= =?UTF-8?q?=20in=20die=20.h-Datei,=20sodass=20sie=20ebenfalls=20f=C3=BCr?= =?UTF-8?q?=20die=20Testdatei=20verf=C3=BCgbar=20ist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 6 +----- src/main/c/Tim/hangman.h | 5 +++++ src/test/c/Tim/test_hangman.c | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 96ecc80..427f788 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -6,11 +6,7 @@ #include #include -#define LISTSIZE 30 -#define MAX_WORD_LENGTH 30 -#define STAGENUM 7 -#define ASCII_ART_SIZE 1000 -#define POSSIBLE_TRYS 6 + char wordlist[LISTSIZE][MAX_WORD_LENGTH] = { diff --git a/src/main/c/Tim/hangman.h b/src/main/c/Tim/hangman.h index 45feb3e..732f005 100644 --- a/src/main/c/Tim/hangman.h +++ b/src/main/c/Tim/hangman.h @@ -2,6 +2,11 @@ #ifndef HANGMAN_H #define HANGMAN_H +#define LISTSIZE 30 +#define MAX_WORD_LENGTH 30 +#define STAGENUM 7 +#define ASCII_ART_SIZE 1000 +#define POSSIBLE_TRYS 6 void hangman(); char* getWordFromList(int); diff --git a/src/test/c/Tim/test_hangman.c b/src/test/c/Tim/test_hangman.c index 4e6d98e..0ddb407 100644 --- a/src/test/c/Tim/test_hangman.c +++ b/src/test/c/Tim/test_hangman.c @@ -43,7 +43,7 @@ void test_getWordFromList_Kleiderschrank_5() void test_getWordFromList_IndexOutOfList() { //arrange - int pos = 60; + int pos = LISTSIZE+1; char expectedResult[] = "Index nicht vorhanden"; //act char actualResult[30]; From eaebe737cafdcf176bb49963dc233f9d78983776 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 21:02:48 +0100 Subject: [PATCH 042/107] refactoring: Auslagern der Empfangsnachricht aus dem GameLoop in Hangman --- src/main/c/Tim/hangman.c | 14 +++++++++----- src/main/c/Tim/hangman.h | 1 + 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 427f788..49ef297 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -77,10 +77,7 @@ void hangman() { srand(time(NULL)); char userSelection; - printf("Willkommen bei Hangman!!!\n" - "Per Zufall wird jede Runde ein Wort aus einem Pool ausgewaehlt. Gebe einen Buchstaben ein, von dem du vermutest,\ndass er in dem gesuchten Wort ist, sobald die Konsole dich dazu auffordert.\n" - "Fuer jede falsche Antwort kommst du dem Tod immer naeher, also waehle weise!\n" - "Mit der Auswahl 0 kommst du zurueck ins Hauptmenue\n\n"); + getWelcomeMessageHangman(); do { @@ -126,7 +123,14 @@ void hangman() }while(userSelection != '0'); } - +void getWelcomeMessageHangman() +{ + printf("Willkommen bei Hangman!!!\n\n"); + drawHangman(6); + printf("\nPer Zufall wird jede Runde ein Wort aus einem Pool ausgewaehlt. Gebe einen Buchstaben ein, von dem du vermutest, dass er in dem gesuchten Wort ist, sobald die Konsole dich dazu auffordert.\n" + "Fuer jede falsche Antwort kommst du dem Tod immer naeher, also waehle weise!\n" + "Mit der Auswahl 0 kommst du zurueck ins Hauptmenue\n\n"); +} char* getWordFromList(int x) { diff --git a/src/main/c/Tim/hangman.h b/src/main/c/Tim/hangman.h index 732f005..c29858a 100644 --- a/src/main/c/Tim/hangman.h +++ b/src/main/c/Tim/hangman.h @@ -9,6 +9,7 @@ #define POSSIBLE_TRYS 6 void hangman(); +void getWelcomeMessageHangman(); char* getWordFromList(int); bool wordGuessed(char[],char[]); bool letterGuessed(char, char[], int); From 2af82e0adc77a3302212f81a942c57c44e22b91c Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 21:03:45 +0100 Subject: [PATCH 043/107] =?UTF-8?q?Aufteilen=20der=20Abbruchbedingung,=20s?= =?UTF-8?q?odass=20bei=20Eingabe=20von=200=20nicht=20nochmal=20Hangman=20g?= =?UTF-8?q?ezeichnet=20wird=20+=20Hinzuf=C3=BCgen=20Abschiedsnachricht?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 49ef297..85050d1 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -106,7 +106,10 @@ void hangman() { printf("Bitte gib einen Buchstaben ein!\n"); scanf(" %c", &userSelection); - + if(userSelection == '0') + { + break; + } if(!letterGuessed(userSelection, guessWord, length)) { countWrongGuess++; @@ -114,7 +117,7 @@ void hangman() } changeLetter(userSelection, guessWord, length, displayWord); drawHangman(countWrongGuess); - if(userSelection == '0'||wordGuessed(guessWord, displayWord)||noTrysLeft(countWrongGuess,guessWord)) + if(wordGuessed(guessWord, displayWord)||noTrysLeft(countWrongGuess,guessWord)) { break; } @@ -122,6 +125,7 @@ void hangman() } }while(userSelection != '0'); + printf("Danke fuers Spielen! Auf Wiedersehen!\n"); } void getWelcomeMessageHangman() { From 3b7e8d14844447cbd84ace35f2e139e995cca184 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 21:04:48 +0100 Subject: [PATCH 044/107] =?UTF-8?q?Hinzuf=C3=BCgen=20der=20Tests=20f=C3=BC?= =?UTF-8?q?r=20noTrysLeft()=20+=20Anpassung=20im=20Produktivcode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 2 +- src/test/c/Tim/test_hangman.c | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 85050d1..2c534e3 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -233,7 +233,7 @@ void drawHangman(int x) bool noTrysLeft(int x, char y[]) { - if(x == POSSIBLE_TRYS) + if(x >= POSSIBLE_TRYS) { printf("Du hast verloren!\n\nDas gesuchte Wort war \"%s\"\n\nHier hast du ein neues Wort zum erraten.\n\n",y); return true; diff --git a/src/test/c/Tim/test_hangman.c b/src/test/c/Tim/test_hangman.c index 0ddb407..a3c54c6 100644 --- a/src/test/c/Tim/test_hangman.c +++ b/src/test/c/Tim/test_hangman.c @@ -119,3 +119,30 @@ void test_letterGuessed_sameCaps_big() TEST_ASSERT_TRUE(letterGuessed(x,y,length)); } +void test_noTrysLeft_x_equals_POSSIBLE_TRYS() +{ + //arrange + char x = POSSIBLE_TRYS; + char y[] ="Kartoffel"; + //assert + TEST_ASSERT_TRUE(noTrysLeft(x, y)); +} + +void test_noTrysLeft_x_lower_POSSIBLE_TRYS() +{ + //arrange + char x = POSSIBLE_TRYS-2; + char y[] ="Kartoffel"; + //assert + TEST_ASSERT_FALSE(noTrysLeft(x, y)); +} + +void test_noTrysLeft_x_higher_POSSIBLE_TRYS() +{ + //arrange + char x = POSSIBLE_TRYS+2; + char y[] ="Kartoffel"; + //assert + TEST_ASSERT_TRUE(noTrysLeft(x, y)); +} + From bd53927ab27b7c716a88c170626df8a545649f0d Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 21:05:56 +0100 Subject: [PATCH 045/107] =?UTF-8?q?Schreiben=20des=20Test=20f=C3=BCr=20unt?= =?UTF-8?q?erschiedliche=20Buchstaben=20bei=20letterGuessed()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 1 + src/test/c/Tim/test_hangman.c | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 2c534e3..f684076 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -127,6 +127,7 @@ void hangman() }while(userSelection != '0'); printf("Danke fuers Spielen! Auf Wiedersehen!\n"); } + void getWelcomeMessageHangman() { printf("Willkommen bei Hangman!!!\n\n"); diff --git a/src/test/c/Tim/test_hangman.c b/src/test/c/Tim/test_hangman.c index a3c54c6..bfbe9c6 100644 --- a/src/test/c/Tim/test_hangman.c +++ b/src/test/c/Tim/test_hangman.c @@ -108,6 +108,16 @@ void test_letterGuessed_sameCaps_small() //assert TEST_ASSERT_TRUE(letterGuessed(x,y,length)); } +void test_letterGuessed_differentLetter() +{ + //arrange + char x ='p'; + char y[] ="Kartoffel"; + int length = 9; + //assert + TEST_ASSERT_FALSE(letterGuessed(x,y,length)); +} + void test_letterGuessed_sameCaps_big() { From 37f32fef2bb247d51a08647b672bb624e73ad903 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 21:07:11 +0100 Subject: [PATCH 046/107] =?UTF-8?q?Erweiterung=20des=20Testfall=20untersch?= =?UTF-8?q?iedlicher=20Buchstabe=20bei=20letterGuessed()=20um=20Gro=C3=9F-?= =?UTF-8?q?=20und=20Kleinschreibung?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/c/Tim/test_hangman.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/test/c/Tim/test_hangman.c b/src/test/c/Tim/test_hangman.c index bfbe9c6..bc1048b 100644 --- a/src/test/c/Tim/test_hangman.c +++ b/src/test/c/Tim/test_hangman.c @@ -108,7 +108,7 @@ void test_letterGuessed_sameCaps_small() //assert TEST_ASSERT_TRUE(letterGuessed(x,y,length)); } -void test_letterGuessed_differentLetter() +void test_letterGuessed_differentLetter_small() { //arrange char x ='p'; @@ -117,6 +117,15 @@ void test_letterGuessed_differentLetter() //assert TEST_ASSERT_FALSE(letterGuessed(x,y,length)); } +void test_letterGuessed_differentLetter_big() +{ + //arrange + char x ='P'; + char y[] ="Kartoffel"; + int length = 9; + //assert + TEST_ASSERT_FALSE(letterGuessed(x,y,length)); +} void test_letterGuessed_sameCaps_big() From 43abfa0aa1ff887ee3bb0315a173c385698cc232 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 21:08:23 +0100 Subject: [PATCH 047/107] =?UTF-8?q?Erweiterung=20der=20Test=20f=C3=BCr=20g?= =?UTF-8?q?etWordFromList=20f=C3=BCr=20Index=20=C3=BCber=20und=20unter=20W?= =?UTF-8?q?ortliste=20+=20Anpassung=20im=20Produktivcode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 2 +- src/test/c/Tim/test_hangman.c | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index f684076..5f99432 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -139,7 +139,7 @@ void getWelcomeMessageHangman() char* getWordFromList(int x) { - if(x=0 && x Date: Thu, 25 Jan 2024 21:09:44 +0100 Subject: [PATCH 048/107] =?UTF-8?q?Hinzuf=C3=BCgen=20einer=20Weiterspielab?= =?UTF-8?q?frage=20nach=20Ende=20einer=20Spielrunde?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 21 ++++++++++++++++++--- src/main/c/Tim/hangman.h | 2 ++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 5f99432..d5c5255 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -123,7 +123,7 @@ void hangman() } printf("\n\n%s\n", displayWord); } - + userSelection = endGame(); }while(userSelection != '0'); printf("Danke fuers Spielen! Auf Wiedersehen!\n"); } @@ -153,7 +153,7 @@ bool wordGuessed(char x[], char y[]) { if(strcmp(x,y) == 0) { - printf("Du hast gewonnen!\nDas gesuchte Wort war \"%s\"\n\nHier hast du ein neues Wort zum erraten.\n\n",x); + printf("Du hast gewonnen!\nDas gesuchte Wort war \"%s\"\n\n",x); return true; } else @@ -236,11 +236,26 @@ bool noTrysLeft(int x, char y[]) { if(x >= POSSIBLE_TRYS) { - printf("Du hast verloren!\n\nDas gesuchte Wort war \"%s\"\n\nHier hast du ein neues Wort zum erraten.\n\n",y); + printf("Du hast verloren!\n\nDas gesuchte Wort war \"%s\"\n\n",y); return true; } else { return false; } +} + +char endGame() +{ + char userSelect; + endGameQuestionHangman(); + scanf(" %c", &userSelect); + + return userSelect; +} + + +void endGameQuestionHangman() +{ + printf("Moechtest du nochmal spielen?\n\nBeliebige Taste: Nochmal spielen\n 0 : Beenden\n"); } \ No newline at end of file diff --git a/src/main/c/Tim/hangman.h b/src/main/c/Tim/hangman.h index c29858a..f68abf8 100644 --- a/src/main/c/Tim/hangman.h +++ b/src/main/c/Tim/hangman.h @@ -16,5 +16,7 @@ bool letterGuessed(char, char[], int); void changeLetter(char, char[], int, char[]); void drawHangman(int); bool noTrysLeft(int,char[]); +char endGame(); +void endGameQuestionHangman(); #endif From 516049f1bda011542f2019c4d79e939f12cb3211 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 21:10:42 +0100 Subject: [PATCH 049/107] =?UTF-8?q?Hotfix:=20Bei=20W=C3=B6rtern=20mit=20kl?= =?UTF-8?q?einen=20Buchstaben=20werden=20die=20Buchstaben=20als=20klein=20?= =?UTF-8?q?=C3=BCbernommen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index d5c5255..8719211 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -201,7 +201,7 @@ void changeLetter(char x, char y[], int length, char ptr[]) { if (x == y[i]-32 || x == y[i] ) { - if(i ==0) + if(i ==0 && isupper(y[0])!=0) { ptr[i] = x; } @@ -215,7 +215,7 @@ void changeLetter(char x, char y[], int length, char ptr[]) { if (x == y[i]||x == y[i]+32) { - if(i == 0) + if(i == 0 && isupper(y[0])!=0) { ptr[i] = x-32; } From 64247b0c528e32e42c2e96e5729cad24f925a3f0 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 21:12:21 +0100 Subject: [PATCH 050/107] =?UTF-8?q?refactoring:=20Umbenennen=20der=20forma?= =?UTF-8?q?len=20Parameter=20von=20wordGuessed()=20zu=20verst=C3=A4ndliche?= =?UTF-8?q?ren=20Namen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 8719211..64e6ccb 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -149,11 +149,11 @@ char* getWordFromList(int x) } } -bool wordGuessed(char x[], char y[]) +bool wordGuessed(char guessWord[], char displayWord[]) { - if(strcmp(x,y) == 0) + if(strcmp(guessWord, displayWord) == 0) { - printf("Du hast gewonnen!\nDas gesuchte Wort war \"%s\"\n\n",x); + printf("Du hast gewonnen!\nDas gesuchte Wort war \"%s\"\n\n",guessWord); return true; } else From c086500f893e5736ba8bb4fc94806434526c56d0 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 21:13:26 +0100 Subject: [PATCH 051/107] =?UTF-8?q?refactoring:=20Umbenennen=20der=20forma?= =?UTF-8?q?len=20Parameter=20von=20letterGuessedzu=20verst=C3=A4ndlicheren?= =?UTF-8?q?=20Namen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 64e6ccb..040a085 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -162,20 +162,20 @@ bool wordGuessed(char guessWord[], char displayWord[]) } } -bool letterGuessed(char x, char y[], int length) +bool letterGuessed(char selectedLetter, char guessWord[], int length) { int counter = 0; for(int i = 0; i0) { - printf("Dein gewaehlter Buchstabe %c war ein Treffer!\n", x); + printf("Dein gewaehlter Buchstabe %c war ein Treffer!\n", selectedLetter); return true; } else From e6c9d0dcb67ddb3fbec7423e48eddde31dfc68a3 Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 21:14:39 +0100 Subject: [PATCH 052/107] =?UTF-8?q?refactoring:=20Umbenennen=20der=20forma?= =?UTF-8?q?len=20Parameter=20von=20changeLetter()=20zu=20verst=C3=A4ndlich?= =?UTF-8?q?eren=20Name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 040a085..228d02a 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -194,33 +194,33 @@ bool letterGuessed(char selectedLetter, char guessWord[], int length) } -void changeLetter(char x, char y[], int length, char ptr[]) +void changeLetter(char selectedLetter, char guessWord[], int length, char ptrDisplayWord[]) { for(int i = 0; i Date: Thu, 25 Jan 2024 21:15:54 +0100 Subject: [PATCH 053/107] =?UTF-8?q?refactoring:=20Umbenennen=20der=20forma?= =?UTF-8?q?len=20Parameter=20von=20drawHangman=20und=20noTrysLeft=20zu=20v?= =?UTF-8?q?erst=C3=A4ndlicheren=20Namen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Tim/hangman.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index 228d02a..08886d6 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -227,16 +227,16 @@ void changeLetter(char selectedLetter, char guessWord[], int length, char ptrDis } } -void drawHangman(int x) +void drawHangman(int wrongGuessCount) { - printf("%s", hangmanStages[x]); + printf("%s", hangmanStages[wrongGuessCount]); } -bool noTrysLeft(int x, char y[]) +bool noTrysLeft(int wrongGuessCount, char guessWord[]) { - if(x >= POSSIBLE_TRYS) + if(wrongGuessCount >= POSSIBLE_TRYS) { - printf("Du hast verloren!\n\nDas gesuchte Wort war \"%s\"\n\n",y); + printf("Du hast verloren!\n\nDas gesuchte Wort war \"%s\"\n\n", guessWord); return true; } else From 6fdccd7ef13f314b7829b0afecc2e6a4aaa0a10c Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Thu, 25 Jan 2024 21:30:20 +0100 Subject: [PATCH 054/107] 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 055/107] 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 056/107] 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 057/107] 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 058/107] 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 059/107] 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 060/107] 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 061/107] 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 062/107] 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 063/107] 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 064/107] 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 065/107] 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 066/107] 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 067/107] 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 068/107] 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 069/107] 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 070/107] 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 071/107] 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 072/107] 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 073/107] 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 074/107] 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 075/107] 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 076/107] 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 077/107] 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 078/107] 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 079/107] 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); From 80f56467ef1e57d933afe814de4c546379742ea0 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:00:07 +0100 Subject: [PATCH 080/107] bugfix: no console output during debugging --- src/main/c/Georg/tictactoe.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 4e0ee11..c62338a 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -12,6 +12,7 @@ struct command COMMANDS[MAX_COMMANDS] = { }; void startTicTacToe(){ + setbuf(stdout, 0); printf( "%s\n", getWelcomeMessageTicTacToe() ); printf( "%s\n\n", getRulesMessageTicTacToe() ); From 6f5ce4501e58cea1c4448e4fc5fa5c92f638ff1d Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:01:37 +0100 Subject: [PATCH 081/107] board initialize added --- src/main/c/Georg/tictactoe.c | 8 ++++++++ src/main/c/Georg/tictactoe.h | 3 +++ src/test/c/Georg/test_tictactoe.c | 23 ++++++++++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index c62338a..3f2bfdc 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -86,6 +86,14 @@ char* getUserInput(){ return userInput; } +void initializeBoard( bool board[3][3] ){ + for (int i = 0; i < 3; ++i) { + for (int j = 0; j < 3; ++j) { + board[i][j] = 0; + } + } +} + int startMenu( int code ){ if( code == -1 ){ // command test return 1; diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 315b22c..fb29d59 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 +#include + #define MAX_INPUT_LENGTH 20 #define MAX_COMMANDS 3 @@ -26,6 +28,7 @@ char* getWelcomeMessageTicTacToe(void); char* getRulesMessageTicTacToe(void); struct ticTacToe createTicTacToe(); int handleCommand( char* input ); +void initializeBoard( bool board[3][3] ); /* commands */ commandFunction getCommandById(int id); diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 2562299..dcc71be 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -105,4 +105,25 @@ void test_callCommandById_startGame(void){ // assert TEST_ASSERT_EQUAL_PTR( startGame, actualCommand ); -} \ No newline at end of file +} + + +void test_initializeBoard(void){ + // arrange + bool expectedBoard[3][3]={ + {0,0,0}, + {0,0,0}, + {0,0,0} + }; + + // act + bool actualBoard[3][3]; + initializeBoard(actualBoard); + + // assert + for (size_t i = 0; i < 3; i++) { + for (size_t ii = 0; ii < 3; ii++) { + TEST_ASSERT_EQUAL_INT(expectedBoard[i][ii], actualBoard[i][ii]); + } + } +} From c29eff6aa44618348cd1b266e4aa9b99cd80debd Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:02:31 +0100 Subject: [PATCH 082/107] add defenition for the board size --- src/main/c/Georg/tictactoe.c | 4 +++- src/main/c/Georg/tictactoe.h | 2 ++ src/test/c/Georg/test_tictactoe.c | 8 ++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 3f2bfdc..85a0fec 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -86,7 +86,7 @@ char* getUserInput(){ return userInput; } -void initializeBoard( bool board[3][3] ){ +void initializeBoard( bool board[BORAD_SIZE][BORAD_SIZE] ){ for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { board[i][j] = 0; @@ -120,6 +120,8 @@ int startGame( int code ){ } printf("Welcome to the game!\n"); + bool board[BORAD_SIZE][BORAD_SIZE]; + initializeBoard( board ); while( GAME.currentState == 1 ){ int nextState = handleCommand( getUserInput() ); diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index fb29d59..a73cb5e 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -5,9 +5,11 @@ #define MAX_INPUT_LENGTH 20 #define MAX_COMMANDS 3 +#define BORAD_SIZE 3 struct ticTacToe{ int currentState; + bool board[BORAD_SIZE][BORAD_SIZE]; }; // Typdefinition für einen Funktionszeiger diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index dcc71be..4413a8a 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -110,19 +110,19 @@ void test_callCommandById_startGame(void){ void test_initializeBoard(void){ // arrange - bool expectedBoard[3][3]={ + bool expectedBoard[BORAD_SIZE][BORAD_SIZE]={ {0,0,0}, {0,0,0}, {0,0,0} }; // act - bool actualBoard[3][3]; + bool actualBoard[BORAD_SIZE][BORAD_SIZE]; initializeBoard(actualBoard); // assert - for (size_t i = 0; i < 3; i++) { - for (size_t ii = 0; ii < 3; ii++) { + for (size_t i = 0; i < BORAD_SIZE; i++) { + for (size_t ii = 0; ii < BORAD_SIZE; ii++) { TEST_ASSERT_EQUAL_INT(expectedBoard[i][ii], actualBoard[i][ii]); } } From b84ed1c00abc55df56ffc3588163b6732ad3f0d1 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:03:22 +0100 Subject: [PATCH 083/107] added a function to print the game board --- src/main/c/Georg/tictactoe.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 85a0fec..7242d8a 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -4,6 +4,8 @@ #include "tictactoe.h" +void printBoard(); + struct ticTacToe GAME; struct command COMMANDS[MAX_COMMANDS] = { @@ -114,6 +116,31 @@ int startMenu( int code ){ return 0; } +void printBoard(){ + for( int i = 0; i < BORAD_SIZE*4+1; i++ ){ + printf("-"); + } + printf("\n"); + + for (int i = 0; i < 3; ++i) { + printf("| "); + for (int j = 0; j < 3; ++j) { + if( GAME.board[i][j] == true ) + printf( "X" ); + else + printf ( " " ); + + printf(" | "); + } + printf( "\n" ); + } + + for( int i = 0; i < BORAD_SIZE*4+1; i++ ){ + printf("-"); + } + printf("\n"); +} + int startGame( int code ){ if( code == -1 ){ // command test return 1; From ce7a6ba66cf811ce3eb2d3d311471c496568874f Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:04:21 +0100 Subject: [PATCH 084/107] add gameCommandHandler --- src/main/c/Georg/tictactoe.c | 8 ++++++++ src/main/c/Georg/tictactoe.h | 1 + src/test/c/Georg/test_tictactoe.c | 12 ++++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 7242d8a..614a3e7 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -96,6 +96,14 @@ void initializeBoard( bool board[BORAD_SIZE][BORAD_SIZE] ){ } } +int handleGameInput( char* input ){ + if( strstr(input, "set") != NULL ){ + return 1; + }else{ + return false; + } +} + int startMenu( int code ){ if( code == -1 ){ // command test return 1; diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index a73cb5e..923fb2e 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -31,6 +31,7 @@ char* getRulesMessageTicTacToe(void); struct ticTacToe createTicTacToe(); int handleCommand( char* input ); void initializeBoard( bool board[3][3] ); +int handleGameInput( char* input ); /* commands */ commandFunction getCommandById(int id); diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 4413a8a..702e4ae 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -127,3 +127,15 @@ void test_initializeBoard(void){ } } } + +void test_handleGameInput(void){ + // arrange + char* teststring = "set 3,4"; + int expectedState = 1; + + // act + int actualState = handleGameInput( teststring ); + + // assert + TEST_ASSERT_EQUAL_INT( expectedState, actualState ); +} From 4c17f9b836a61ebb73c731fe755c6e480e1a9faf Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:04:47 +0100 Subject: [PATCH 085/107] refactoring: test handleGameInput --- src/test/c/Georg/test_tictactoe.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 702e4ae..989ca12 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -131,11 +131,11 @@ void test_initializeBoard(void){ void test_handleGameInput(void){ // arrange char* teststring = "set 3,4"; - int expectedState = 1; + int expectedCommand = 1; // act int actualState = handleGameInput( teststring ); // assert - TEST_ASSERT_EQUAL_INT( expectedState, actualState ); + TEST_ASSERT_EQUAL_INT( expectedCommand, actualState ); } From 75e050c8d47291fb0495373ba5bee594ccafb017 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:05:44 +0100 Subject: [PATCH 086/107] add game command handler to the game --- src/main/c/Georg/tictactoe.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 614a3e7..4838534 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -100,7 +100,7 @@ int handleGameInput( char* input ){ if( strstr(input, "set") != NULL ){ return 1; }else{ - return false; + return -1; } } @@ -159,9 +159,12 @@ int startGame( int code ){ initializeBoard( board ); while( GAME.currentState == 1 ){ - int nextState = handleCommand( getUserInput() ); + char* input = getUserInput(); + int nextState = handleCommand( input ); + + if( nextState == -1 ){ // no stateCommand + int gameCommand = handleGameInput( input ); - if( nextState == -1 ){ printf("command not found!"); }else{ GAME.currentState = nextState; From 25c6e54e254fac1a11cf7df664f022fe941f585b Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:06:10 +0100 Subject: [PATCH 087/107] add function to parse the game commands --- src/main/c/Georg/tictactoe.c | 13 +++++++++++++ src/main/c/Georg/tictactoe.h | 1 + src/test/c/Georg/test_tictactoe.c | 14 ++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 4838534..fbee1de 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -149,6 +149,19 @@ void printBoard(){ printf("\n"); } +int* getMarkerParameters( char* input ){ + int* array = (int*)malloc(2 * sizeof(int)); + + int index = strchr(input, ',') - input; + int firstArgument = input[index-1] - '0'; + int secondArgument = input[index+1] - '0'; + + array[0] = firstArgument; + array[1] = secondArgument; + + return array; +} + int startGame( int code ){ if( code == -1 ){ // command test return 1; diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 923fb2e..7cf6b60 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -32,6 +32,7 @@ struct ticTacToe createTicTacToe(); int handleCommand( char* input ); void initializeBoard( bool board[3][3] ); int handleGameInput( char* input ); +int* getMarkerParameters(); /* commands */ commandFunction getCommandById(int id); diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 989ca12..5194ece 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -139,3 +139,17 @@ void test_handleGameInput(void){ // assert TEST_ASSERT_EQUAL_INT( expectedCommand, actualState ); } + +void test_getMarkerParameters(void){ + // arrange + char* teststring = "set 3,3"; + int expectedParams[2] = {3, 3}; + + // act + int* actualParams = getMarkerParameters( teststring ); + + // assert + for( int i = 0; i < 2; i++ ){ + TEST_ASSERT_EQUAL_INT( expectedParams[i], actualParams[i] ); + } +} \ No newline at end of file From 3a0e4ff19527afb8ab7152da07f278683d10cca0 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:06:26 +0100 Subject: [PATCH 088/107] added a game handler function --- src/main/c/Georg/tictactoe.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index fbee1de..1c602df 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -162,6 +162,30 @@ int* getMarkerParameters( char* input ){ return array; } +void handleGame(){ + char* input = getUserInput(); + int nextState = handleCommand( input ); + + // check commands, if no command found return for new Input + // gameCommands are saved and processed after this block + int gameCommand = -1; + if( nextState == -1 ){ // no stateCommand + gameCommand = handleGameInput( input ); + if( gameCommand == -1 ){ + printf("command not found!"); + return; + } + }else{ + GAME.currentState = nextState; + return; + } + + // gameCommand processing + if( gameCommand == 1 ) { // set marker in field + + } +} + int startGame( int code ){ if( code == -1 ){ // command test return 1; @@ -172,16 +196,7 @@ int startGame( int code ){ initializeBoard( board ); while( GAME.currentState == 1 ){ - char* input = getUserInput(); - int nextState = handleCommand( input ); - - if( nextState == -1 ){ // no stateCommand - int gameCommand = handleGameInput( input ); - - printf("command not found!"); - }else{ - GAME.currentState = nextState; - } + handleGame(); } return 0; From 955e91509fe84719159e7579db4596dcb89c4b3a Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:06:47 +0100 Subject: [PATCH 089/107] add function to set BoardMarker --- src/main/c/Georg/tictactoe.c | 12 +++++++++--- src/main/c/Georg/tictactoe.h | 1 + src/test/c/Georg/test_tictactoe.c | 19 ++++++++++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 1c602df..6888e7e 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -156,12 +156,16 @@ int* getMarkerParameters( char* input ){ int firstArgument = input[index-1] - '0'; int secondArgument = input[index+1] - '0'; - array[0] = firstArgument; - array[1] = secondArgument; + array[0] = firstArgument-1; + array[1] = secondArgument-1; return array; } +void setBoardMarker( bool board[BORAD_SIZE][BORAD_SIZE], int* params ){ + board[params[0]][params[1]] = 1; +} + void handleGame(){ char* input = getUserInput(); int nextState = handleCommand( input ); @@ -182,7 +186,9 @@ void handleGame(){ // gameCommand processing if( gameCommand == 1 ) { // set marker in field - + int* params = getMarkerParameters( input ); + setBoardMarker( GAME.board, params ); + free(params); } } diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 7cf6b60..7adc058 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -33,6 +33,7 @@ int handleCommand( char* input ); void initializeBoard( bool board[3][3] ); int handleGameInput( char* input ); int* getMarkerParameters(); +void setBoardMarker( bool board[BORAD_SIZE][BORAD_SIZE], int* params ); /* commands */ commandFunction getCommandById(int id); diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 5194ece..ffadb35 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -143,7 +143,7 @@ void test_handleGameInput(void){ void test_getMarkerParameters(void){ // arrange char* teststring = "set 3,3"; - int expectedParams[2] = {3, 3}; + int expectedParams[2] = {2, 2}; // act int* actualParams = getMarkerParameters( teststring ); @@ -152,4 +152,21 @@ void test_getMarkerParameters(void){ for( int i = 0; i < 2; i++ ){ TEST_ASSERT_EQUAL_INT( expectedParams[i], actualParams[i] ); } +} + +void test_setBoardFields(){ + // arrange + // arrange + bool board[3][3]={ + {0,0,0}, + {0,0,0}, + {0,0,0} + }; + int params[2] = {2,2}; + + // act + setBoardMarker( board, params ); + + // assert + TEST_ASSERT_EQUAL_INT( 1, board[2][2] ); } \ No newline at end of file From 7eed3ab448f84965e7c45aba78a019586758e884 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:07:04 +0100 Subject: [PATCH 090/107] add board print function --- src/main/c/Georg/tictactoe.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 6888e7e..43ff038 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -189,6 +189,8 @@ void handleGame(){ int* params = getMarkerParameters( input ); setBoardMarker( GAME.board, params ); free(params); + + printBoard(); } } @@ -201,6 +203,8 @@ int startGame( int code ){ bool board[BORAD_SIZE][BORAD_SIZE]; initializeBoard( board ); + printBoard(); + while( GAME.currentState == 1 ){ handleGame(); } From 9f1a33d710c6feec3e2e2d4567bec6bf1663da35 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:08:04 +0100 Subject: [PATCH 091/107] add player won logic --- src/main/c/Georg/tictactoe.c | 21 +++++++++++++++++++++ src/main/c/Georg/tictactoe.h | 1 + src/test/c/Georg/test_tictactoe.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 43ff038..45cb1e0 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -194,6 +194,27 @@ void handleGame(){ } } +bool playerHasWon( bool board[BORAD_SIZE][BORAD_SIZE]){ + bool player = 1; + // Überprüfe Zeilen und Spalten + for (int i = 0; i < 3; i++) { + // Überprüfe Zeilen + if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) || + // Überprüfe Spalten + (board[0][i] == player && board[1][i] == player && board[2][i] == player)) { + return true; // Spieler hat gewonnen + } + } + + // Überprüfe Diagonalen + if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) || + (board[0][2] == player && board[1][1] == player && board[2][0] == player)) { + return true; // Spieler hat gewonnen + } + + return false; +} + int startGame( int code ){ if( code == -1 ){ // command test return 1; diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 7adc058..efd1d1e 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -34,6 +34,7 @@ void initializeBoard( bool board[3][3] ); int handleGameInput( char* input ); int* getMarkerParameters(); void setBoardMarker( bool board[BORAD_SIZE][BORAD_SIZE], int* params ); +bool playerHasWon( bool board[BORAD_SIZE][BORAD_SIZE] ); /* commands */ commandFunction getCommandById(int id); diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index ffadb35..1881f89 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -169,4 +169,35 @@ void test_setBoardFields(){ // assert TEST_ASSERT_EQUAL_INT( 1, board[2][2] ); +} + +void test_checkOnWin_vertically(void){ + // arrange + bool board1[3][3]={ + {0,0,1}, + {0,0,1}, + {0,0,1} + }; + + bool board2[3][3]={ + {0,1,0}, + {0,1,0}, + {0,1,0} + }; + + bool board3[3][3]={ + {1,0,0}, + {1,0,0}, + {1,0,0} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + + // assert + TEST_ASSERT_EQUAL_INT( 1, result ); + TEST_ASSERT_EQUAL_INT( 1, result2 ); + TEST_ASSERT_EQUAL_INT( 1, result3 ); } \ No newline at end of file From f4640c343d6642e4f2e5dbc0d6544d1b5e906084 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:08:25 +0100 Subject: [PATCH 092/107] add a horizontal player won test --- src/test/c/Georg/test_tictactoe.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 1881f89..b8abc87 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -200,4 +200,35 @@ void test_checkOnWin_vertically(void){ TEST_ASSERT_EQUAL_INT( 1, result ); TEST_ASSERT_EQUAL_INT( 1, result2 ); TEST_ASSERT_EQUAL_INT( 1, result3 ); +} + +void test_negativ_checkOnWin_horizontally(void){ + // arrange + bool board1[3][3]={ + {1,0,0}, + {0,0,0}, + {0,0,0} + }; + + bool board2[3][3]={ + {0,0,0}, + {0,1,0}, + {0,0,0} + }; + + bool board3[3][3]={ + {0,0,0}, + {0,0,0}, + {0,0,1} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + + // assert + TEST_ASSERT_EQUAL_INT( 0, result ); + TEST_ASSERT_EQUAL_INT( 0, result2 ); + TEST_ASSERT_EQUAL_INT( 0, result3 ); } \ No newline at end of file From b2ddfbafb329fdc2288a14cb914d047577b1c455 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:08:38 +0100 Subject: [PATCH 093/107] add another horizontal player won test --- src/test/c/Georg/test_tictactoe.c | 33 ++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index b8abc87..3cd7a56 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -231,4 +231,35 @@ void test_negativ_checkOnWin_horizontally(void){ TEST_ASSERT_EQUAL_INT( 0, result ); TEST_ASSERT_EQUAL_INT( 0, result2 ); TEST_ASSERT_EQUAL_INT( 0, result3 ); -} \ No newline at end of file +} + +void test_negativ_checkOnWin_horizontally2(void){ + // arrange + bool board1[3][3]={ + {1,1,0}, + {0,0,0}, + {0,0,0} + }; + + bool board2[3][3]={ + {0,0,0}, + {0,1,1}, + {0,0,0} + }; + + bool board3[3][3]={ + {0,0,0}, + {0,0,0}, + {1,0,1} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + + // assert + TEST_ASSERT_EQUAL_INT( 0, result ); + TEST_ASSERT_EQUAL_INT( 0, result2 ); + TEST_ASSERT_EQUAL_INT( 0, result3 ); +} From 47cf6b43ed3975e0062603429c2b484ca5169ebc Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:08:51 +0100 Subject: [PATCH 094/107] add a vertical player won test --- src/test/c/Georg/test_tictactoe.c | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 3cd7a56..eed3cc1 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -263,3 +263,35 @@ void test_negativ_checkOnWin_horizontally2(void){ TEST_ASSERT_EQUAL_INT( 0, result2 ); TEST_ASSERT_EQUAL_INT( 0, result3 ); } + +void test_negativ_checkOnWin_vertically(void){ + // arrange + bool board1[3][3]={ + {1,0,0}, + {0,0,0}, + {0,0,0} + }; + + bool board2[3][3]={ + {0,0,0}, + {1,0,0}, + {0,0,0} + }; + + bool board3[3][3]={ + {0,0,0}, + {0,0,0}, + {1,0,0} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + + // assert + TEST_ASSERT_EQUAL_INT( 0, result ); + TEST_ASSERT_EQUAL_INT( 0, result2 ); + TEST_ASSERT_EQUAL_INT( 0, result3 ); +} + From 952568988f1af0f1a2b48a0771d9e7de1d7af4ed Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:09:09 +0100 Subject: [PATCH 095/107] add another vertical player won test --- src/test/c/Georg/test_tictactoe.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index eed3cc1..851eeb7 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -295,3 +295,33 @@ void test_negativ_checkOnWin_vertically(void){ TEST_ASSERT_EQUAL_INT( 0, result3 ); } +void test_negativ_checkOnWin_vertically2(void){ + // arrange + bool board1[3][3]={ + {0,1,0}, + {0,0,0}, + {0,0,0} + }; + + bool board2[3][3]={ + {0,0,0}, + {0,1,0}, + {0,0,0} + }; + + bool board3[3][3]={ + {0,0,0}, + {0,0,0}, + {0,1,0} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + + // assert + TEST_ASSERT_EQUAL_INT( 0, result ); + TEST_ASSERT_EQUAL_INT( 0, result2 ); + TEST_ASSERT_EQUAL_INT( 0, result3 ); +} \ No newline at end of file From 40ca77c8f6beb3c6107cf0ec89f86b4871839bbb Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:09:22 +0100 Subject: [PATCH 096/107] add another vertical player won test --- src/test/c/Georg/test_tictactoe.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 851eeb7..6c2bcb5 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -320,6 +320,37 @@ void test_negativ_checkOnWin_vertically2(void){ bool result2 = playerHasWon( board2 ); bool result3 = playerHasWon( board3 ); + // assert + TEST_ASSERT_EQUAL_INT( 0, result ); + TEST_ASSERT_EQUAL_INT( 0, result2 ); + TEST_ASSERT_EQUAL_INT( 0, result3 ); +} + +void test_negativ_checkOnWin_vertically3(void){ + // arrange + bool board1[3][3]={ + {0,0,1}, + {0,0,0}, + {0,0,0} + }; + + bool board2[3][3]={ + {0,0,0}, + {0,0,1}, + {0,0,0} + }; + + bool board3[3][3]={ + {0,0,0}, + {0,0,0}, + {0,0,1} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + // assert TEST_ASSERT_EQUAL_INT( 0, result ); TEST_ASSERT_EQUAL_INT( 0, result2 ); From e937b3d8c3154c36d4a4fd92228813be3382e9fb Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:09:47 +0100 Subject: [PATCH 097/107] add the player won logic to the game handler --- src/main/c/Georg/tictactoe.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 45cb1e0..fcade80 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -191,6 +191,12 @@ void handleGame(){ free(params); printBoard(); + + if( playerHasWon( GAME.board ) ){ + printf("\n\nDu hast gewonnwn!\n\n"); + // start menu + GAME.currentState=0; + } } } From c057d434510d556b048a2ba3d8056982c02d09d7 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:16:37 +0100 Subject: [PATCH 098/107] refactoring: rename the command structure --- src/main/c/Georg/tictactoe.c | 14 +++++++------- src/main/c/Georg/tictactoe.h | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index fcade80..ee0f8dc 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -8,7 +8,7 @@ void printBoard(); struct ticTacToe GAME; -struct command COMMANDS[MAX_COMMANDS] = { +struct usrCommand COMMANDS[MAX_COMMANDS] = { { 1, "\"start menu\" - startet das menu", startMenu}, { 2, "\"start game\" - startet das spiel", startGame} }; @@ -21,12 +21,12 @@ void startTicTacToe(){ GAME = createTicTacToe(); while( GAME.currentState != -1 ){ - commandFunction command; + commandFunction usrCommand; printf("search command!\n"); - command = getCommandById( GAME.currentState + 1); + usrCommand = getCommandById( GAME.currentState + 1); - if( command != NULL) - command(0); + if( usrCommand != NULL) + usrCommand(0); else{ printf("command not found"); return; @@ -105,7 +105,7 @@ int handleGameInput( char* input ){ } int startMenu( int code ){ - if( code == -1 ){ // command test + if( code == -1 ){ // usrCommand test return 1; } @@ -222,7 +222,7 @@ bool playerHasWon( bool board[BORAD_SIZE][BORAD_SIZE]){ } int startGame( int code ){ - if( code == -1 ){ // command test + if( code == -1 ){ // usrCommand test return 1; } diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index efd1d1e..59f90d9 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -15,14 +15,14 @@ struct ticTacToe{ // Typdefinition für einen Funktionszeiger typedef int (*commandFunction)( int ); -struct command{ +struct usrCommand{ int id; char* description; commandFunction fun; }; extern struct ticTacToe GAME; -extern struct command COMMANDS[MAX_COMMANDS]; +extern struct usrCommand COMMANDS[MAX_COMMANDS]; void startTicTacToe(); From 2423d72ef7df2bdfbd90f942172e2df5490e8d37 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:17:08 +0100 Subject: [PATCH 099/107] refactoring: commend a function --- 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 ee0f8dc..4f0b919 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -149,6 +149,11 @@ void printBoard(){ printf("\n"); } +/** + * Get a the user input and parse it. + * @param input + * @return a array with the x and y direction where the user wants to set the marker. + */ int* getMarkerParameters( char* input ){ int* array = (int*)malloc(2 * sizeof(int)); From 558582a8ad5db04811b0b25a25878605a14d269f Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:17:29 +0100 Subject: [PATCH 100/107] refactoring: commend the start function --- 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 4f0b919..2e900d6 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -14,11 +14,11 @@ struct usrCommand COMMANDS[MAX_COMMANDS] = { }; void startTicTacToe(){ - setbuf(stdout, 0); + setbuf(stdout, 0); // for debug output printf( "%s\n", getWelcomeMessageTicTacToe() ); printf( "%s\n\n", getRulesMessageTicTacToe() ); - GAME = createTicTacToe(); + GAME = createTicTacToe(); // create the "game object" while( GAME.currentState != -1 ){ commandFunction usrCommand; @@ -26,7 +26,7 @@ void startTicTacToe(){ usrCommand = getCommandById( GAME.currentState + 1); if( usrCommand != NULL) - usrCommand(0); + usrCommand(0); // 0, for non test behavior else{ printf("command not found"); return; From 4396c27a8caa5883c4b86fe61058301dedb40845 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:17:59 +0100 Subject: [PATCH 101/107] refactoring: disable debug output --- src/main/c/Georg/tictactoe.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 2e900d6..89fc6a5 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -22,7 +22,7 @@ void startTicTacToe(){ while( GAME.currentState != -1 ){ commandFunction usrCommand; - printf("search command!\n"); + //printf("search command!\n"); usrCommand = getCommandById( GAME.currentState + 1); if( usrCommand != NULL) From d4a4b71eb31f04ef65a5e32ac203128ea0c0bf16 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:18:16 +0100 Subject: [PATCH 102/107] refactoring: commend a function --- src/main/c/Georg/tictactoe.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 89fc6a5..d3b3c29 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -64,11 +64,11 @@ int handleCommand( char* input ){ commandFunction getCommandById( int id ){ commandFunction result = NULL; - size_t arraySize = sizeof(COMMANDS) / sizeof(COMMANDS[0]); + size_t arraySize = sizeof(COMMANDS) / sizeof(COMMANDS[0]); // calculate size of Array for (size_t i = 0; i < arraySize; i++) { //printf( "%s", COMMANDS[i].description ); if( COMMANDS[i].id == id ){ - result = COMMANDS[i].fun; + result = COMMANDS[i].fun; // save the function pointer as result break; } } From f0164972c9c49b33d0ce7a8c1a017659fc14219f Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:18:41 +0100 Subject: [PATCH 103/107] refactoring: comment a function and rename a variable --- src/main/c/Georg/tictactoe.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index d3b3c29..8f3e976 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -80,11 +80,10 @@ char* getUserInput(){ printf( ":" ); fgets(userInput, sizeof(userInput), stdin); - size_t len = strlen(userInput); - if (len > 0 && userInput[len - 1] == '\n') { - userInput[len - 1] = '\0'; + size_t lengthOfInput = strlen(userInput); + if (lengthOfInput > 0 && userInput[lengthOfInput - 1] == '\n') { + userInput[lengthOfInput - 1] = '\0'; // declare end of command } - return userInput; } From 8af1c7b6e0bee9f71264753501c5bdc7d88a0c7a Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:18:58 +0100 Subject: [PATCH 104/107] refactoring: renamed vars --- 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 8f3e976..535475d 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -88,9 +88,9 @@ char* getUserInput(){ } void initializeBoard( bool board[BORAD_SIZE][BORAD_SIZE] ){ - for (int i = 0; i < 3; ++i) { - for (int j = 0; j < 3; ++j) { - board[i][j] = 0; + for (int line = 0; line < 3; ++line) { + for (int column = 0; column < 3; ++column) { + board[line][column] = 0; } } } From 8ae3f760650b4a1d409f1ea05009ae463a3eab88 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:19:17 +0100 Subject: [PATCH 105/107] refactoring: comment a function --- 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 535475d..3d6d591 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -95,6 +95,11 @@ void initializeBoard( bool board[BORAD_SIZE][BORAD_SIZE] ){ } } +/** + * The function checks if the user has written a valid command + * @param input + * @return 1 for a valid command and 0 for an invalid one. + */ int handleGameInput( char* input ){ if( strstr(input, "set") != NULL ){ return 1; From 2c8f8aff685775e0c09cd0aeea0a8deee653ae21 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:19:47 +0100 Subject: [PATCH 106/107] refactoring: renamed some vars --- 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 3d6d591..0818463 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -134,10 +134,10 @@ void printBoard(){ } printf("\n"); - for (int i = 0; i < 3; ++i) { + for (int line = 0; line < 3; ++line) { printf("| "); - for (int j = 0; j < 3; ++j) { - if( GAME.board[i][j] == true ) + for (int column = 0; column < 3; ++column) { + if( GAME.board[line][column] == true ) printf( "X" ); else printf ( " " ); From 12168dd05720eb25847d101bf28f70004f3d5c81 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:20:23 +0100 Subject: [PATCH 107/107] refactoring: revise comments --- src/main/c/Georg/tictactoe.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 0818463..241ee34 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -129,6 +129,7 @@ int startMenu( int code ){ } void printBoard(){ + // calculate the size of the board and print the upper frame for( int i = 0; i < BORAD_SIZE*4+1; i++ ){ printf("-"); } @@ -147,6 +148,7 @@ void printBoard(){ printf( "\n" ); } + // calculate the size of the board and print the upper frame for( int i = 0; i < BORAD_SIZE*4+1; i++ ){ printf("-"); } @@ -165,8 +167,8 @@ int* getMarkerParameters( char* input ){ int firstArgument = input[index-1] - '0'; int secondArgument = input[index+1] - '0'; - array[0] = firstArgument-1; - array[1] = secondArgument-1; + array[0] = firstArgument-1; // return x + array[1] = secondArgument-1; // return y return array; } @@ -195,9 +197,9 @@ void handleGame(){ // gameCommand processing if( gameCommand == 1 ) { // set marker in field - int* params = getMarkerParameters( input ); - setBoardMarker( GAME.board, params ); - free(params); + int* params = getMarkerParameters( input ); // get the x and y values + setBoardMarker( GAME.board, params ); // apply the x and y values in the field + free(params); // prent memory leakage printBoard(); @@ -211,17 +213,16 @@ void handleGame(){ bool playerHasWon( bool board[BORAD_SIZE][BORAD_SIZE]){ bool player = 1; - // Überprüfe Zeilen und Spalten for (int i = 0; i < 3; i++) { - // Überprüfe Zeilen + // check the rows if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) || - // Überprüfe Spalten + // check the columns (board[0][i] == player && board[1][i] == player && board[2][i] == player)) { return true; // Spieler hat gewonnen } } - // Überprüfe Diagonalen + // check the diagonal line if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) || (board[0][2] == player && board[1][1] == player && board[2][0] == player)) { return true; // Spieler hat gewonnen