Browse Source

Player wins Game

remotes/origin/ticTacToe
Aimee Reincke 2 years ago
parent
commit
4333fbf343
  1. 35
      src/c/ticTacToe.c
  2. 8
      src/c/ticTacToe.h
  3. 17
      test/c/test_ticTacToe.c

35
src/c/ticTacToe.c

@ -52,4 +52,39 @@ bool validateUserInput(int row, int col){
} }
} }
return false; 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;
} }

8
src/c/ticTacToe.h

@ -2,10 +2,18 @@
#define TICTACTOE_H #define TICTACTOE_H
#include <stdbool.h> #include <stdbool.h>
enum Winner{
PLAYER,
COMPUTER,
NOWINNERYET,
DRAW
};
void printPrompt(); void printPrompt();
void printField(char field[3][3]); void printField(char field[3][3]);
void initField(char field[3][3]); void initField(char field[3][3]);
void getPlayerInput(char field[3][3]); void getPlayerInput(char field[3][3]);
bool validateUserInput(int row, int col); bool validateUserInput(int row, int col);
int wasGameWon(char field[3][3]);
#endif #endif

17
test/c/test_ticTacToe.c

@ -37,5 +37,22 @@ void test_ticTacToe_invalidUserInput(void)
TEST_ASSERT_EQUAL_INT(false, result); 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 #endif // TEST
Loading…
Cancel
Save