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.
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.