How To Write A Sudoku Program In Java

Posted on admin

This blog provides tips and techniques on Java Programming. How to write a sudoku generator? It is a good program to generate Sudoku puzzles. Okay guys, I'm writing a program that will find the possible solutions to a sudoku puzzle. It's simpler than it sounds. The objectives are: Take a. Sudoku game using Java. I wrote the original code for my Sudoku program using Visual Basic on Microsoft's Visual. Let's hope it will be “Write Once.

  1. Give More Feedback

When i compile GraphicallyRepresentation.java in JCreator LE 5.00.007, there is a caution came out. “Note: C: Java GraphicallyRepresentation.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.” I do not have problem with Logic.java. My knowledge about Java not too high and i don’t understand to solve it. One more thing, there is some minor problem when i run the project which is Option button, Setting button, Help button is not working.

Please help me. Thank you brother.

. Introduction This is the code-behind to generate / solve / use a Sudoku grid.

I won't describe or provide the code to display a grid because there are millions of ways to do that and I will let you decide how you want to do it. But, I will provide an executable with an interface to demonstrate how it can be used. Background This code was written with speed in mind. Using the Code In the zip file, I have provided a project file, a module and four classes. The code inside the class could be moved into the module; since the function / sub inside them are shared, I decided to split it that way so it's clearer.

Let's start with the project file; there is one major thing inside it: I enabled 'Remove integer overflow checks' in the advanced compiler settings, everything else are the default settings. Louwin 31-Dec-09 22:56 31-Dec-09 22:56 I was interested in your code to implement in a C# Sudoku game I enjoy playing so was interested in transalating it to C#.

Meanwhile I generated and played a few games. They tended to end in stalemates, multiple solution situations. I fed one of your games into mine (downloaded of Sourceforge and GPL) and it couldn't rate its' difficulty, whatever that means. So I generated a few more games and selected 'Verify' off your own game menu. They all came back 'False', again, whatever that means. I had intended to replace the generation logic in my game (Sourceforge, GPL and my modifications) because I suspected the generation logic was too slow.

I have since found that the generation logic isn't fast but it isn't that slow either, it was the game difficulty setting routine that was taking almost ALL the time. My game was generationg a game then removing some numbers to achieve a 'hard' game. In this process it generates a game, removes some numbers (59 as the case is) and plays the game intelligently to guage it difficulty level. It then replaces some of the numbers (too hard) or removes some more numbers (too easy) to try to achieve the required difficulty level. It then replays the game and sees if the difficulty level is as required and repeats and so on and so on.

Now I have to search for faster game difficulty setting logic. I may be wrong about your generation algorythm and multiple solution tendencies but I don't think so. Unless I was just unlucky with the games I generated and played. I agree, your code is extremely fast. I projected that it would generate thousands of grids a second on my HP 624MHz PDA. I tried to implement your VB code on VS2008 for my PDA but, being a newbie at VS2008 and VB, I found it beyond my experience. Plus the non inclusion of an output routine in the downloadable code was a major huddle.

I am in the process of timing the C# code I'm working on and expect the routine to take a few milliseconds not microseconds. But being a newbie in C# as well I am having trouble with this too. I have been a programmer for 30 years but mainly assemblers, COBOL and BASIC. I accept that it is the number removal logic that is at fault not your grid generation. (My?) removal logic can take 21 MINUTES, on occassion, to run on the VS2008 emulator.

I've worked out how to time routines to minutes and seconds and am still trying for more detail (milliseconds?). What does the 'false' Verify mean?

I repeat, after grid generation if you select 'Verify' the game replies 'False'. Thank you for making your code available. I wish it was in C# though. I have been mis-using your 'Verify' option. I have been using 'Verify' as a 'Check Validity' facility. I am used to a program which, say after entering a few numbers, you select 'Check Validity' (Verify) and it replies - No Solution, One Solution or Multiple Solutions. From the questions you ask I now know (I think) that your verify option only works to check the entire solution - all squares filled out correctly.

I apologise, I misinterpreted the use of your 'Verify' option. I saw it as a verify the grid as filled in so far. Hello again, Thanks for the link to the conversion routine. If I remember correctly (I had tried a couple of converter sites) it has problems converting the VB source because of the REDIM requirement.

Probably in the logic between the 9x9 and the 16x16 modes. I suppose I should really have stressed the PPC aspect of my question. Plus the converter sites doesn't convert 'proj' file. So what I was after is a converted C# project (including a '.csproj' file) AND how to implement it on a PPC (WM2003).

