brycematheson

I'm creating a Sudoku game, and according to the rules of Sudoku, there cannot be the same number in any row or column. My question is this: How do I compare a value with an entire row or column in an array?

For example: I have a 9x9 array that looks like this (a '0' indicates an empty space). Say I place the value '8' in the coordinates 'B2'. How would I check with the entire row '2' and the entire column 'B' to make sure that there isn't a value of '8' already there?

A B C D E F G H I
7 2 3 0 0 0 1 5 9
6 0 0 3 0 2 0 0 8
8 0 0 0 1 0 0 0 2
0 7 0 6 5 4 0 2 0
0 0 4 2 0 7 3 0 0
0 5 0 9 3 1 0 4 0
5 0 0 0 7 0 0 0 3
4 0 0 1 0 3 0 0 6
9 3 2 0 0 0 7 1 4

Much appreciated. :)

thelamb 163

The board is small, so you don't need any fancy search strategy...
Take this as example:

``````int row1[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 2 };

bool isInRow = false;
for( int i = 0; i < 9; ++i )
if( row1[i] == NewNumber )
isInRow = true; // it is in the row

if( isInRow )
; // show an error``````

So this is just checking one row.. It shouldn't be too hard to change this to check in a 2d array, depending on where the NewNumber is added.

B2 would be the second column and the second row.
Assume your board is defined as int[9][9] you want to check int[1] and int[1].

Fbody 682

Do you know how to traverse a single-dimensional array?

``````const int SIZE = 6;              //declare a constant for the array size

int myArray[SIZE] = {0};         //declare and initialize the array

for (int i = 0; i < SIZE; ++i) { //traverse an array/row
myArray[i] = i;
}``````

You can use 2 nested for loops to traverse all elements of a multi-dimensional array:

``````const int ROW = 6;              //declare constants for the array size
const int COL = 10;

int myArray[ROW][COL] = {0};    //declare and initialize the array

for (int r = 0; r < ROW; ++r) {   //traverse the array columns
for (int c = 0; c < COL; ++c) { //traverse the array rows
myArray[r][c] = (r * c);
}
}``````

(I know 2 of the comments look "backward", but they're not...)

You can take that same concept and tweak it to apply it to a single row/column of a multi-dimensional array:

``````//to traverse a single column
const int ROW = 6;              //declare constants for the array size
const int COL = 10;

int myArray[ROW][COL] = {0};    //declare and initialize the array

int currentColumn = 3;
for (int r = 0; r < ROW; ++r) { //traverse a column
myArray[r][currentColumn] = r;
}``````

brycematheson

Okay, so how about something like this? Does this work?

``````for (int col = 0; col < 9; col++)
{
for (int row = 0; row < 9; row++)
{
if (value == sudokuBoard[row][col])
{
cout << "ERROR: Value \'" << value << "\' in square \'" << letter << number << "\' is invalid\n";
}
}
}``````

thelamb 163

Think about what those loops are doing.

Do you think it is correct?

Fbody 682

For your purposes, that won't work. That's a full traversal like my second example. For a Sudoku board, you need 2 different variations of my third example.

EDIT:
rats, lamb jumped in there again ;P

brycematheson

Okay, here is what I've come up with. Any suggestions?

``````{
int currentColumn = (letter - 65);
int currentRow = (number - 1);

for (int i = 0; i < 9; ++i)
{
if (sudokuBoard[i][currentColumn] == value)
{
cout << "ERROR: Value \'" << value << "\' in square \'" << letter << number << "\' is invalid\n";
cout << "\n";
getOption(sudokuBoard);
}
else if (sudokuBoard[currentRow][i] == value)
{
cout << "ERROR: Value \'" << value << "\' in square \'" << letter << number << "\' is invalid\n";
cout << "\n";
getOption(sudokuBoard);
}
}``````

Fbody 682

What are "letter" and "number"? Are they some sort of user inputs?