From c057d434510d556b048a2ba3d8056982c02d09d7 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:16:37 +0100 Subject: [PATCH 01/10] refactoring: rename the command structure --- src/main/c/Georg/tictactoe.c | 14 +++++++------- src/main/c/Georg/tictactoe.h | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index fcade80..ee0f8dc 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -8,7 +8,7 @@ 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} }; @@ -21,12 +21,12 @@ void startTicTacToe(){ GAME = createTicTacToe(); while( GAME.currentState != -1 ){ - commandFunction command; + commandFunction usrCommand; printf("search command!\n"); - command = getCommandById( GAME.currentState + 1); + usrCommand = getCommandById( GAME.currentState + 1); - if( command != NULL) - command(0); + if( usrCommand != NULL) + usrCommand(0); else{ printf("command not found"); return; @@ -105,7 +105,7 @@ int handleGameInput( char* input ){ } int startMenu( int code ){ - if( code == -1 ){ // command test + if( code == -1 ){ // usrCommand test return 1; } @@ -222,7 +222,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(); From 2423d72ef7df2bdfbd90f942172e2df5490e8d37 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:17:08 +0100 Subject: [PATCH 02/10] refactoring: commend a function --- 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 ee0f8dc..4f0b919 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -149,6 +149,11 @@ void printBoard(){ 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)); From 558582a8ad5db04811b0b25a25878605a14d269f Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:17:29 +0100 Subject: [PATCH 03/10] refactoring: commend the start function --- src/main/c/Georg/tictactoe.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 4f0b919..2e900d6 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -14,11 +14,11 @@ struct usrCommand COMMANDS[MAX_COMMANDS] = { }; 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 usrCommand; @@ -26,7 +26,7 @@ void startTicTacToe(){ usrCommand = getCommandById( GAME.currentState + 1); if( usrCommand != NULL) - usrCommand(0); + usrCommand(0); // 0, for non test behavior else{ printf("command not found"); return; From 4396c27a8caa5883c4b86fe61058301dedb40845 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:17:59 +0100 Subject: [PATCH 04/10] refactoring: disable debug output --- src/main/c/Georg/tictactoe.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 2e900d6..89fc6a5 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -22,7 +22,7 @@ void startTicTacToe(){ while( GAME.currentState != -1 ){ commandFunction usrCommand; - printf("search command!\n"); + //printf("search command!\n"); usrCommand = getCommandById( GAME.currentState + 1); if( usrCommand != NULL) From d4a4b71eb31f04ef65a5e32ac203128ea0c0bf16 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:18:16 +0100 Subject: [PATCH 05/10] refactoring: commend a function --- src/main/c/Georg/tictactoe.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 89fc6a5..d3b3c29 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -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; } } From f0164972c9c49b33d0ce7a8c1a017659fc14219f Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:18:41 +0100 Subject: [PATCH 06/10] refactoring: comment a function and rename a variable --- src/main/c/Georg/tictactoe.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index d3b3c29..8f3e976 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -80,11 +80,10 @@ 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; } From 8af1c7b6e0bee9f71264753501c5bdc7d88a0c7a Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:18:58 +0100 Subject: [PATCH 07/10] refactoring: renamed vars --- src/main/c/Georg/tictactoe.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 8f3e976..535475d 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -88,9 +88,9 @@ char* getUserInput(){ } 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; } } } From 8ae3f760650b4a1d409f1ea05009ae463a3eab88 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:19:17 +0100 Subject: [PATCH 08/10] refactoring: comment a function --- 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 535475d..3d6d591 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -95,6 +95,11 @@ void initializeBoard( bool board[BORAD_SIZE][BORAD_SIZE] ){ } } +/** + * 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; From 2c8f8aff685775e0c09cd0aeea0a8deee653ae21 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:19:47 +0100 Subject: [PATCH 09/10] refactoring: renamed some vars --- src/main/c/Georg/tictactoe.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 3d6d591..0818463 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -134,10 +134,10 @@ void printBoard(){ } 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 ( " " ); From 12168dd05720eb25847d101bf28f70004f3d5c81 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:20:23 +0100 Subject: [PATCH 10/10] refactoring: revise comments --- src/main/c/Georg/tictactoe.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 0818463..241ee34 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -129,6 +129,7 @@ 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("-"); } @@ -147,6 +148,7 @@ 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("-"); } @@ -165,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; } @@ -195,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(); @@ -211,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