SUDOKU PUZZLE BUILDER Using Java
by parthshah01234 in Workshop > Science
1261 Views, 4 Favorites, 0 Comments
SUDOKU PUZZLE BUILDER Using Java
Most of us are familiar with Sudoku from our childhood, it is a puzzle based on logic and combinatorial number placement. It's an extremely fun game to play and making it is fun too.
In order to make the game, one must be familiar with the rules as well. The objective is to fill a 9×9 grid with digits so that each column, each row, and each of the nine 3×3 subgrids that compose the grid contain all of the digits from 1 to 9. The same number cannot be repeated in the same line, horizontally or vertically.
A Sudoku puzzule looks similar to the above image.
Supplies
The code I wrote is in JAVA so in order to run a JAVA code in Windows 10 you require
- Install JAVA from oracle website
- Set up environment variables
- Write JAVA code and save with .java extension
- Execute JAVA code using cmd
HOW a SUDOKU PUZZLE CREATED
The first step is to create the puzzle's solution. Each row, column, and 3x3 grid must contain the numbers one through nine without duplicates. Fill in the grid completely obeying the rules and create a puzzle solution.
The next step is to begin removing numbers from the completed Sudoku puzzle solution.
The next step is to remove mirrored pairs from the puzzle. Each time a mirror pair is removed from the puzzle solution, the puzzle must be tested to see if it has a single solution. If it does not have a single solution, it is an invalid puzzle.
The process is known as backtracking
RULES OF SOLVING SUDOKU
In a typical sudoku, you'll have a square grid of 9 large squares. Inside each of those larger squares will be 9 smaller squares. When faced with a puzzle, some of those smaller squares will be filled in with numbers from 1 to 9. More difficult puzzles will have fewer squares filled in. The larger squares are often outlined with a darker line, while the smaller squares have a thinner line.
One basic rule of the game is every column and row must have all of the numbers from 1 to 9. That means that within a row or column, a number cannot repeat.
Similarly, in each of the 9 large squares, every number from 1 to 9 must appear. Once again, that means that each number can only appear once, as there's only 9 smaller squares in each larger square.
So, if a large square already has the number “2” in it, you know it can’t include another number “2” anywhere in the square.
JAVA CODE TO GENERATE a SUDOKU PUZZLE
Copy the code in a .java file and execute the JAVA code. The output will be a sudoku puzzle in which the 0 represent the empty boxes.
If you want to increase the difficulty of the game then increase the value of the variable K in the main function.
The variable K represents number of empty blocks there should be in the sudoku
Downloads
HOW TO SOLVE a SUDOKU PUZZLE
There are two ways to approach the Sudoku Solving process. You can use logic retaining in your memory the possible candidates for each cell, row, column, and region or you can write the candidates down.
I prefer to use the process of elimination to solve Sudoku. This leads me to my first of several Sudoku tips and that is pencil in all the possible candidates for each cell, row, column, and region.
Look for the easy play first: When you first start to play a Sudoku puzzle, look for where you have the easiest opportunities to add a number. Usually this is where there is a crowded square or a row that is almost full of numbers. Sometimes, especially on the Easy-rated Sudoku puzzles, you can quickly use process of elimination to figure out where to place a number. For example, if there is a square that already has numbers 1-7, you know that you only need to figure out where to put numbers 8 and 9. Look at the rows that feed into that row or square – sometimes you will be able to eliminate one number or the other, and can quickly fill in the gaps.
Look for which numbers are missing: Sudoku is about placing numbers where they don’t already exist – it’s a logical process of elimination. If a number already exists in a row or square, then that number cannot be placed again. Your challenge is to keep thinking and looking and spotting opportunities to add numbers where they haven’t already been placed. For example, if the top row of a Sudoku puzzle already has the numbers 1, 7, 8, 5, 9 and 2, this means that the row still needs numbers 3, 4, and 6. Look in the nearby rows (within the same squares) to see if you can rule out any of those three missing numbers.
Don’t guess: Sudoku does not require guesswork. If you aren’t sure if a number belongs in a certain spot, you’re better off not guessing.
Keep moving: Sudoku rewards the “roving eye” – if you feel stuck, don’t concentrate too hard on one part of the puzzle grid. Instead, let your eye and your mind wander to a different place on the grid where you haven’t placed any numbers yet, and see which new possibilities become apparent to you.
Constantly re-evaluate: Every time you place a new number on the Sudoku grid, you should ask yourself, “What changed? What do I know now, as a result of having placed that number? For example, if you successfully place a number 5 in a horizontal row, how does that 5 affect what’s going on in the neighboring squares? Every single time you place a number, it gives you an opportunity to potentially place more numbers in nearby rows and squares (depending on which other numbers in those places are already known). This is one of the most satisfying aspects of playing Sudoku – every step in solving the puzzle leads you closer to the conclusion.