I'm trying to create a puzzle-like, sudoku-style game called "0hh1", but i seem to keep getting stuck on this one fuction. The game doesn't allow the user to put three consecutive squares of the same color (there are two colors, red and blue) next to each other, but two is fine. This function should check the row for any two consecutive squares of the same color and write the opposite color in the square not yet filled (UNKNOWN squares). It should write the ooposite color on both ends of two consecutive tiles in row, and in the middle between two tiles of the same color in row. mark_square_as() is the function that assigns color to a square and the each board has the same number of rows and columns.
Example: ---- becomes ----
XX-- XXO-
-XX- OXXO
--X- --X-
in which X represent red and O represents blue, - is unknown
Here is my code:
void solve_three_in_a_row(int board[MAX_SIZE][MAX_SIZE],
int size,
int row,
bool announce) {
for (int i = 0; i < size; i++) {
if (board[row][i] == 1 && board[row][i + 1] == 1) {
if (i + 1 == size - 1 && i - 1 >= 0 && board[row][i - 1] == UNKNOWN) {
mark_square_as(board, size, row, i - 1, 2, announce);
}
else if (i == 0 && i + 2 <= size && board[row][i + 2] == UNKNOWN) {
mark_square_as(board, size, row, i + 2, 2, announce);
}
else if (i - 1 >= 0 && i + 2 <= size && board[row][i - 1] == UNKNOWN && board[row][i + 2] == UNKNOWN) {
mark_square_as(board, size, row, i - 1, 2, announce);
mark_square_as(board, size, row, i + 2, 2, announce);
}
}
else if (board[row][i] == 1 && board[row][i + 2] == 1 && board[row][i + 1] == UNKNOWN) {
mark_square_as(board, size, row, i + 1, 2, announce);
}
}
for (int i = 0; i < size; i++) {
if (board[row][i] == 2 && board[row][i + 1] == 2) {
if (i + 1 == size - 1 && i - 1 >= 0 && board[row][i - 1] == UNKNOWN) {
mark_square_as(board, size, row, i - 1, 1, announce);
}
else if (i == 0 && i + 2 <= size && board[row][i + 2] == UNKNOWN) {
mark_square_as(board, size, row, i + 2, 1, announce);
}
else if (i - 1 >= 0 && i + 2 <= size && [row][i - 1] == UNKNOWN && board[row][i + 2] == UNKNOWN) {
mark_square_as(board, size, row, i - 1, 1, announce);
mark_square_as(board, size, row, i + 2, 1, announce);
}
}
else if (board[row][i] == 2 && board[row][i + 2] == 2 && board[row][i + 1] == UNKNOWN) {
mark_square_as(board, size, row, i + 1, 1, announce);
}
}
}
However, when i try running it, the answers weren't correct for a few cases: (the right side is my output, the left side is the correct output)
OXXO
XOXO
OXOX
XOOX
(my output said to mark (2nd row, 5th column) as X, when there is not even a fifth column)
O-
--
(my output said to mark (1st row, 2nd column) as X, when it should mark nothing)
OO-X--
X--X-O
------
X---OX
---XOO
-X-O-O
(my output said to mark (1st row, 3rd column) and (6th row, 5th column) as X, which is correct. But it didn't say to mark (1st row, 5th column) as O as it should because (1st row, 3rd column) would have also been marked).
I'm not sure where my code went wrong, thank you so much.
Aucun commentaire:
Enregistrer un commentaire