I am still new to programming and this site please be nice to me an unknowing fool. I am currently working on an assignment. We need to make a Binoxxo puzzle in C and when entering a value check if it's correct according to the rules. The rules are:
- There can't be more than two following 'x' or 'o'.
- Every row and column needs to have an equal amount of 'x' and 'o'.
- All rows and columns are unique.
The Binoxxo field is an array[10][10]
filled with 'x', 'o' and ' '. I have a working solution for the second rule. However, I can't really figure out the other two. I'm currently getting more used to array's but here I guess I still don't really understand it.
This was my kind of approach to rule 1:
//There can't be more than two following 'x' or 'o'
for (i = j = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
if ((puzzle[i][j + 1] == val) && (puzzle[i][j - 1] == val)) x++;
if ((puzzle[i][j + 1] == val) && (puzzle[i][j + 2] == val)) x++;
if ((puzzle[i][j - 1] == val) && (puzzle[i][j - 2] == val)) x++;
if ((puzzle[i + 1][j] == val) && (puzzle[i - 1][j] == val)) x++;
if ((puzzle[i + 1][j] == val) && (puzzle[i + 2][j] == val)) x++;
if ((puzzle[i - 1][j] == val) && (puzzle[i - 2][j] == val)) x++;
if (x > 0)
return false;
}
}
This is a picture of the array for better understanding.
I was trying to cover the areas around one point to make sure that there would not be more than two x or o in a row. Val is entered in another function through the console and can be x or o.
When I try to run this it first starts correctly and wenn I try to enter something I get the error 0xC0000005 Access Violation
.
I read that this error can occur when there is an issue with a pointer. I noticed that I have a warning C6011
dereferencing NULL-Pointer, which leads me to this:
char** board()
{
int i, j;
char** puzzle;
char arrpuzzle[10][10] = {' ', ' ', 'o', 'x', ' ', ' ', ' ', ' ', ' ', 'o',
'x', ' ', ' ', 'x', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', 'o', ' ', ' ', 'x', ' ', ' ', ' ', 'o',
' ', ' ', ' ', ' ', 'o', ' ', ' ', ' ', 'o', ' ',
'o', ' ', ' ', 'x', ' ', ' ', ' ', 'x', ' ', ' ',
'o', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'o', ' ',
' ', ' ', ' ', 'x', ' ', 'x', ' ', ' ', ' ', ' ',
' ', 'o', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', 'o', ' ', 'o', ' ', 'x', 'x', ' ', ' ', ' ',
' ', ' ', 'x', ' ', ' ', ' ', 'o', ' ', 'x', 'o' };
puzzle = (char**)malloc(sizeof(char*) * 10);
for (i = 0; i < 10; i++) {
puzzle[i] = (char*)malloc(sizeof(char) * 10); // Warning here...
for (j = 0; j < 10; j++) {
puzzle[i][j] = arrpuzzle[i][j]; // And here
}
}
return puzzle;
}
What did I do wrong? Is the issue due to malloc? What can I do to fix this? Can someone give me a few tips?