From 6fdccd7ef13f314b7829b0afecc2e6a4aaa0a10c Mon Sep 17 00:00:00 2001 From: KaffeeMaus <georgvolkmar088@gmail.com> Date: Thu, 25 Jan 2024 21:30:20 +0100 Subject: [PATCH 01/44] 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 <stdio.h> +#include <stdlib.h> + +#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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 10:54:39 +0100 Subject: [PATCH 02/44] 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 <stdlib.h> #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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 10:56:26 +0100 Subject: [PATCH 03/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 10:58:48 +0100 Subject: [PATCH 04/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:02:41 +0100 Subject: [PATCH 05/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:03:53 +0100 Subject: [PATCH 06/44] 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 <stdio.h> #include <stdlib.h> +#include <string.h> #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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:05:31 +0100 Subject: [PATCH 07/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:06:08 +0100 Subject: [PATCH 08/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:06:36 +0100 Subject: [PATCH 09/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:07:11 +0100 Subject: [PATCH 10/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:08:48 +0100 Subject: [PATCH 11/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:31:08 +0100 Subject: [PATCH 12/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:31:34 +0100 Subject: [PATCH 13/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:31:57 +0100 Subject: [PATCH 14/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:32:19 +0100 Subject: [PATCH 15/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:32:49 +0100 Subject: [PATCH 16/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:33:19 +0100 Subject: [PATCH 17/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:33:39 +0100 Subject: [PATCH 18/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:34:09 +0100 Subject: [PATCH 19/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:34:49 +0100 Subject: [PATCH 20/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:35:14 +0100 Subject: [PATCH 21/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:35:40 +0100 Subject: [PATCH 22/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:35:57 +0100 Subject: [PATCH 23/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:36:19 +0100 Subject: [PATCH 24/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:37:00 +0100 Subject: [PATCH 25/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 11:52:36 +0100 Subject: [PATCH 26/44] 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 <stdio.h> #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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:00:07 +0100 Subject: [PATCH 27/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:01:37 +0100 Subject: [PATCH 28/44] 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 <stdbool.h> + #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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:02:31 +0100 Subject: [PATCH 29/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:03:22 +0100 Subject: [PATCH 30/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:04:21 +0100 Subject: [PATCH 31/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:04:47 +0100 Subject: [PATCH 32/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:05:44 +0100 Subject: [PATCH 33/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:06:10 +0100 Subject: [PATCH 34/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:06:26 +0100 Subject: [PATCH 35/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:06:47 +0100 Subject: [PATCH 36/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:07:04 +0100 Subject: [PATCH 37/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:08:04 +0100 Subject: [PATCH 38/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:08:25 +0100 Subject: [PATCH 39/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:08:38 +0100 Subject: [PATCH 40/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:08:51 +0100 Subject: [PATCH 41/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:09:09 +0100 Subject: [PATCH 42/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:09:22 +0100 Subject: [PATCH 43/44] 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 <georgvolkmar088@gmail.com> Date: Fri, 26 Jan 2024 12:09:47 +0100 Subject: [PATCH 44/44] 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; + } } }