From 80f56467ef1e57d933afe814de4c546379742ea0 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:00:07 +0100 Subject: [PATCH 01/18] bugfix: no console output during debugging --- src/main/c/Georg/tictactoe.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 4e0ee11..c62338a 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -12,6 +12,7 @@ struct command COMMANDS[MAX_COMMANDS] = { }; void startTicTacToe(){ + setbuf(stdout, 0); printf( "%s\n", getWelcomeMessageTicTacToe() ); printf( "%s\n\n", getRulesMessageTicTacToe() ); From 6f5ce4501e58cea1c4448e4fc5fa5c92f638ff1d Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:01:37 +0100 Subject: [PATCH 02/18] board initialize added --- src/main/c/Georg/tictactoe.c | 8 ++++++++ src/main/c/Georg/tictactoe.h | 3 +++ src/test/c/Georg/test_tictactoe.c | 23 ++++++++++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index c62338a..3f2bfdc 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -86,6 +86,14 @@ char* getUserInput(){ return userInput; } +void initializeBoard( bool board[3][3] ){ + for (int i = 0; i < 3; ++i) { + for (int j = 0; j < 3; ++j) { + board[i][j] = 0; + } + } +} + int startMenu( int code ){ if( code == -1 ){ // command test return 1; diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 315b22c..fb29d59 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -1,6 +1,8 @@ #ifndef TICTACTOE_H #define TICTACTOE_H +#include + #define MAX_INPUT_LENGTH 20 #define MAX_COMMANDS 3 @@ -26,6 +28,7 @@ char* getWelcomeMessageTicTacToe(void); char* getRulesMessageTicTacToe(void); struct ticTacToe createTicTacToe(); int handleCommand( char* input ); +void initializeBoard( bool board[3][3] ); /* commands */ commandFunction getCommandById(int id); diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 2562299..dcc71be 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -105,4 +105,25 @@ void test_callCommandById_startGame(void){ // assert TEST_ASSERT_EQUAL_PTR( startGame, actualCommand ); -} \ No newline at end of file +} + + +void test_initializeBoard(void){ + // arrange + bool expectedBoard[3][3]={ + {0,0,0}, + {0,0,0}, + {0,0,0} + }; + + // act + bool actualBoard[3][3]; + initializeBoard(actualBoard); + + // assert + for (size_t i = 0; i < 3; i++) { + for (size_t ii = 0; ii < 3; ii++) { + TEST_ASSERT_EQUAL_INT(expectedBoard[i][ii], actualBoard[i][ii]); + } + } +} From c29eff6aa44618348cd1b266e4aa9b99cd80debd Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:02:31 +0100 Subject: [PATCH 03/18] add defenition for the board size --- src/main/c/Georg/tictactoe.c | 4 +++- src/main/c/Georg/tictactoe.h | 2 ++ src/test/c/Georg/test_tictactoe.c | 8 ++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 3f2bfdc..85a0fec 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -86,7 +86,7 @@ char* getUserInput(){ return userInput; } -void initializeBoard( bool board[3][3] ){ +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; @@ -120,6 +120,8 @@ int startGame( int code ){ } printf("Welcome to the game!\n"); + bool board[BORAD_SIZE][BORAD_SIZE]; + initializeBoard( board ); while( GAME.currentState == 1 ){ int nextState = handleCommand( getUserInput() ); diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index fb29d59..a73cb5e 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -5,9 +5,11 @@ #define MAX_INPUT_LENGTH 20 #define MAX_COMMANDS 3 +#define BORAD_SIZE 3 struct ticTacToe{ int currentState; + bool board[BORAD_SIZE][BORAD_SIZE]; }; // Typdefinition für einen Funktionszeiger diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index dcc71be..4413a8a 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -110,19 +110,19 @@ void test_callCommandById_startGame(void){ void test_initializeBoard(void){ // arrange - bool expectedBoard[3][3]={ + bool expectedBoard[BORAD_SIZE][BORAD_SIZE]={ {0,0,0}, {0,0,0}, {0,0,0} }; // act - bool actualBoard[3][3]; + bool actualBoard[BORAD_SIZE][BORAD_SIZE]; initializeBoard(actualBoard); // assert - for (size_t i = 0; i < 3; i++) { - for (size_t ii = 0; ii < 3; ii++) { + for (size_t i = 0; i < BORAD_SIZE; i++) { + for (size_t ii = 0; ii < BORAD_SIZE; ii++) { TEST_ASSERT_EQUAL_INT(expectedBoard[i][ii], actualBoard[i][ii]); } } From b84ed1c00abc55df56ffc3588163b6732ad3f0d1 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:03:22 +0100 Subject: [PATCH 04/18] added a function to print the game board --- src/main/c/Georg/tictactoe.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 85a0fec..7242d8a 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -4,6 +4,8 @@ #include "tictactoe.h" +void printBoard(); + struct ticTacToe GAME; struct command COMMANDS[MAX_COMMANDS] = { @@ -114,6 +116,31 @@ int startMenu( int code ){ return 0; } +void printBoard(){ + for( int i = 0; i < BORAD_SIZE*4+1; i++ ){ + printf("-"); + } + printf("\n"); + + for (int i = 0; i < 3; ++i) { + printf("| "); + for (int j = 0; j < 3; ++j) { + if( GAME.board[i][j] == true ) + printf( "X" ); + else + printf ( " " ); + + printf(" | "); + } + printf( "\n" ); + } + + for( int i = 0; i < BORAD_SIZE*4+1; i++ ){ + printf("-"); + } + printf("\n"); +} + int startGame( int code ){ if( code == -1 ){ // command test return 1; From ce7a6ba66cf811ce3eb2d3d311471c496568874f Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:04:21 +0100 Subject: [PATCH 05/18] add gameCommandHandler --- src/main/c/Georg/tictactoe.c | 8 ++++++++ src/main/c/Georg/tictactoe.h | 1 + src/test/c/Georg/test_tictactoe.c | 12 ++++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 7242d8a..614a3e7 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -96,6 +96,14 @@ void initializeBoard( bool board[BORAD_SIZE][BORAD_SIZE] ){ } } +int handleGameInput( char* input ){ + if( strstr(input, "set") != NULL ){ + return 1; + }else{ + return false; + } +} + int startMenu( int code ){ if( code == -1 ){ // command test return 1; diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index a73cb5e..923fb2e 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -31,6 +31,7 @@ char* getRulesMessageTicTacToe(void); struct ticTacToe createTicTacToe(); int handleCommand( char* input ); void initializeBoard( bool board[3][3] ); +int handleGameInput( char* input ); /* commands */ commandFunction getCommandById(int id); diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 4413a8a..702e4ae 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -127,3 +127,15 @@ void test_initializeBoard(void){ } } } + +void test_handleGameInput(void){ + // arrange + char* teststring = "set 3,4"; + int expectedState = 1; + + // act + int actualState = handleGameInput( teststring ); + + // assert + TEST_ASSERT_EQUAL_INT( expectedState, actualState ); +} From 4c17f9b836a61ebb73c731fe755c6e480e1a9faf Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:04:47 +0100 Subject: [PATCH 06/18] refactoring: test handleGameInput --- src/test/c/Georg/test_tictactoe.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 702e4ae..989ca12 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -131,11 +131,11 @@ void test_initializeBoard(void){ void test_handleGameInput(void){ // arrange char* teststring = "set 3,4"; - int expectedState = 1; + int expectedCommand = 1; // act int actualState = handleGameInput( teststring ); // assert - TEST_ASSERT_EQUAL_INT( expectedState, actualState ); + TEST_ASSERT_EQUAL_INT( expectedCommand, actualState ); } From 75e050c8d47291fb0495373ba5bee594ccafb017 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:05:44 +0100 Subject: [PATCH 07/18] add game command handler to the game --- src/main/c/Georg/tictactoe.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 614a3e7..4838534 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -100,7 +100,7 @@ int handleGameInput( char* input ){ if( strstr(input, "set") != NULL ){ return 1; }else{ - return false; + return -1; } } @@ -159,9 +159,12 @@ int startGame( int code ){ initializeBoard( board ); while( GAME.currentState == 1 ){ - int nextState = handleCommand( getUserInput() ); + char* input = getUserInput(); + int nextState = handleCommand( input ); + + if( nextState == -1 ){ // no stateCommand + int gameCommand = handleGameInput( input ); - if( nextState == -1 ){ printf("command not found!"); }else{ GAME.currentState = nextState; From 25c6e54e254fac1a11cf7df664f022fe941f585b Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:06:10 +0100 Subject: [PATCH 08/18] add function to parse the game commands --- src/main/c/Georg/tictactoe.c | 13 +++++++++++++ src/main/c/Georg/tictactoe.h | 1 + src/test/c/Georg/test_tictactoe.c | 14 ++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 4838534..fbee1de 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -149,6 +149,19 @@ void printBoard(){ printf("\n"); } +int* getMarkerParameters( char* input ){ + int* array = (int*)malloc(2 * sizeof(int)); + + int index = strchr(input, ',') - input; + int firstArgument = input[index-1] - '0'; + int secondArgument = input[index+1] - '0'; + + array[0] = firstArgument; + array[1] = secondArgument; + + return array; +} + int startGame( int code ){ if( code == -1 ){ // command test return 1; diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 923fb2e..7cf6b60 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -32,6 +32,7 @@ struct ticTacToe createTicTacToe(); int handleCommand( char* input ); void initializeBoard( bool board[3][3] ); int handleGameInput( char* input ); +int* getMarkerParameters(); /* commands */ commandFunction getCommandById(int id); diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 989ca12..5194ece 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -139,3 +139,17 @@ void test_handleGameInput(void){ // assert TEST_ASSERT_EQUAL_INT( expectedCommand, actualState ); } + +void test_getMarkerParameters(void){ + // arrange + char* teststring = "set 3,3"; + int expectedParams[2] = {3, 3}; + + // act + int* actualParams = getMarkerParameters( teststring ); + + // assert + for( int i = 0; i < 2; i++ ){ + TEST_ASSERT_EQUAL_INT( expectedParams[i], actualParams[i] ); + } +} \ No newline at end of file From 3a0e4ff19527afb8ab7152da07f278683d10cca0 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:06:26 +0100 Subject: [PATCH 09/18] added a game handler function --- src/main/c/Georg/tictactoe.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index fbee1de..1c602df 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -162,6 +162,30 @@ int* getMarkerParameters( char* input ){ return array; } +void handleGame(){ + char* input = getUserInput(); + int nextState = handleCommand( input ); + + // check commands, if no command found return for new Input + // gameCommands are saved and processed after this block + int gameCommand = -1; + if( nextState == -1 ){ // no stateCommand + gameCommand = handleGameInput( input ); + if( gameCommand == -1 ){ + printf("command not found!"); + return; + } + }else{ + GAME.currentState = nextState; + return; + } + + // gameCommand processing + if( gameCommand == 1 ) { // set marker in field + + } +} + int startGame( int code ){ if( code == -1 ){ // command test return 1; @@ -172,16 +196,7 @@ int startGame( int code ){ initializeBoard( board ); while( GAME.currentState == 1 ){ - char* input = getUserInput(); - int nextState = handleCommand( input ); - - if( nextState == -1 ){ // no stateCommand - int gameCommand = handleGameInput( input ); - - printf("command not found!"); - }else{ - GAME.currentState = nextState; - } + handleGame(); } return 0; From 955e91509fe84719159e7579db4596dcb89c4b3a Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:06:47 +0100 Subject: [PATCH 10/18] add function to set BoardMarker --- src/main/c/Georg/tictactoe.c | 12 +++++++++--- src/main/c/Georg/tictactoe.h | 1 + src/test/c/Georg/test_tictactoe.c | 19 ++++++++++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 1c602df..6888e7e 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -156,12 +156,16 @@ int* getMarkerParameters( char* input ){ int firstArgument = input[index-1] - '0'; int secondArgument = input[index+1] - '0'; - array[0] = firstArgument; - array[1] = secondArgument; + array[0] = firstArgument-1; + array[1] = secondArgument-1; return array; } +void setBoardMarker( bool board[BORAD_SIZE][BORAD_SIZE], int* params ){ + board[params[0]][params[1]] = 1; +} + void handleGame(){ char* input = getUserInput(); int nextState = handleCommand( input ); @@ -182,7 +186,9 @@ void handleGame(){ // gameCommand processing if( gameCommand == 1 ) { // set marker in field - + int* params = getMarkerParameters( input ); + setBoardMarker( GAME.board, params ); + free(params); } } diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 7cf6b60..7adc058 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -33,6 +33,7 @@ int handleCommand( char* input ); void initializeBoard( bool board[3][3] ); int handleGameInput( char* input ); int* getMarkerParameters(); +void setBoardMarker( bool board[BORAD_SIZE][BORAD_SIZE], int* params ); /* commands */ commandFunction getCommandById(int id); diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 5194ece..ffadb35 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -143,7 +143,7 @@ void test_handleGameInput(void){ void test_getMarkerParameters(void){ // arrange char* teststring = "set 3,3"; - int expectedParams[2] = {3, 3}; + int expectedParams[2] = {2, 2}; // act int* actualParams = getMarkerParameters( teststring ); @@ -152,4 +152,21 @@ void test_getMarkerParameters(void){ for( int i = 0; i < 2; i++ ){ TEST_ASSERT_EQUAL_INT( expectedParams[i], actualParams[i] ); } +} + +void test_setBoardFields(){ + // arrange + // arrange + bool board[3][3]={ + {0,0,0}, + {0,0,0}, + {0,0,0} + }; + int params[2] = {2,2}; + + // act + setBoardMarker( board, params ); + + // assert + TEST_ASSERT_EQUAL_INT( 1, board[2][2] ); } \ No newline at end of file From 7eed3ab448f84965e7c45aba78a019586758e884 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:07:04 +0100 Subject: [PATCH 11/18] add board print function --- src/main/c/Georg/tictactoe.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 6888e7e..43ff038 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -189,6 +189,8 @@ void handleGame(){ int* params = getMarkerParameters( input ); setBoardMarker( GAME.board, params ); free(params); + + printBoard(); } } @@ -201,6 +203,8 @@ int startGame( int code ){ bool board[BORAD_SIZE][BORAD_SIZE]; initializeBoard( board ); + printBoard(); + while( GAME.currentState == 1 ){ handleGame(); } From 9f1a33d710c6feec3e2e2d4567bec6bf1663da35 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:08:04 +0100 Subject: [PATCH 12/18] add player won logic --- src/main/c/Georg/tictactoe.c | 21 +++++++++++++++++++++ src/main/c/Georg/tictactoe.h | 1 + src/test/c/Georg/test_tictactoe.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 43ff038..45cb1e0 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -194,6 +194,27 @@ 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 + if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) || + // Überprüfe Spalten + (board[0][i] == player && board[1][i] == player && board[2][i] == player)) { + return true; // Spieler hat gewonnen + } + } + + // Überprüfe Diagonalen + 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 + } + + return false; +} + int startGame( int code ){ if( code == -1 ){ // command test return 1; diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h index 7adc058..efd1d1e 100644 --- a/src/main/c/Georg/tictactoe.h +++ b/src/main/c/Georg/tictactoe.h @@ -34,6 +34,7 @@ void initializeBoard( bool board[3][3] ); int handleGameInput( char* input ); int* getMarkerParameters(); void setBoardMarker( bool board[BORAD_SIZE][BORAD_SIZE], int* params ); +bool playerHasWon( bool board[BORAD_SIZE][BORAD_SIZE] ); /* commands */ commandFunction getCommandById(int id); diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index ffadb35..1881f89 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -169,4 +169,35 @@ void test_setBoardFields(){ // assert TEST_ASSERT_EQUAL_INT( 1, board[2][2] ); +} + +void test_checkOnWin_vertically(void){ + // arrange + bool board1[3][3]={ + {0,0,1}, + {0,0,1}, + {0,0,1} + }; + + bool board2[3][3]={ + {0,1,0}, + {0,1,0}, + {0,1,0} + }; + + bool board3[3][3]={ + {1,0,0}, + {1,0,0}, + {1,0,0} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + + // assert + TEST_ASSERT_EQUAL_INT( 1, result ); + TEST_ASSERT_EQUAL_INT( 1, result2 ); + TEST_ASSERT_EQUAL_INT( 1, result3 ); } \ No newline at end of file From f4640c343d6642e4f2e5dbc0d6544d1b5e906084 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:08:25 +0100 Subject: [PATCH 13/18] add a horizontal player won test --- src/test/c/Georg/test_tictactoe.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 1881f89..b8abc87 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -200,4 +200,35 @@ void test_checkOnWin_vertically(void){ TEST_ASSERT_EQUAL_INT( 1, result ); TEST_ASSERT_EQUAL_INT( 1, result2 ); TEST_ASSERT_EQUAL_INT( 1, result3 ); +} + +void test_negativ_checkOnWin_horizontally(void){ + // arrange + bool board1[3][3]={ + {1,0,0}, + {0,0,0}, + {0,0,0} + }; + + bool board2[3][3]={ + {0,0,0}, + {0,1,0}, + {0,0,0} + }; + + bool board3[3][3]={ + {0,0,0}, + {0,0,0}, + {0,0,1} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + + // assert + TEST_ASSERT_EQUAL_INT( 0, result ); + TEST_ASSERT_EQUAL_INT( 0, result2 ); + TEST_ASSERT_EQUAL_INT( 0, result3 ); } \ No newline at end of file From b2ddfbafb329fdc2288a14cb914d047577b1c455 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:08:38 +0100 Subject: [PATCH 14/18] add another horizontal player won test --- src/test/c/Georg/test_tictactoe.c | 33 ++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index b8abc87..3cd7a56 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -231,4 +231,35 @@ void test_negativ_checkOnWin_horizontally(void){ TEST_ASSERT_EQUAL_INT( 0, result ); TEST_ASSERT_EQUAL_INT( 0, result2 ); TEST_ASSERT_EQUAL_INT( 0, result3 ); -} \ No newline at end of file +} + +void test_negativ_checkOnWin_horizontally2(void){ + // arrange + bool board1[3][3]={ + {1,1,0}, + {0,0,0}, + {0,0,0} + }; + + bool board2[3][3]={ + {0,0,0}, + {0,1,1}, + {0,0,0} + }; + + bool board3[3][3]={ + {0,0,0}, + {0,0,0}, + {1,0,1} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + + // assert + TEST_ASSERT_EQUAL_INT( 0, result ); + TEST_ASSERT_EQUAL_INT( 0, result2 ); + TEST_ASSERT_EQUAL_INT( 0, result3 ); +} From 47cf6b43ed3975e0062603429c2b484ca5169ebc Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:08:51 +0100 Subject: [PATCH 15/18] add a vertical player won test --- src/test/c/Georg/test_tictactoe.c | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 3cd7a56..eed3cc1 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -263,3 +263,35 @@ void test_negativ_checkOnWin_horizontally2(void){ TEST_ASSERT_EQUAL_INT( 0, result2 ); TEST_ASSERT_EQUAL_INT( 0, result3 ); } + +void test_negativ_checkOnWin_vertically(void){ + // arrange + bool board1[3][3]={ + {1,0,0}, + {0,0,0}, + {0,0,0} + }; + + bool board2[3][3]={ + {0,0,0}, + {1,0,0}, + {0,0,0} + }; + + bool board3[3][3]={ + {0,0,0}, + {0,0,0}, + {1,0,0} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + + // assert + TEST_ASSERT_EQUAL_INT( 0, result ); + TEST_ASSERT_EQUAL_INT( 0, result2 ); + TEST_ASSERT_EQUAL_INT( 0, result3 ); +} + From 952568988f1af0f1a2b48a0771d9e7de1d7af4ed Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:09:09 +0100 Subject: [PATCH 16/18] add another vertical player won test --- src/test/c/Georg/test_tictactoe.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index eed3cc1..851eeb7 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -295,3 +295,33 @@ void test_negativ_checkOnWin_vertically(void){ TEST_ASSERT_EQUAL_INT( 0, result3 ); } +void test_negativ_checkOnWin_vertically2(void){ + // arrange + bool board1[3][3]={ + {0,1,0}, + {0,0,0}, + {0,0,0} + }; + + bool board2[3][3]={ + {0,0,0}, + {0,1,0}, + {0,0,0} + }; + + bool board3[3][3]={ + {0,0,0}, + {0,0,0}, + {0,1,0} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + + // assert + TEST_ASSERT_EQUAL_INT( 0, result ); + TEST_ASSERT_EQUAL_INT( 0, result2 ); + TEST_ASSERT_EQUAL_INT( 0, result3 ); +} \ No newline at end of file From 40ca77c8f6beb3c6107cf0ec89f86b4871839bbb Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:09:22 +0100 Subject: [PATCH 17/18] add another vertical player won test --- src/test/c/Georg/test_tictactoe.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c index 851eeb7..6c2bcb5 100644 --- a/src/test/c/Georg/test_tictactoe.c +++ b/src/test/c/Georg/test_tictactoe.c @@ -320,6 +320,37 @@ void test_negativ_checkOnWin_vertically2(void){ bool result2 = playerHasWon( board2 ); bool result3 = playerHasWon( board3 ); + // assert + TEST_ASSERT_EQUAL_INT( 0, result ); + TEST_ASSERT_EQUAL_INT( 0, result2 ); + TEST_ASSERT_EQUAL_INT( 0, result3 ); +} + +void test_negativ_checkOnWin_vertically3(void){ + // arrange + bool board1[3][3]={ + {0,0,1}, + {0,0,0}, + {0,0,0} + }; + + bool board2[3][3]={ + {0,0,0}, + {0,0,1}, + {0,0,0} + }; + + bool board3[3][3]={ + {0,0,0}, + {0,0,0}, + {0,0,1} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + // assert TEST_ASSERT_EQUAL_INT( 0, result ); TEST_ASSERT_EQUAL_INT( 0, result2 ); From e937b3d8c3154c36d4a4fd92228813be3382e9fb Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:09:47 +0100 Subject: [PATCH 18/18] add the player won logic to the game handler --- src/main/c/Georg/tictactoe.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c index 45cb1e0..fcade80 100644 --- a/src/main/c/Georg/tictactoe.c +++ b/src/main/c/Georg/tictactoe.c @@ -191,6 +191,12 @@ void handleGame(){ free(params); printBoard(); + + if( playerHasWon( GAME.board ) ){ + printf("\n\nDu hast gewonnwn!\n\n"); + // start menu + GAME.currentState=0; + } } }