
December 8th, 2010, 12:02 AM
#1
Getting Possible Values (Sudoku Game)
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:
Code:
//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 readonly\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!

December 8th, 2010, 10:14 AM
#2
Re: Getting Possible Values (Sudoku Game)
The first thing you should do is to check letter and number after input. If the user types wrong coordinates (or small letter for instance) you probably would crash when accessing your table entries.
To check for possible values of a given cell, you simply could make a for loop from 1 to 9 and for each number you make a triple check on row, column and sqare whether the number already occurs. The checks can be done by an additional for loop, e. g. for row check you keep the row index fixed and run the column index and vice versa for column check.
Square is a little bit more tricky cause you need to compute the topleft coordinates of the square from the current row and column. That could be done by using integer division:
int rowTopOfSquare = (currentRow/3) * 3;
The above gives 0 for currentRow = 0,1,2 and 3 for currentRow = 3,4,5 and so on.
Do the same with columnLeftOfSqare.
Then in the check loop running i from 1 to 9 you calculate
row = (rowTopOfSquare + ((i1)/3); // division gives 0,0,0,1,1,1,2,2,2
col = (columnLeftOfSqare +((i1)%3); // modulo 3 gives 0,1,2,0,1,2,0,1,2
That way you'll get all coordinates of the given square.
If any of the triple checks finds the i already set you could break the current loop and omit the remaining checks. Only an i which passes all three loops without hit would be a possible number that you can store in a new array of 9.

December 8th, 2010, 10:45 AM
#3
Re: Getting Possible Values (Sudoku Game)
I'm a little confused. As you can tell, I'm a beginner at C++. From the above statement, this is about what I got:
Code:
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
}
}
Haha. Could you maybe explain it a little bit more? Thank you!

December 8th, 2010, 11:43 AM
#4
Re: Getting Possible Values (Sudoku Game)
Originally Posted by brycematheson
I'm a little confused. As you can tell, I'm a beginner at C++. From the above statement, this is about what I got:
Haha. Could you maybe explain it a little bit more? Thank you!
Perhaps you should first think about what the program should do. Take an example and work it out on paper. Then try to find a general form of the algorithm you use and work that out. Then check if it works in all cases. Before you've done all that you shouldn't be writing a single line of code.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it.  P. D. Ouspensky

December 9th, 2010, 04:27 AM
#5
Re: Getting Possible Values (Sudoku Game)
Originally Posted by brycematheson
I'm a little confused. As you can tell, I'm a beginner at C++. From the above statement, this is about what I got:
Code:
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
}
}
Haha. Could you maybe explain it a little bit more? Thank you!
The above nested loops would run 81 cycles, i. e. they were suitable to examine each cell in your sudoku game. Your current q. however asked for the possible numbers for a given empty cell, right? The examination of the whole table won't help for taht, right?
So, you better read my post more thoroughly. It describes what you could do below comment
//Displays the possible values based on the coordinates the user entered
and the description isn't  mainly  C++ code but a verbal description what you could do to find the possible numbers. It is now your task to work out the C++ code for that.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
OnDemand Webinars (sponsored)
