From 51818122b7326b23511857e69cb8b0dea32bed13 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Wed, 1 Feb 2023 15:30:29 +0100 Subject: [PATCH] Field full no winner --- src/c/ticTacToe.c | 17 +++++++++++++---- test/c/test_ticTacToe.c | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index f5ecc78..985549d 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -55,7 +55,16 @@ bool validateUserInput(int row, int col){ } int wasGameWon(char field[3][3]){ - int winner = NOWINNERYET; + int winner = NOWINNERYET; + bool fieldFull = true; + for (int i = 0; i < 3; i++){ + for (int j = 0; j < 3; j++){ + if(field[i][j]=='-'){ + fieldFull = false; + } + } + } + if (fieldFull == true) winner = DRAW; if(field[0][0] == 'O'){ if (field[0][0] == field[0][1] && field[0][2] == field[0][1]){ winner = PLAYER; @@ -75,7 +84,7 @@ int wasGameWon(char field[3][3]){ winner = PLAYER; } } - if(field[1][1] == 'O'){ + else if(field[1][1] == 'O'){ if (field[1][1] == field[0][1] && field[0][1] == field[2][1]){ winner = PLAYER; } @@ -86,7 +95,7 @@ int wasGameWon(char field[3][3]){ winner = PLAYER; } } - if(field[0][0] == 'X'){ + else if(field[0][0] == 'X'){ if (field[0][0] == field[0][1] && field[0][2] == field[0][1]){ winner = COMPUTER; } @@ -105,7 +114,7 @@ int wasGameWon(char field[3][3]){ winner = COMPUTER; } } - if(field[1][1] == 'X'){ + else if(field[1][1] == 'X'){ if (field[1][1] == field[0][1] && field[0][1] == field[2][1]){ winner = COMPUTER; } diff --git a/test/c/test_ticTacToe.c b/test/c/test_ticTacToe.c index ae35d69..bc4439f 100644 --- a/test/c/test_ticTacToe.c +++ b/test/c/test_ticTacToe.c @@ -71,4 +71,21 @@ void test_ticTacToe_ComputerWins(void) TEST_ASSERT_EQUAL_INT(COMPUTER, result); } +void test_ticTacToe_NooneWins(void) +{ + /* arrange */ + int result; + char field[][3] = { + {'X', 'O', 'O'}, + {'O', 'X', 'X'}, + {'X', 'O', 'O'} + }; + + /* act */ + result = wasGameWon(field); + + /* assert */ + TEST_ASSERT_EQUAL_INT(DRAW, result); +} + #endif // TEST \ No newline at end of file