PDA

View Full Version : C Sudoku solving program


RubberSoul_Guy
November 1st, 2005, 07:16 AM
okay firstly, i am in dire need of some help. This will probably only make sense to you if you're a progammer and know a little about C programming.

I was trying a little exercise the other day to write a program in C that solves sudoku puzzles. I'm pretty much stumped and was wondering if anyone here could help me on my way. I've been provided with some starter code which i'll write below, plus the task of writing a few methods. I'm just throwing this out there, incase there is anyone who would be kind enough to help me.

Okay this is the code i was provided with.

STARTS

#include "sudoku.h"
#include <ctype.h>
#include <stdio.h>
#include <assert.h>


Puzzle Guardian24June2005 =
{
" 19 3 ",
" 2 ",
"76 2 9",
"3 6 5",
" 21 34 ",
"4 9 3",
"1 3 97",
" 4 ",
" 5 86 "
};

Puzzle PandJ25June2005 =
{
" 2 74" ,
" 5 96 ",
"7 58 2",
" 3 91 ",
" 267 5 91",
" 68 4 ",
"9 31 6",
" 18 4 3 ",
"2 7 "
};

Cellgroup group[27];

/* this should return a solution to the puzzle in Board B */
Board solve(Board B){
static int ntries = 2;
static Board failure ;
Board res;
//if (ntries-- <0) return B;
if (isSolved(&B)) return B;
if (! isFailed(&B)){
int row, col, k;
for ( row = 0 ; row < 9 ; row++){
for ( col = 0 ; col < 9 ; col++){
for ( k = 1; k < 10 ; k++){
if ( B.board[row][col].npossible > 1
&& B.board[row][col].possible[k]){
Board b2 = B;
setvisible(&(b2.board[row][col]), k);
rework(&b2);
res = solve(b2);
if (isSolved(&res)) return res;
}
}
}
}
}
return failure;
}
/* insert functions below */

ENDS

I pretty much understand this but not quite. If anyone could give a brief description it would be magic.

Then the methods:-

The first one requires i write a method:-

void Boardfill(Board * B);

Boardfill visits each cell, and sets
1.visible to ' '
2.all possible to 1 (true).
N.B. possible[0] is defined but ignored; only possible[1] .. possible[9] are meaningful.
3. npossible to 9

This is just one of about 12 methods i have to write. (but don't worry i want to do the rest myself, i just need help starting)
Can anyone please have a go at this and return with what they have.

Heres hoping!! Thanks to anyone in advance.

Keith
November 1st, 2005, 05:55 PM
Wow.. Outta my league.. CC is one to help out for coding..
I'll see if she can have a look at this to help you out.

CyberCobre
November 1st, 2005, 06:03 PM
What don't you understand?

2nd_coming
November 2nd, 2005, 12:24 AM
I think before you try to make programs in C++ you should 1st learn it, but a book or download one or something, I tried sometyhing similar with PHP Hypertext Protocol b4, could never get my head around databases, but I was TeH 1337 with txt files, but that was 3 or 4 years ago, so, my brain is pretty much rinsed.

RubberSoul_Guy
November 2nd, 2005, 04:58 AM
Im just not sure how to go about writing this method. I understand that the method i was provided with solves any sudoku problem it is given, and that my task is to set up the board and whatnot, but i just need some help in starting. Should i just use a for loop like in the method provided to visit each cell and set it to ' ' ?

Could you have a go so i can study it and try and work it out?

The other thing is that i'm not quite sure about is what the k is being used for. I understand seaching each row and column, but its not clear to me what purpose that int k is serving.

thanks so much for any help.

RubberSoul_Guy
November 7th, 2005, 10:32 AM
Okay im not sure if people have given up on this thread or not but i'll post this anyway.

This is what i have for the Boardfill() method.

void Boardfill(Board * B)
{
int row, col, k;
for(row = 0; row<9; row++){
for(col = 0; col<9; col++){
Cell * c = &(B->board[col][row]);
c->visible = ' ';
for(k = 0; k=10; k++);
}
c->npossible = 9
}
}


what do you think of that? Does it look right?
and now im stuck on this one.

void setvisible(Cell * C, int val);

use assert to check that val is between 1 and 9 inclusive.
This function is used when we know the final value of a cell.
The function should:-
set visible to the character corresponding to val
set possibility[val] to be 1 (true) and the rest 0 (false)
set npossible to 1



Any clue how to go about this?

thanks in advance.


im thinking of putting
assert((val >0 && val <10) || (val >48 && val >58)) /* to deal with ASCII */

in somewhere but after that im stumped. Got any ideas?