Check 3 in a Row

Hi, so I'm working on a project for school where part of it involves having to check if there are 3 of the same color in a row on a 2D array board. I was given the arguments in the function declaration below so I have to work with them to solve the problem. I thought this would do the trick but it spits out 1 or doesn't return. MAX_SIZE is a constant of 8 I was given and it makes a square board.

Below is a test board. X's are RED and O's are BLUE. I'd appreciate the help, thanks!
(It's from the game 0h h1)

EDIT: If row is 1, then the corresponding element I would figure would be i-1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool row_has_no_threes_of_color(const int board[MAX_SIZE][MAX_SIZE],
                                int size,
                                int row,
                                int color) {
    for (int i = row; i = row; i++) {
        for (int x = 0; x < size; x++) {
            if (color == RED && (board[i - 1][x] == board[i - 1][x + 1] ==
                board[i - 1][x + 2])) {
                return true;
            }
            if (color == BLUE && (board[i - 1][x] == board[i - 1][x + 1] ==
                board[i - 1][x + 2])) {
                return true;
            }
        }
    }
    return false;
}

string test_board_1[] = { "-XXX",
                          "OOO-",
                          "X-XX",
                          "XO--" };
Last edited on
You can check one row of the board with a program like this (modify as required), so to check the entire 2D array you apply this to each row of the board:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

bool threeInRowCheck(const int* board, const int& SIZE)
{
    for (size_t i = 0; i < SIZE - 2; ++i)
    {
        if ((*(board + i) == *(board + i + 1) && (*(board + i + 1) == *(board + i+2))))
        {
               std::cout << "Found \n";
               return true;
        }
    }
    std::cout << "Not found \n";
    return false;
}
int main()
{
    const int board [] = {1, 2, 9, 9, 9, 4, 5};
    std::cout << threeInRowCheck(board, sizeof(board)/sizeof(board[0]));
}
@Bizzy

I changed your code up a bit, but the concept should be the same. I'm not checking columns, as you never mentioned needing them. Anyway, here's one way of doing the project..
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
#include <iostream>
#include <string>

using namespace std;

bool row_has_no_threes_of_color(const string board[4], int size, int row, char color)
{
	int found = 0;
	
		for(int i=0;i<size;i++)
		{
			if(board[row][i]==color)
				found++;
		}
		if(found>=3)
		{
			cout << "Found " << found << " " << color << "'s in row " << row+1 << endl;
			return true;
		}

	return false;
}

int main()
{
	string board[] = { "-XXX","OOO-","X-XX","XO--"};
	char ck[3]={'-','X','O'}; // Search for 
	bool yes=true;
	for(int x=0;x<3;x++)
	{
		for(int i=0;i<4;i++)
			{
				yes = row_has_no_threes_of_color(board, 4, i, ck[x]);
					if(yes)
						cout << "3 or more matches for " << ck[x] << " found in row " << i+1 << ".." << endl;
		}
	}
	return 0;
}
@whitenite1

I think I've found a mistake in your code... what if "XXOX" is one of the rows of board[]? Althought there are 3 "X"s in the row, there are not 3 X's in a row (3 X's in sequence...). I've tried to rewrite your code. Now, whenever the sequence is broken, found = 0. Take a look:

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
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>

using namespace std;

bool row_has_no_threes_of_color(const string board[], int size, int row, char color)
{
	int found = 0;
	
		for(int i=0;i<size;i++)
		{

			if (board[row][i]==color)
			{
				found++;
			} else 
			{
				found = 0;
			}

			if(found>=3)
			{
				cout << "Found " << found << " " << color << "'s in row " << row+1 << endl;
				return true;
			}
			
		}

	return false;
}

int main()
{
	string board[] = { "XX0X","OOO-","X-XX","XO--"};
	char ck[3]={'-','X','O'}; // Search for 
	bool yes=true;
	for(int x=0;x<3;x++)
	{
		for(int i=0;i<4;i++)
			{
				yes = row_has_no_threes_of_color(board, 4, i, ck[x]);
					if(yes)
						cout << "3 or more matches for " << ck[x] << " found in row " << i+1 
					         << ".." << endl;
		}
	}
	return 0;
}
Last edited on
@ufrnkiddo

That is not the way I read the OP request.
check if there are 3 of the same color in a row on a 2D array board

I take it that there are 3 out of the 4 in each of the rows, not that they are in sequence.

Looking at your code, I see problems, though. What if the row being looked at, was "XXX-". Well when it gets to the last entry in the sequence, that is not the one being searched for, and found becomes 0. Now, it doesn't look like a sequence of three or more was found, and a false is returned. Wrong answer received in main().
@whitenite1

If the row being looked is "XXX-", then, when the iteration gets to the third X, the input "Found 3 X's in row ?
3 or more matches for X found in row ?.." will be printed, before found = 0 (that only happens when i==3). I did this by putting the last if inside the for loop. What do you think?
The rules of the game (apparently!)
https://www.microsoft.com/en-us/store/p/0h-h1/9wzdncrcs0jl

I thought the OP's board had to be an integer array?

If you are going to send a row (or column) as a string then you might as well use the .find() member function of the string class:
found = ( row.find("XXX") != string::npos );
etc.

Or am I missing something?
I really apologize for the lack of clarity:
The rules for the game include that there cannot be 3 of the same color in SEQUENCE. This part that I'm working on is specifically referring to rows and not columns because there is a separate function for checking that which I will do after. Unfortunately I can't combine the two functions because of the specifics of the assignment.

And the string board was just something I was given in the assignment as a basic test. I don't think I'm supposed to use .find()

Here's a link to the original game if you'd like: http://0hh1.com/

@ufrnkiddo
Was your fix to my code adequate then, or was there anything else you might need to know?
Last edited on
@ufrnkiddo
Your code was spot on, man. I appreciate the help from everyone.
Topic archived. No new replies allowed.