From 1d25e840019c6b077685d0ecf3df76f132f4be63 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:31:08 +0100 Subject: [PATCH 01/15] 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 02/15] 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 03/15] 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 04/15] 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 05/15] 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 06/15] 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 07/15] 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 08/15] 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 09/15] 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 10/15] 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 11/15] 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 12/15] 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 13/15] 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 14/15] 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 15/15] 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);