Hello guys.

I decided i was going to make a sudoku solver in visual c++. So far I have a program which can solve easy/medium difficulty sudokus. I still have to implement one more piece of logic, as well as the whole brute force method if a guess is required to complete the puzzle.

I would say im fairly new to programming, I have only done about a terms worth of c++ at university. The thing is my code at the moment is not even nearly finished, but it is getting very long and messy.

I have been looking around the internet and found a site which seems quite usefull.

http://compprog.wordpress.com/2007/10/31/sudoku-solver/

I'm not sure wether its because he declares constants at the start and uses these throughout, but the code seems a bit too complex for me to understand at the minute. Could anyone explain some of the concepts he uses to avoid large and unecessary pieces of code?

For example, once my program has found out which number goes into a certain square. It will pass the number and its x and y coordinates to the function below. The function then checks within the 3x3 block and calls another function 'blocklist'. This basically deletes the number from the possible list of values of all the other cells in that 3v3 grid.

Code:

void block(nodeptr pointerarr[9][9], int y,int x, int value)
{
int i,j;
if(x <= 2){
if (y <= 2){
for (i=0;i<3;i++){
for (j=0;j<3;j++){
blocklist(pointerarr,value,i,j);
}
}
}
else
{
if (y <= 5){
for (i=0;i<3;i++){
for (j=3;j<6;j++){
blocklist(pointerarr,value,i,j);
}
}
}
else{
for (i=0;i<3;i++){
for (j=6;j<9;j++){
blocklist(pointerarr,value,i,j);
}
}
}
}
}
else
{
if(x <= 5){
if (y <= 2){
for (i=3;i<6;i++){
for (j=0;j<3;j++){
blocklist(pointerarr,value,i,j);
}
}
}
else
{
if (y <= 5){
for (i=3;i<6;i++){
for (j=3;j<6;j++){
blocklist(pointerarr,value,i,j);
}
}
}
else{
for (i=3;i<6;i++){
for (j=6;j<9;j++){
blocklist(pointerarr,value,i,j);
}
}
}
}
}
if(x >= 6){
if (y <= 2){
for (i=6;i<9;i++){
for (j=0;j<3;j++){
blocklist(pointerarr,value,i,j);
}
}
}
else
{
if (y <= 5){
for (i=6;i<9;i++){
for (j=3;j<6;j++){
blocklist(pointerarr,value,i,j);
}
}
}
else{
for (i=6;i<9;i++){
for (j=6;j<9;j++){
blocklist(pointerarr,value,i,j);
}
}
}
}
}
}
}

Im obviously doing something wrong!! (or stupidly )

Thanks for reading