From 9f1a33d710c6feec3e2e2d4567bec6bf1663da35 Mon Sep 17 00:00:00 2001 From: KaffeeMaus Date: Fri, 26 Jan 2024 12:08:04 +0100 Subject: [PATCH] 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