diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index c3b5525..4e0ee11 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -4,13 +4,38 @@ #include "tictactoe.h" -struct ticTacToe TICTACTOE; +struct ticTacToe GAME; -char* getWelcomeMessage(){ +struct command COMMANDS[MAX_COMMANDS] = { + { 1, "\"start menu\" - startet das menu", startMenu}, + { 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* 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" @@ -29,5 +54,73 @@ int handleCommand( char* input ){ return 0; }else if( strcmp(input, "start game") == 0 ){ return 1; + }else{ + return -1; } +} + +commandFunction getCommandById( int id ){ + commandFunction result = NULL; + size_t arraySize = sizeof(COMMANDS) / sizeof(COMMANDS[0]); + for (size_t i = 0; i < arraySize; i++) { + //printf( "%s", COMMANDS[i].description ); + if( COMMANDS[i].id == id ){ + result = COMMANDS[i].fun; + break; + } + } + 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; + } + + printf("Welcome to the menu!\n"); + + while( GAME.currentState == 0 ){ + int nextState = handleCommand( getUserInput() ); + + if( nextState == -1 ){ + printf("command not found!"); + }else{ + GAME.currentState = nextState; + } + } + + return 0; +} + +int startGame( int code ){ + if( code == -1 ){ // command test + 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 d054a66..315b22c 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -1,13 +1,35 @@ #ifndef TICTACTOE_H #define TICTACTOE_H +#define MAX_INPUT_LENGTH 20 +#define MAX_COMMANDS 3 + struct ticTacToe{ int currentState; }; -char* getWelcomeMessage(); -char* getRulesMessage(); +// Typdefinition für einen Funktionszeiger +typedef int (*commandFunction)( int ); + +struct command{ + int id; + char* description; + commandFunction fun; +}; + +extern struct ticTacToe GAME; +extern struct command COMMANDS[MAX_COMMANDS]; + + +void startTicTacToe(); +char* getWelcomeMessageTicTacToe(void); +char* getRulesMessageTicTacToe(void); struct ticTacToe createTicTacToe(); int handleCommand( char* input ); +/* commands */ +commandFunction getCommandById(int id); +int startMenu( int code ); +int startGame( int code ); + #endif //TICTACTOE_H 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 546dc88..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); @@ -73,4 +73,36 @@ void test_command_startMenu(void){ // assert TEST_ASSERT_EQUAL_INT( expectedState, actualState ); +} + +void test_checkCommandlist(void){ + // arrange + + // act + + 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) ); + } +} + + +void test_callCommandById(void){ + // arrange + + // act + 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