Browse Source

add player won logic

remotes/origin/georg
KaffeeMaus 11 months ago
parent
commit
9f1a33d710
  1. 21
      src/main/c/Georg/tictactoe.c
  2. 1
      src/main/c/Georg/tictactoe.h
  3. 31
      src/test/c/Georg/test_tictactoe.c

21
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 ){ int startGame( int code ){
if( code == -1 ){ // command test if( code == -1 ){ // command test
return 1; return 1;

1
src/main/c/Georg/tictactoe.h

@ -34,6 +34,7 @@ void initializeBoard( bool board[3][3] );
int handleGameInput( char* input ); int handleGameInput( char* input );
int* getMarkerParameters(); int* getMarkerParameters();
void setBoardMarker( bool board[BORAD_SIZE][BORAD_SIZE], int* params ); void setBoardMarker( bool board[BORAD_SIZE][BORAD_SIZE], int* params );
bool playerHasWon( bool board[BORAD_SIZE][BORAD_SIZE] );
/* commands */ /* commands */
commandFunction getCommandById(int id); commandFunction getCommandById(int id);

31
src/test/c/Georg/test_tictactoe.c

@ -169,4 +169,35 @@ void test_setBoardFields(){
// assert // assert
TEST_ASSERT_EQUAL_INT( 1, board[2][2] ); 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 );
} }
Loading…
Cancel
Save