diff --git a/src/c/ticTacToe.c b/src/c/ticTacToe.c index 3f3066a..f811deb 100644 --- a/src/c/ticTacToe.c +++ b/src/c/ticTacToe.c @@ -139,7 +139,8 @@ int wasGameWon(char field[3][3]){ } void getComputerInput(char field[3][3]){ - bool symbolSet = false; + bool symbolSet = false, *pt = &symbolSet; + fillAlmostFull(field, pt); while (!symbolSet){ int row, col; row = rand() % 3; @@ -150,3 +151,265 @@ void getComputerInput(char field[3][3]){ } } } + + +void fillAlmostFull(char field[3][3], bool* pt) { + if (field[0][0] == 'X') { + if (field[0][1] == 'X') { + field[0][2] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'X') { + field[0][1] = 'X'; + *pt = true; + return; + } + else if (field[1][0] == 'X') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[2][0] == 'X') { + field[1][0] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'X') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'X') { + field[1][1] = 'X'; + *pt = true; + return; + } + } + if (field[0][1] == 'X') { + if (field[0][0] == 'X') { + field[0][2] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'X') { + field[0][0] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'X') { + field[2][1] = 'X'; + *pt = true; + return; + } + else if (field[2][1] == 'X') { + field[1][1] = 'X'; + *pt = true; + return; + } + } + if (field[0][2] == 'X') { + if (field[0][0] == 'X') { + field[0][1] = 'X'; + *pt = true; + return; + } + else if (field[0][1] == 'X') { + field[0][0] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'X') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[2][0] == 'X') { + field[1][1] = 'X'; + *pt = true; + return; + } + else if (field[1][2] == 'X') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'X') { + field[1][2] = 'X'; + *pt = true; + return; + } + } + if (field[1][0] == 'X') { + if (field[1][1] == 'X') { + field[1][2] = 'X'; + *pt = true; + return; + } + else if (field[1][2] == 'X') { + field[1][1] = 'X'; + *pt = true; + return; + } + else if (field[0][0] == 'X') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[2][0] == 'X') { + field[0][0] = 'X'; + *pt = true; + return; + } + } + if (field[1][1] == 'X') { + if (field[1][0] == 'X') { + field[1][2] = 'X'; + *pt = true; + return; + } + else if (field[1][2] == 'X') { + field[1][0] = 'X'; + *pt = true; + return; + } + else if (field[0][1] == 'X') { + field[2][1] = 'X'; + *pt = true; + return; + } + else if (field[2][1] == 'X') { + field[0][1] = 'X'; + *pt = true; + return; + } + else if (field[0][0] == 'X') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'X') { + field[0][0] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'X') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[2][0] == 'X') { + field[0][2] = 'X'; + *pt = true; + return; + } + } + if (field[1][2] == 'X') { + if (field[1][0] == 'X') { + field[1][1] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'X') { + field[1][0] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'X') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'X') { + field[0][2] = 'X'; + *pt = true; + return; + } + } + if (field[2][0] == 'X') { + if (field[2][1] == 'X') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'X') { + field[2][1] = 'X'; + *pt = true; + return; + } + else if (field[0][0] == 'X') { + field[1][0] = 'X'; + *pt = true; + return; + } + else if (field[1][0] == 'X') { + field[0][0] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'X') { + field[0][2] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'X') { + field[1][1] = 'X'; + *pt = true; + return; + } + } + if (field[2][1] == 'X') { + if (field[2][0] == 'X') { + field[2][2] = 'X'; + *pt = true; + return; + } + else if (field[2][2] == 'X') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'X') { + field[0][1] = 'X'; + *pt = true; + return; + } + else if (field[0][1] == 'X') { + field[1][1] = 'X'; + *pt = true; + return; + } + } + if (field[2][2] == 'X') { + if (field[2][0] == 'X') { + field[2][1] = 'X'; + *pt = true; + return; + } + else if (field[2][1] == 'X') { + field[2][0] = 'X'; + *pt = true; + return; + } + else if (field[0][2] == 'X') { + field[1][2] = 'X'; + *pt = true; + return; + } + else if (field[1][2] == 'X') { + field[0][2] = 'X'; + *pt = true; + return; + } + else if (field[1][1] == 'X') { + field[0][0] = 'X'; + *pt = true; + return; + } + else if (field[0][0] == 'X') { + field[1][1] = 'X'; + *pt = true; + return; + } + } +} diff --git a/src/c/ticTacToe.h b/src/c/ticTacToe.h index 222383d..6db536c 100644 --- a/src/c/ticTacToe.h +++ b/src/c/ticTacToe.h @@ -16,5 +16,6 @@ void getPlayerInput(char field[3][3]); bool validatePlayerInput(int row, int col); int wasGameWon(char field[3][3]); void getComputerInput(char field[3][3]); +void fillAlmostFull(char field[3][3], bool *pt); #endif \ No newline at end of file