|
|
@ -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; |
|
|
|
} |