From 4333fbf343ae5e4c939ff8f877babd4feb6fbb79 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Wed, 1 Feb 2023 15:17:00 +0100 Subject: [PATCH] Player wins Game --- src/c/ticTacToe.c | 35 +++++++++++++++++++++++++++++++++++ src/c/ticTacToe.h | 8 ++++++++ test/c/test_ticTacToe.c | 17 +++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 38be611..a4208a8 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -52,4 +52,39 @@ bool validateUserInput(int row, int col){ } } return false; +} + +int wasGameWon(char field[3][3]){ + int winner = NOWINNERYET; + if(field[0][0] == 'O'){ + if (field[0][0] == field[0][1] && field[0][2] == field[0][1]){ + winner = PLAYER; + } + else if (field[0][0] == field[1][0] && field[1][0] == field[2][0]){ + winner = PLAYER; + } + else if (field[0][0] == field[1][1] && field[1][1] == field[2][2]){ + winner = PLAYER; + } + } + else if(field[2][2] == 'O'){ + if (field[2][2] == field[2][1] && field[2][2] == field[2][0]){ + winner = PLAYER; + } + else if (field[2][2] == field[1][2] && field[1][2] == field[0][2]){ + winner = PLAYER; + } + } + if(field[1][1] == 'O'){ + if (field[1][1] == field[0][1] && field[0][1] == field[2][1]){ + winner = PLAYER; + } + else if (field[1][1] == field[1][0] && field[1][0] == field[1][2]){ + winner = PLAYER; + } + else if (field[2][0] == field[1][1] && field[1][1] == field[0][2]){ + winner = PLAYER; + } + } + return winner; } \ No newline at end of file diff --git a/src/c/ticTacToe.h b/src/c/ticTacToe.h index 250a462..9bca53a 100644 --- a/src/c/ticTacToe.h +++ b/src/c/ticTacToe.h @@ -2,10 +2,18 @@ #define TICTACTOE_H #include +enum Winner{ + PLAYER, + COMPUTER, + NOWINNERYET, + DRAW +}; + void printPrompt(); void printField(char field[3][3]); void initField(char field[3][3]); void getPlayerInput(char field[3][3]); bool validateUserInput(int row, int col); +int wasGameWon(char field[3][3]); #endif \ No newline at end of file diff --git a/test/c/test_ticTacToe.c b/test/c/test_ticTacToe.c index bb0509c..f86583b 100644 --- a/test/c/test_ticTacToe.c +++ b/test/c/test_ticTacToe.c @@ -37,5 +37,22 @@ void test_ticTacToe_invalidUserInput(void) TEST_ASSERT_EQUAL_INT(false, result); } +void test_ticTacToe_playerWins(void) +{ + /* arrange */ + int result; + char field[][3] = { + {'O', 'O', '-'}, + {'-', 'O', '-'}, + {'-', '-', 'O'} + }; + + /* act */ + result = wasGameWon(field); + + /* assert */ + TEST_ASSERT_EQUAL_INT(PLAYER, result); +} + #endif // TEST \ No newline at end of file