Browse Source

computer wins game

remotes/origin/ticTacToe
Aimee Reincke 2 years ago
parent
commit
f0a9f0fdde
  1. 30
      src/c/ticTacToe.c
  2. 16
      test/c/test_ticTacToe.c

30
src/c/ticTacToe.c

@ -86,5 +86,35 @@ int wasGameWon(char field[3][3]){
winner = PLAYER; winner = PLAYER;
} }
} }
if(field[0][0] == 'X'){
if (field[0][0] == field[0][1] && field[0][2] == field[0][1]){
winner = COMPUTER;
}
else if (field[0][0] == field[1][0] && field[1][0] == field[2][0]){
winner = COMPUTER;
}
else if (field[0][0] == field[1][1] && field[1][1] == field[2][2]){
winner = COMPUTER;
}
}
else if(field[2][2] == 'X'){
if (field[2][2] == field[2][1] && field[2][2] == field[2][0]){
winner = COMPUTER;
}
else if (field[2][2] == field[1][2] && field[1][2] == field[0][2]){
winner = COMPUTER;
}
}
if(field[1][1] == 'X'){
if (field[1][1] == field[0][1] && field[0][1] == field[2][1]){
winner = COMPUTER;
}
else if (field[1][1] == field[1][0] && field[1][0] == field[1][2]){
winner = COMPUTER;
}
else if (field[2][0] == field[1][1] && field[1][1] == field[0][2]){
winner = COMPUTER;
}
}
return winner; return winner;
} }

16
test/c/test_ticTacToe.c

@ -54,5 +54,21 @@ void test_ticTacToe_playerWins(void)
TEST_ASSERT_EQUAL_INT(PLAYER, result); TEST_ASSERT_EQUAL_INT(PLAYER, result);
} }
void test_ticTacToe_ComputerWins(void)
{
/* arrange */
int result;
char field[][3] = {
{'X', '-', '-'},
{'X', '-', '-'},
{'X', '-', '-'}
};
/* act */
result = wasGameWon(field);
/* assert */
TEST_ASSERT_EQUAL_INT(COMPUTER, result);
}
#endif // TEST #endif // TEST
Loading…
Cancel
Save