Once again, I'm having trouble with my Sudoku Game. This is the last part and I'm almost there! I just need a little bit of help. According to the rules of Sudoku, there can only be once instance of each value in each column, row, and 3x3 grid. I'm writing a function which will tell you the possible values you can input in each spot. I'm having problems.

What I'm trying to do is write all the values of the row into one array, and then all the values of the column into another array. Then, compare the two arrays and output the numbers that don't exist in either of the two. This is what I have so far:

//Declare variables
   char letter;
   int number;
   int rowNotPossibles[9];
   int colNotPossibles[9];
   int possibles[] = {1,2,3,4,5,6,7,8,9};

   //Gets letter/number coordinates
   cout << "What are the coordinates of the square: ";
   cin  >> letter >> number;

   //Converts letter to uppercase otherwise
   //ASCII conversion in next step won't work
   letter = toupper(letter);

   //If square is not equal to zero, display "read only" message
   if (sudokuBoard[letter - 65][number - 1] != 0)
   {
      cout << "ERROR: Square \'" << letter << number << "\' is read-only\n";
      cout << "\n";
      getOption(sudokuBoard);
   }
   else
   {
      //Gets the current column and row
      int currentColumn = (letter - 65);
      int currentRow = (number - 1);

      for (int i = 0; i < 9; i++)
      {
         colNotPossibles[i] = sudokuBoard[i][currentColumn];
         rowNotPossibles[i] = sudokuBoard[currentRow][i];
      }

   }
   //Displays the possible values based on the coordinates the user entered
   cout << "The possible values for \'" << letter << number << "\' are: ";

   cout << "\n\n";
   getOption(sudokuBoard);
}

I'm lost. Any help? I'm also absolutely completely and entirely clueless on how to compare it to a 3x3 grid. Any help on that would be greatly appreciated, also. Thank you in advance!

There's more than one way to do it. Myself, I would set up a boolean array of size 9. Set all elements to true. Now go through the column. Set the array for each element in the column to false. Then do the same for the row. Set the flag false for each element in the column. Then do the grid. After that, any true index of the array represents a valid entry for that box.

You compare the grid by figuring out the relevant columns and rows using a little integer arithmetic:

int minCol = (col / 3) * 3;
int minRow = (row / 3) * 3;

Add two to each and you get the maxCol and maxRow by adding 2 to minCol and minRow.

Now loop through the elements:

for(rowIndex = minRow; rowIndex <= maxRow; rowIndex++)
{
    for(colIndex = minCol; colIndex <= maxCol; colIndex++)
    {
        // do something
    }
}

I'm using 0 through 8, not 1 through 9, so adjust accordingly if needed.

Saw row 4, column 6 is going to have minRow = 3, maxRow = 5, minCol = 6, maxCol = 8. Loop through the boxes and check, just like with the rows and columns.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.