|
![]() |
|
| Author |
|
|||||||
|
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.. |
|||||||
| #0 07:04pm 28/10/05 |
|
|||||||
|
system
|
--
|
|||||||
| #0 |
|
|||||||
|
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'. |
|||||||
| #1 07:06pm 28/10/05 |
|
|||||||
|
rolo_tomasi
Posts: 1163
Location: Sunshine Coast, Queensland
|
sudoku = fun.
|
|||||||
| #2 07:17pm 28/10/05 |
|
|||||||
|
eXemplar
Posts: 1365
Location: Brisbane, Queensland
|
What language is this in ?
|
|||||||
| #3 08:05pm 28/10/05 |
|
|||||||
|
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 |
|||||||
| #4 08:49pm 28/10/05 |
|
|||||||
|
habib
Posts: 350
Location: Brisbane, Queensland
|
This should get you started then (let's see if this works)...
|
|||||||
| #5 11:45pm 28/10/05 |
|
|||||||
|
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 |
|||||||
| #6 03:23am 29/10/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 |
|||||||
| #7 11:14am 29/10/05 |
|
|||||||
|
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
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 |
|||||||
| #8 12:18pm 29/10/05 |
|
|||||||
|
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
|
|||||||
| #9 12:20pm 29/10/05 |
|
|||||||
|
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 |
|||||||
| #10 04:17pm 29/10/05 |
|
|||||||
|
habib
Posts: 353
Location: Brisbane, Queensland
|
Changing the output of the co-ordinate system is easy:
As for ignoring zeroes, just add a
before the printf messages which say there are duplicates/missing values etc. |
|||||||
| #11 04:09pm 29/10/05 |
|
|||||||
|
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 |
|||||||
| #12 04:29pm 29/10/05 |
|
|||||||
|
system
|
--
|
|||||||
| #12 |
|
|||||||
|
| ||||||||