Browse Source

added fillAlmostFull for computerwin

remotes/origin/ticTacToe
Aimee Reincke 2 years ago
parent
commit
21269448d3
  1. 265
      src/c/ticTacToe.c
  2. 1
      src/c/ticTacToe.h

265
src/c/ticTacToe.c

@ -139,7 +139,8 @@ int wasGameWon(char field[3][3]){
} }
void getComputerInput(char field[3][3]){ void getComputerInput(char field[3][3]){
bool symbolSet = false;
bool symbolSet = false, *pt = &symbolSet;
fillAlmostFull(field, pt);
while (!symbolSet){ while (!symbolSet){
int row, col; int row, col;
row = rand() % 3; 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;
}
}
}

1
src/c/ticTacToe.h

@ -16,5 +16,6 @@ void getPlayerInput(char field[3][3]);
bool validatePlayerInput(int row, int col); bool validatePlayerInput(int row, int col);
int wasGameWon(char field[3][3]); int wasGameWon(char field[3][3]);
void getComputerInput(char field[3][3]); void getComputerInput(char field[3][3]);
void fillAlmostFull(char field[3][3], bool *pt);
#endif #endif
Loading…
Cancel
Save