(Jeez, he doesn't want much does he?). Thanks again for making the code available, it is very good and very fast. I confirm, there are conversion errors in cSudokuGridSolve.vb and sudoku.vb. I include the conversion errors in cSudokuGridSolve.vb below as an example:- - line 50 col 13: Not supported in C#: ReDimStatement - line 54 col 13: Not supported in C#: ReDimStatement - line 65 col 9: Not supported in C#: ReDimStatement - line 66 col 9: Not supported in C#: ReDimStatement - line 199 col 13: Not supported in C#: ReDimStatement I expect this shouldn't be problem in implementing the grid generation routine but would be a problem in trying to implement the existing program on the PPC.

Not even considering the missing output module. I have printed out your article in the hope of trying to understand the grid generation logic while vegitating in front of the TV. Thanks again for making the code available. Thanks for that. It was very good of you to go through all that trouble for me. I will load it into VS2008 and try to understand it and implement it in my code.

ProgramJava

JFYI I timed my routines. 7seconds to generate (fairly constant time over about 6 runs, one run returned 10seconds).

Difficulty setting logic range from 4seconds to 7minutes ( 1 run at 4s, two runs at 52s, one run at 1m54s). The generate logic randomly loads the first number then 'bruteforce' solves it. If there is 1 solution it does the next number. If there are no solutions it backtracks and changes the number. If there is 1 solution it then brutesolves the grid setting the remaining numbers. This routine seems to run between 63 to 68 loops before it finds a valid grid but mostly 65 loops (don't ask me why). This probably explains the 7s generate.

Give More Feedback

It seems to do 2 to 8 backtracks. All this was done on my old system where I have the 90day trial VS2008 loaded.

All these timings were made on the WM2003SE emulator. I will try to implement your algorythm but this will only save me 7 seconds. My real problem is the difficulty setting routine. This has been known to take up to 20minutes to run. I am doing this because the program I'm using has fabulous game play. I haven't seen its' features in any other Sudoku I've seen and I've seen a fair few. Thanks again for all your hard work.

You seemed to have used the DeveloperFusion site to convert the code but your conversion is different from mine (at the same site). On mine it puts the ' on the same line as the 'for' and 'if' etc. I eventually sat down with your routine and nutted it out. I realise you are programming for two separate sizes hence the naming conventions.

I replaced squareExp2 with Dimension and squareExp3 (which should have been squareExp3M1 (minus 1)?) with fullGrid and it started to make more sense. Then, because i'm only interested in 9X9, I replaced ALL the.square. variables with the 9X9 values and things became very clear. I understood gridRow and grdiCol fairly quickly but gridbox and gridbox1 (should have been gridBox and gridBox1?) took a couple of minutes. I don't understand why you chose 10 tries (so close to dimension) or why you backtrack 7 or 16 cells (looks a bit drastic, why not 1 and 2?). I suppose how often the routine backtracks will tell you what effect such big backtracks have on the routine timing. I know you mentioned the 7 and 16 backtracks specifically in your article.

I will replace the generation logic with yours but it will take an interface routine as the original author of my(?) routine was allowing for 2X2, 2X3, 3X2, 3X3, 3X4, 4X3 and 4X4 sizes and declared the 'Board' class using Lists of Lists. I used the debugger to extract the values of gridRow/gridCol/gridbox/gridbox1 and will predefine these (replacing the need for initArray). Your article says 'I wrote a function that verifies if the grid is valid. You need to pass the grid to the function and it will return true/false.' Does this say the the grid generated might not be valid?

Or have multiple solutions? Thanks for your article and making your code open source. Unlistedemail24@gmail.com 11-Apr-09 12:54 11-Apr-09 12:54 I spent a lot of time trying to write a grid generator and finally gave up and just copied your code verbatim as it obviously works brilliantly, but for the life of me I cannot figure out what the hell your code is doing The lack of comments, cryptic variable names and VERY cryptic logic make this a really tough piece of code to follow. I'd hate to be a maintenance programmer in your shop! Works really fast though, so totally way to go. You're obviously a lot smarter than I am!

One question though, what's the reason you didn't use a matrix array? Would addressing by row,col been simpler than offsetting your way through the array?

Give

Thanks for the code. I'm going to use it. Just wish I knew what it's doing! Thanks, I think For you question; In the beginning, I had (row)(col)(box) array then I dropped it into (row)(col) array and finally (grid) array the only reason was: speed.

But it added a bit of complexity in the code, best example is the gridbox logic, that one is really. Ok, I'm 100% with you on that one between the 3-2-1 dimension transformation I gained a huge performance boost, in the order of 10% to 20%.

If you have any specific question, just ask, I will gladly update my article. Yes, there was a compliment in there somewhere I'm retired now (at a modest 45 years old), but in my misspent youth I did a lot of contract programming for large corporations. In that environment one has to assume that the programmer coming after you is dumber than you are, and that you're pretty dumb to begin with. Much as this sounds bad, it actually creates an environment where the code is explained in some detail through comments, and the variable names are as descriptive as possible, to improve the readability of the code. Use of constructs such as 'i -= squareExp2Mult2 - 2' and variables such as 'x' would get you spanked pretty hard. One has to write code that can be understood, read and reused, not code that's just really good (like yours was!). If you happen to have that version using the matrix array instead of a 1D array, I'd love to see it.

I just couldn't get my backtracking logic working, so I'd like to see how a smart guy does it. I just looked into my backup files and I can only find a version with (row)(col)(box) array, a VERY old one, the code is too much ugly to be moved out of my backup files I created the (grid) array version 3 days after I started rewriting my logic from the version above. Which mean, I don't have the version of (row)(col) - I normally comment my code when I need to remember 'why' I did this or that, which in this case I don't have to since, for me, it's obvious. But don't worry, your not the first one asking me to write more comment in my code, many colleagues did ask me to write more, I always had problem commenting something I understand.

I hope you will understand Writing this article took me a while only because of that problem and I'm currently writing another one, look at the post below for me information. for 'i -= squareExp2Mult2 - 2', I should have wrote, for this article, 'gridPosition -= (squareExp2Mult2 - 2)' and the reason for the '- 2' is; I did many benchmark with no minus, with '+ 1', with '- 1', with '+ 2', with '- 2', with '+ 3', with '- 3', etc etc etc and the fastest one was '- 2'. Member 4169159 11-Mar-09 12:18 11-Mar-09 12:18 First off, great work.

• No ads or bloatwares. Usb vcom driver for mtk 8127. • No hidden fee in MTK Drivers.

Very easy to follow and the expanations were very well done. Ran the demo sample that was provided and was very impressed on how fast and on the visual portion of how the data was being displayed. I know you stated that you were not going to get into the display portion as there were many ways to do this.

I was just curious what methods you used in your demo example. Are you just drawing lines over a panel and then using graphic fonts to draw the numbers, or are you using some other method. Thanks, Brent.