Netbeans JUnit Tests Tutorial

I'm assuming that you are working on a project and would like to create a JUnit test file using Netbeans. For your information, I'm currently using Netbeans 6.7. My simple tutorial here will be using SudSolver v0.23(the download link is on the right bar).

1. Right click on the file that you wish to test -> Tools -> Create JUnit Tests (shortcut key: Ctrl + shift +U)
2. Click ok. The class name needs to be appended with "Test". Other than that, it's ok to change the class name though it's recommended to stick to the default. As for the checkboxes, you can leave them as they are. You can modify them those things in the test file later anyway.

3. Modify the class however you want it. I recommend reading the following JUnit tutorial. You may refer to my SudokuTest.java to see how I implemented it.

4. Once you're done, right click on the test file -> Test File (shortcut key: Ctrl + F6) or right click on the file that you're testing -> Test File. The difference between the two methods is that the latter will run all test files associated with the file being tested.

SudSolver v0.23

I created the JUnit test file to replace my older testing methods. View the tutorial for JUnit test file to see how it works.

In the next version, the random puzzle button and the brute force search should work correctly(I hope).

Random puzzle and brute force search

I have been trying to implement the "random puzzle" function. So far, I have two versions of it in my program and both are behaving in a weird manner.

After some investigations I found out that my brute force search function isn't correct actually. It gives the correct solution in some situations but in tough cases where the number of solved cells is low, it doesn't solve the puzzle correctly.

I should mention that there's also the possibility that my function is correct, but I didn't get the correct result because of memory restrictions(from the way I implement the function, quite a lot of memory has to be consumed to hold all the temp variables).

Anyway, I'm currently working on a correctly implemented brute force search and hopefully, in my next version, you'll also see a working "random puzzle" button.

SudSolver v0.22

I fix the Clear button after realizing that I didn't check for the case when the square has number zero. I have tried my program on a few Sudoku puzzles that I found on the newspapers and so far it has solved any puzzle within a few secs and not give me any trouble. So, for anyone looking for a Sudoku solver, v0.22 should work fine and with the Clear button fixed, you no longer have to restart the program if you wish to continue solving another puzzle.

In the future, I may implement the Random Puzzle(it's still a dummy button now) or replace it with Print Puzzle or both. I may also do something related to Shidoku and Rokudoku(hence the posts) although that may require significant changes to my program.

Changelog
v0.22(May 4, 2009)
1) Clear button now works correctly

Rokudoku

Rokudoku is the 6x6 variant of Sudoku. Rokudoku and Shidoku(4x4 variant) are known as Kids Sudoku. All variants of Sudoku which has a smaller grid than the normal Sudoku are collectively known as Sub Doku.

Below is a sample puzzle of Rokudoku. Fill in the grid so that every row, every column, and every 2x3 block contains the digits 1 through 6.

Related Links
Sample Puzzles:
1) Free Kids Sudoku Puzzle
2) Kids Sudoku Puzzle
1) Sudoku for kids

Free printable online daily Sudoku puzzles for kids:
1) Sudoku.com.au (click on Kids Sudokus on the right panel)

Shidoku, Children Sudoku, Kids Sudoku

Shidoku, also known as Children Sudoku, is the smaller and easier 4x4 variant of Sudoku. Shidoku and Rokudoku(6x6 variant) are known as Kids Sudoku. All variants of Sudoku which has a smaller grid than the normal Sudoku are collectively known as Sub Doku.

Below is a sample puzzle of Shidoku. Fill in the grid so that every row, every column, and every 2x2 block contains the digits 1 through 4.

Related links
Mathematics of Shidoku:
1) Exploring the mathematics of Shidoku
2) Taking Sudoku Seriously

Sample Puzzles:
1) Free Kids Sudoku Puzzle
2) Kids Sudoku Puzzle
1) Sudoku for kids

Free printable online daily Sudoku puzzles for kids:
1) Sudoku.com.au (click on Kids Sudokus on the right panel)

Eh? First on Google search?

Some interesting facts turned up once I included the Google Analytics code on this blog. For those that don't know, Analytics is a tool that allows you to track visitors to your web site. It allows you to see page visits, visitor's region, bounce rate, etc.

One of the few things that I found is that a few hits to my blog actually come from search engines! This means that someone did a Google or Yahoo or a search on a search engine and found my blog. Out of curiosity, I googled "sudoku strategy squeezing" and voila, first on Google search!

Ironically, my new website is nowhere near top 10, and that's after some search engine optimization. Oh well, maybe I should chronicle my Sudoku programming journey as a separate blog and see if it attracts traffic.

SudSolver v0.21 is up

I have included the Jar executable now so there's no need to compile the code. Also, I have added a way for users to input their puzzle. Unfortunately, I couldn't get the clear button to work properly so if you wish to start a new puzzle, you need to close and open the program again.

Changelog
v0.21(march 19, 2009)
1) User can now input puzzle
2) Exit button

Interface and brute force search

I spent some time on my Sudoku program and now I have integrated graphical user interface to it. The menu bar still doesn't work though, so don't try to exit using file -> exit. Anyway, v0.2 is available now in the sidebar.

Brute force search has also been implemented. This means that any puzzle should be solvable now(provided of course, that the puzzle is valid). Of course, given the nature of brute force search, it may take a gadzillion years for the program to find the solution if the puzzle is too hard. So please be gentle on my program :p

I haven't been able to thoroughly test this program yet. So I can't say it's as bug-free as v0.1.

First version is up

The first version of my Sudoku program is up and the link to download it is shown at the right of the page.

