top_left top_right
bottom_left
Next Event: Unknown | Forum Rules | QGL Website | Event Registration
openFolder AusForums.com
iconwatfolderLineopenFolder LANs
iconwatfolderLineopenFolder QGL
iconwatfolderLineopenFolder QGL Forum
Author
Topic: Sudoku logic checker (programming hlp)
autolux
Posts: 228
Location: Queensland
Hey All,

I'm a semi-spastic when it comes to programming, but I have an assignment coming up where I have to check sudoku solutions... I never actually bothered to try these puzzles in the paper, but yeh.. basically have to write a console app with a function that will ask for the values for each square store it in a file, and check for a solution, and I suppose either return yes it works, or the coordinates of the square that fails the puzzle..

The file io stuff is messing me up, I'm only up to the checker and haven't had any luck and everyone keeps telling me, 'its done just by logic! its so simple!' but im lost as to how to tackle it... google has only yielded people programming window apps for it, so its not all that useful as I have to workout a grid system and print it to console etc..

If anyones familiar with programming or sudoku is willing to help me through the woods that would be most excellent..
system
--
Strik3r
Posts: 1165
Location: Gold Coast, Queensland
your going to need to give us a little bit more to work with than that....

www.rafb.net/paste is your friend for pasting code. Why don't you paste what you have and tell us what functions your having trouble with instead of saying 'do my assignment kthxbi'.
rolo_tomasi
Posts: 1163
Location: Sunshine Coast, Queensland
sudoku = fun.
eXemplar
Posts: 1365
Location: Brisbane, Queensland
What language is this in ?
autolux
Posts: 229
Location: Queensland
It is for a C++ course

I just want to know how its done, its embarassing it is so simple but im stuck. I've scrapped most things I've tried as I'm confused firstly with the actual question wording which is in engrish... and secondly with this file inout stuff, as I have never used it. I just tried the example for filestream in the text book, and it gave a compile error also, so thats pretty helpful lol..

After reviewing the question a bit more, what it is basically asking is a lot simpler than a full sudoku checker, it requires an input file (ie. sudoku.in) and to write a function which reads the file, and then checks columns and rows to see if any number has been used more than once ie. if it is a solution or if its wrong, and where.

i made a file sudoku.in put in the following which is the incomplete sudoku at sudoku.com

060104050
008305600
200000001
800406006
006000300
700901004
500000002
007206900
040508070

With 0's as spaces and hence not included in the scan.. and thats where I'm at, I'm not sure that will even workout properly.. I'm at a loss as to how to implement a solution.. I need to workout the file in/out thing, and I guess read those numbers into a 2Dimensional array and also give each position co-ordinates starting at (1,1), and call a checking function on them.. and then output results..

really the sudoku part is irrelevant, its just scanning a 2D array for repeated numbers..


last edited by autolux at 20:49:53 28/Oct/05
habib
Posts: 350
Location: Brisbane, Queensland
This should get you started then (let's see if this works)...



#include "windows.h"
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"

int main(int argc, char* argv[])
{
int nSodukuArray[9][9];
ZeroMemory(nSodukuArray,sizeof(nSodukuArray));

//read file into array
char sLineBuf[256];
FILE *f = fopen("soduku.in","rt");
int row = 0, col = 0;
while (fgets(sLineBuf,255,f))
{
for (col=0; col<9; col++)
{
nSodukuArray[row][col] = sLineBuf[col] - '0';
}
row++;
}
fclose(f);

//check rows
for (row = 0; row < 9; row++)
{
//...
}

//check columns
for (col = 0; col < 9; col++)
{
//...
}
return 0;
}
Insom
Posts: 214
Location: Brisbane, Queensland
I tried something like this earlier this year, ie. a sudoku solver, in VB.NET but the principle is the same. It's not clear whether your program is supposed to actually solve games.

Search Google for sudoku techniques, the same ones people use to solve the game on paper - "striping", et cetera. This is the best place to start - it is helpful to think of a blank square as maintaining a list of numbers that it could possibly be, so that when the list is reduced to one, that number is drawn in the square.


last edited by Insom at 03:23:15 29/Oct/05
autolux
Posts: 230
Location: Queensland
thanks habib, i tried messing around with that still a bit confused..

i was trying to file io stuff, which i think is working, now i just don't know how to read the 2D array, and then scan it for 1-9 appearing >1 times..

http://www.rafb.net/paste/results/WTmNC052.html
habib
Posts: 351
Location: Brisbane, Queensland
That looks ok except you have put char value of each of the number into the int array, not ints. So when you do nArray[x][y] = fgetc(), fgetc() returns a char (say '0') and this is converted into an int for you by the compiler (0x30 for '0'). If you subtract '0' from the value returned by fgetc(), then you will be putting the integers 0,1,2,3 into the array instead of the integer value of the characters '0','1','2','3' etc. (Think ASCII tables).

So just change line 13 to read


nSudokuArray[i][j] = fgetc(fin) - '0';

and then you should be ok (and your code will be functionally equivalent to mine, just with a little more error checking).

As for checking the array for duplicates once you're finished, I'd write something like this:

http://www.rafb.net/paste/results/uDlXYc89.html

habib
Posts: 352
Location: Brisbane, Queensland
Oh, just realised you didn't care if 0 appeared multiple times - that should be pretty easy to fix
autolux
Posts: 231
Location: Queensland
cheers, thats a bit clearer now, is the map library part of c or c++, better lookup on that documentation for stuff...

i hyrbridised the two, changed nSudokuArray to just a for clearer output

http://www.rafb.net/paste/results/oss9HX59.html

only issues i have to sort out i guess, is still ignoring 0's for spaces and the printout of the array coordinates, starting at [1,1] is a requirement.. tried having a[i+1][j+1] and a few things like that, but didnt seem to work..


last edited by autolux at 16:17:58 29/Oct/05
habib
Posts: 353
Location: Brisbane, Queensland
Changing the output of the co-ordinate system is easy:


printf("a[%d][%d]=%d ", i+1, j+1, a[i][j]);


As for ignoring zeroes, just add a


if (!Number)
continue;


before the printf messages which say there are duplicates/missing values etc.
autolux
Posts: 232
Location: Queensland
argh doh, +1 in the output not the input!

sweet, just fixed up the coord numbering and output col/row numbering.. looks good to go.. thanks for all the help!

last edited by autolux at 16:29:40 29/Oct/05
system
--
Not a new post since your last visit.
New Post Since your last visit
Back To Forum
Advertise with Us | Privacy Policy | Contact Us
© Copyright 2001-2026 AusGamers Pty Ltd. ACN 093 772 242.
Hosted by Mammoth Networks - Australian VPS Hosting
Web development by Mammoth Media.