0hh1 puzzle game

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:
----
XX--
-XX-
--X-

becomes

----
XXO-
OXXO
--X-

in which X represent red and O represents blue, - is unknown

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  void solve_three_in_a_row(int board[MAX_SIZE][MAX_SIZE],
    int size,
    int row,
    bool announce) {
    for (int i = 0; i < size - 1; 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 - 1; 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&&board[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);
        }
    }
}


1 is red, 2 is blue

However, when i tried running it, the answer wasn't correct for a case:

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.
Last edited on
Topic archived. No new replies allowed.