SuDoku algorithm blues <-- HELP!
Posted: 09 Jan 2007, 14:06
So its varsity holidays, I'm bored and teaching myself C#. and just for the hell of it making another Sudoku game. So my issues lie in populating the grid, how am i doing this? using a random number generator as I cycle through the grid checking if each number is allowed there and if not trying another, a bit simplified but you get my drift. this works fine except that for some ... ... reason the validation algorithm below doesn't quite work as it should, if you comment out any 2 of the 3 sections, the remaining section appears to work but it doesn't work all together.
Heres the validation checking method:
excuse the underscores PHP NUKE was having a problems with the square brackets indexing my array thinking that they were tags
please someone tell me what the ---- im doing wrong because im stymied!
so any and all comments will be accepted!
Heres the validation checking method:
Code: Select all
* r is the value of the current grid position to check
* x & y are the current grid co-ordinates
*
private bool validgridposition(int r, int x, int y)
{
* check all x-axis values up till current position*
for (int xi = 0; xi < x; xi++)
{
if (grid_y__xi_.getValue() == r) return false;
}
*check all y-axis values up till current position*
for (int yi = 0; yi < y; yi++)
{
if (grid_yi__x_.getValue() == r) return false;
}
*creates a sub grid to search
* eg: if a 9x9 grid,
* should produce a 3x3 subgrid
* containing x and y
* where segments = sqrt(length of 1 side of the grid)
* as all elements exist im not getting nulls*
int xoffset = (x / segments) * segments;
int yoffset = (y / segments) * segments;
for (int ys = yoffset; ys < yoffset + segments; ys++)
{
for (int xs = xoffset; xs < xoffset + segments; xs++)
{
if (!((xs == x) && (ys == y)))
{
if (grid_ys__xs_.getValue() == r)
return false;
}
}
}
* if all checks pass*
return true;
}
please someone tell me what the ---- im doing wrong because im stymied!
so any and all comments will be accepted!