both...
Printable View
both...
help her on.
ahaha
cool.
btw, the main thing is it allow user to enter 16 numbers without repetition at each column and row?
Lemmi guess, you guys are in the same class? And it depends, the sodoku may be larger than 16x16. A sodoku solver, from my point of view, should solve a pre-set/pre-loaded incomplete sodoku.
Opinions may vary, but mine is that a sodoku solver should be able to complete an incomplete sodoku either loaded to the program, or defined by the user at run-time.
meaning I need to came out with an algorithm of the game itself first?
Well what do you have so far, design wise?
just started the design phase..
but still kinda stuck with the whole thingy.
what kind of condition should I assign to in order to check if the user repeated the number?
I've been thinking - creating a sodoku solver would be difficult without using arrays? I can't seem to find a way without arrays, using arrays is still pretty tough!
I've written a sudoku solver before, but I should mention that I wrote it out of my head without doing any research first. I'm reasonably good at solving sudoku puzzles, but I'm sure you'd find more waterproof methods if you looked around.
Anyway, the method I used went something like this. I represented all of the data in the puzzle as a two-dimensional array, and applied various algorithms to the puzzle until it was solved or the puzzle was unchanged (in which case the program wasn't good enough to solve it). The algorithms would be strategies that you might use when solving a sudoku puzzle by hand; e.g., if eight of the nine numbers in a row are filled out, then the last number is whichever number does not appear in the row.
As I said, it's a reasonably good method, but not perfect. For one thing, it doesn't completely model how a human sudoku-solver will think. A lot of the thought process goes something like this: "if I put this 7 here, would it create an impossibility?" Here an "impossibility" is something like two 7's in the same quadrant, column, or row. If it is impossible, you rule out that move, which narrows down your choices; maybe since the 7 can't go there, it has to go somewhere else. And so on.
I never did look at a "real" sudoku-solving program. There are lots of them online that profess to be able to solve any sudoku puzzle (which is certainly possible). If you want more ideas you could see how some of them work.
Well, here I was talking about automatically solving sudoku puzzles. You'll have to forgive me if that's not what you want, since you haven't been very clear on that point. Are you just trying to create a sudoku game that ensures that every move the player makes is a valid one?
That's pretty easy. When the user asks to place a '7' in a particular spot, you just scan that row and column for other sevens, and that quadrant too. I'm not sure if "quadrant" is quite the right word, but you know what I mean. Here's the sort of code you might use.
That assumes you use -1 or something to represent a square which has not been given any number.Code:/** Check to see if the user made a valid move. The user is assumed to have
attempted to place the number @a number at position @c data[x][y].
*/
int is_valid_move(int data[9][9], int x, int y, int number) {
/* The x and y coordinates of the upper-left corner of the quadrant
in which the position (x, y) lies.
*/
int qx = (x / 3) * 3, qy = (y / 3) * 3;
int i, j;
for(i = 0; i < 9; i ++) {
/* check this column */
if(data[x][i] == number) return 0;
/* and this row */
if(data[i][y] == number) return 0;
}
/* check this quadrant */
for(int i = 0; i < 3; i ++) {
for(int j = 0; j < 3; j ++) {
if(data[qx + i][qy + j] == number) return 0;
}
}
/* The number wasn't duplicated in the column, row, or quadrant;
therefore, this must be a valid move.
*/
return 1;
}
Anyway, enough blathering. If you want more help you'd better describe what you're trying to do.
In relation to the solution dwks said, I implemented a way for the solver to check if there are any numbers that absolutely cannot be any other number than a single number. After each move, since each number placement could determine the placement of another number.