From be2cb960ce17707f53f1c11f1f4d97f433e77cd4 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 11:37:00 +0100 Subject: [PATCH] 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