Browse Source

Field full no winner

remotes/origin/ticTacToe
Aimee Reincke 2 years ago
parent
commit
51818122b7
  1. 17
      src/c/ticTacToe.c
  2. 17
      test/c/test_ticTacToe.c

17
src/c/ticTacToe.c

@ -55,7 +55,16 @@ bool validateUserInput(int row, int col){
} }
int wasGameWon(char field[3][3]){ 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] == 'O'){
if (field[0][0] == field[0][1] && field[0][2] == field[0][1]){ if (field[0][0] == field[0][1] && field[0][2] == field[0][1]){
winner = PLAYER; winner = PLAYER;
@ -75,7 +84,7 @@ int wasGameWon(char field[3][3]){
winner = PLAYER; 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]){ if (field[1][1] == field[0][1] && field[0][1] == field[2][1]){
winner = PLAYER; winner = PLAYER;
} }
@ -86,7 +95,7 @@ int wasGameWon(char field[3][3]){
winner = PLAYER; 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]){ if (field[0][0] == field[0][1] && field[0][2] == field[0][1]){
winner = COMPUTER; winner = COMPUTER;
} }
@ -105,7 +114,7 @@ int wasGameWon(char field[3][3]){
winner = COMPUTER; 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]){ if (field[1][1] == field[0][1] && field[0][1] == field[2][1]){
winner = COMPUTER; winner = COMPUTER;
} }

17
test/c/test_ticTacToe.c

@ -71,4 +71,21 @@ void test_ticTacToe_ComputerWins(void)
TEST_ASSERT_EQUAL_INT(COMPUTER, result); 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 #endif // TEST
Loading…
Cancel
Save