I first tried hosting it at sourceforge but it requires approval and a detailed description of my project. I have no idea how long the approval will take and so I hosted the program at mediafire instead as it's the first result from google when I type file hosting. If you know of a better place, let me know.

Sudoku Strategy - Singles(Hidden Single)

See cross hatching and squeezing.

Sudoku strategy - Singles(Last Digit)

Last digit happens when there is only one instance of a number missing from the whole grid and the missing instance has only one cell to go to.

For instance, consider the figure above. The number 7 has been solved in 8 boxes and is only missing one instance. The missing 7 can only go into R8C8.

Sudoku strategy - Singles(Full House)

A house is a group of 9 cells which must contain all the numbers from 1 to 9, i.e a house can be a row, a column or a box.

A full house is such a group of 9 cells with one unsolved cell.


The figure above shows an example of a full house with a box with one unsolved cell. The missing number 9 is the solution to the unsolved cell.

For the program, the sole candidate method is sufficient to find any full house and so there is no need to implement extra methods for this strategy.

Sudoku strategy - Singles(Sole Candidate)

Strictly speaking, sole candidate is not a strategy at all. First it requires you to maintain the possible values for each unsolved cell. If any cell has only 1 possible value, then that cell must
certainly take on the only possibly value.


For instance, for the box shown above, the small gray numbers in the unsolved cells represent the possible values. The number 6 is the sole candidate for the bottom left cell and thus is the solution to the unsolved cell.

Sole candidate is the most basic program method to solving Sudoku. My Sudoku program first scans the entire grid for sole candidates and if any is found, the cell is resolved and the possible values for each cell is updated for the affected row, column and box. Then the program looks again for sole candidate. Of course, it may reach a point where no more sole candidates can be found even though the puzzle is still unsolved. That's when more advanced methods are needed.

Sudoku strategy - Cross Hatching

Cross-hatching is similar to squeezing but here, we will be using two or more solved numbers that are connected in different directions from our target box.

The graphic above shows an example of cross-hatching. The solved 7s from box G eliminate the possibility of 7 from column 2 while the solved 7 from block C eliminate the possibility of 7 from row 1. The only cell left in block A is R3C1 and hence must be 7.

Could you determine where the 7s are in blocks E and I using cross-hatching?

The single spotted using squeezing or cross hatching is also known as hidden single.

Sudoku strategy - Squeezing

Squeezing utilizes one or two solved numbers in connected boxes to locate the same number in the third connected box. Consider the band (3 horizontally connected boxes) shown above. The number 4 is already solved in the last two boxes. The green color highlights all the squares on which a 4 is not permissible. In the third connected block, only one unsolved square is not highlighted by green, and since that's the only square left, it must be a 4.

The graphic above shows an example of when one solved number is enough to obtain a squeezing solution.

Sudoku

Note: Sudoku terminologies are bolded.

The rule of Sudoku is simple: Fill in the empty cells in the grid so that each row, column and box contains the number 1 to 9.

Notation
We will refer to a particular cell in the grid using the notation R#C#. For instance, the cell in the third row and fourth column is denoted as R3C4.

The 9 boxes of 3x3 will be labeled with A-I in the following manner:

The most systematic way to solve Sudoku is to meticulously maintain a list of possible candidates for each cell. This process is usually tedious, error-prone and detracts the fun out of Sudoku. For a start, we will look at some of the simple strategies. *The list here will be updated from time to time:

1) Squeezing.
2) Cross-hatching

Matlab vs Java

The first thing I tried yesterday was to translate the Matlab function for verifying a Sudoku puzzle into Java. That was the first time I realized how easy it was to code in Matlab compared to Java.

As a comparison, let's look the codes. For matlab, to check for duplicate values , here's the code:
k = find(A(i,:) == j);

It's just a single line and I only need to check the length of k next to determine whether duplicate values exist or. For Java, I have write a loop to get the desired result.

int[] count = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int value = 0;
// check for duplicate values
for(int i = 0; i <>
value = cells[i];
count[value] += 1;
if(value != 0 && count[value] > 1) {
return true;
}

Conclusion: Matlab beats Java with a resounding victory. Of course, that only applies when it comes to ease of programming.

NetBeans vs Eclipse

So the GUI tutorial that I found uses NetBeans IDE instead of Eclipse. I installed Eclipse a few days earlier because well, it's popular. Although with hindsight, I probably should have done more research especially after running through Eclipse's not-up-to-date java tutorial. It's probably not a big deal if my wireless internet connection is not downloading stuff at 10 kilobytes/second, yeap that's about twice as fast as 56kbps dial-up connection. Unfortunately I'm forced to use the frequently disconnected wireless before my router arrives.

Anyway, after some Googling and exploring around, I found out that NetBeans is actually the better IDE for implementing GUI in java. I proceeded to download and installed it(which wasn't done until this morning because of the slow download speed). And yes, NetBeans GUI builder is so good I wish I have such a good builder when I was coding my MEng project. The drag and drop visual builder would have made my life easier and made my project looks prettier. I wasted so much time painstakingly aligning the various elements. Netbeans solved all that by having a grid that you can snap your elements to at a distance(from other elements) that makes it looks appealing.


Update: Here's how my Sudoku solver interface looks like in the Netbeans IDE(there are a few more buttons at the bottom but it's not shown). To the right is the palette containing all the elements that you can drag and drop to create your GUI. You can also switch back to the source code to see how each element is coded.

Bottomline: If you are a beginner at Java or are new to GUI, then NetBeans is definitely the preferred IDE.