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
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:
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..
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:
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().
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?
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.
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?