diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index fcade80..241ee34 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -8,25 +8,25 @@ void printBoard(); struct ticTacToe GAME; -struct command COMMANDS[MAX_COMMANDS] = { +struct usrCommand COMMANDS[MAX_COMMANDS] = { { 1, "\"start menu\" - startet das menu", startMenu}, { 2, "\"start game\" - startet das spiel", startGame} }; void startTicTacToe(){ - setbuf(stdout, 0); + setbuf(stdout, 0); // for debug output printf( "%s\n", getWelcomeMessageTicTacToe() ); printf( "%s\n\n", getRulesMessageTicTacToe() ); - GAME = createTicTacToe(); + GAME = createTicTacToe(); // create the "game object" while( GAME.currentState != -1 ){ - commandFunction command; - printf("search command!\n"); - command = getCommandById( GAME.currentState + 1); + commandFunction usrCommand; + //printf("search command!\n"); + usrCommand = getCommandById( GAME.currentState + 1); - if( command != NULL) - command(0); + if( usrCommand != NULL) + usrCommand(0); // 0, for non test behavior else{ printf("command not found"); return; @@ -64,11 +64,11 @@ int handleCommand( char* input ){ commandFunction getCommandById( int id ){ commandFunction result = NULL; - size_t arraySize = sizeof(COMMANDS) / sizeof(COMMANDS[0]); + size_t arraySize = sizeof(COMMANDS) / sizeof(COMMANDS[0]); // calculate size of Array for (size_t i = 0; i < arraySize; i++) { //printf( "%s", COMMANDS[i].description ); if( COMMANDS[i].id == id ){ - result = COMMANDS[i].fun; + result = COMMANDS[i].fun; // save the function pointer as result break; } } @@ -80,22 +80,26 @@ char* getUserInput(){ printf( ":" ); fgets(userInput, sizeof(userInput), stdin); - size_t len = strlen(userInput); - if (len > 0 && userInput[len - 1] == '\n') { - userInput[len - 1] = '\0'; + size_t lengthOfInput = strlen(userInput); + if (lengthOfInput > 0 && userInput[lengthOfInput - 1] == '\n') { + userInput[lengthOfInput - 1] = '\0'; // declare end of command } - return userInput; } 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; + for (int line = 0; line < 3; ++line) { + for (int column = 0; column < 3; ++column) { + board[line][column] = 0; } } } +/** + * The function checks if the user has written a valid command + * @param input + * @return 1 for a valid command and 0 for an invalid one. + */ int handleGameInput( char* input ){ if( strstr(input, "set") != NULL ){ return 1; @@ -105,7 +109,7 @@ int handleGameInput( char* input ){ } int startMenu( int code ){ - if( code == -1 ){ // command test + if( code == -1 ){ // usrCommand test return 1; } @@ -125,15 +129,16 @@ int startMenu( int code ){ } void printBoard(){ + // calculate the size of the board and print the upper frame for( int i = 0; i < BORAD_SIZE*4+1; i++ ){ printf("-"); } printf("\n"); - for (int i = 0; i < 3; ++i) { + for (int line = 0; line < 3; ++line) { printf("| "); - for (int j = 0; j < 3; ++j) { - if( GAME.board[i][j] == true ) + for (int column = 0; column < 3; ++column) { + if( GAME.board[line][column] == true ) printf( "X" ); else printf ( " " ); @@ -143,12 +148,18 @@ void printBoard(){ printf( "\n" ); } + // calculate the size of the board and print the upper frame for( int i = 0; i < BORAD_SIZE*4+1; i++ ){ printf("-"); } printf("\n"); } +/** + * Get a the user input and parse it. + * @param input + * @return a array with the x and y direction where the user wants to set the marker. + */ int* getMarkerParameters( char* input ){ int* array = (int*)malloc(2 * sizeof(int)); @@ -156,8 +167,8 @@ int* getMarkerParameters( char* input ){ int firstArgument = input[index-1] - '0'; int secondArgument = input[index+1] - '0'; - array[0] = firstArgument-1; - array[1] = secondArgument-1; + array[0] = firstArgument-1; // return x + array[1] = secondArgument-1; // return y return array; } @@ -186,9 +197,9 @@ void handleGame(){ // gameCommand processing if( gameCommand == 1 ) { // set marker in field - int* params = getMarkerParameters( input ); - setBoardMarker( GAME.board, params ); - free(params); + int* params = getMarkerParameters( input ); // get the x and y values + setBoardMarker( GAME.board, params ); // apply the x and y values in the field + free(params); // prent memory leakage printBoard(); @@ -202,17 +213,16 @@ 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 + // check the rows if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) || - // Überprüfe Spalten + // check the columns (board[0][i] == player && board[1][i] == player && board[2][i] == player)) { return true; // Spieler hat gewonnen } } - // Überprüfe Diagonalen + // check the diagonal line 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 @@ -222,7 +232,7 @@ bool playerHasWon( bool board[BORAD_SIZE][BORAD_SIZE]){ } int startGame( int code ){ - if( code == -1 ){ // command test + if( code == -1 ){ // usrCommand test return 1; } diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index efd1d1e..59f90d9 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -15,14 +15,14 @@ struct ticTacToe{ // Typdefinition für einen Funktionszeiger typedef int (*commandFunction)( int ); -struct command{ +struct usrCommand{ int id; char* description; commandFunction fun; }; extern struct ticTacToe GAME; -extern struct command COMMANDS[MAX_COMMANDS]; +extern struct usrCommand COMMANDS[MAX_COMMANDS]; void startTicTacToe();