Want to use your Java skills to build a real-world project? How about a Sudoku game?

We just posted a full course from Ryan Kay on the freeCodeCamp.org YouTube channel that will teach you how to build a Sudoku game desktop application using Java.

Ryan uses a Sudoku Application as a teaching tool in order to explain how to design, architect, and build Java Desktop Applications. The course is highly practical in nature, and the idea is that you follow along in your own IDE.

Here are some of the topics covered in this course:

- Problem Domain Classes
- Constants w/ Enums & static final variables
- Application Container/Entry Point
- User Interface w/ Java Interfaces
- Control Logic
- Computation Logic
- Build Logic
- Persistence/Data Storage
- Acknowledgements & further resources

I’m a teacher and developer with freeCodeCamp.org. I run the freeCodeCamp.org YouTube channel.

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons – all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.

Donations to freeCodeCamp go toward our education initiatives and help pay for servers, services, and staff.

## Introduction

Hello, and welcome to my article. Today, I want to talk about how easy it is to create a Sudoku game with Visual Basic.

## What Is Sudoku?

Sudoku is a logic-based, combinatorial number-placement puzzle. The objective is to fill a 9X9 grid with digits so that each column, each row, and each of the nine 3X3 sub-grids that compose the grid contain all of the digits from 1 to 9. The puzzle setter provides a partially completed grid, which for a well-posed puzzle has a unique solution. For more information about Sudoku, read this article.

## Our Project

I have taken the very long and tedious route to create this project. This means that you have a lot of work to do today, especially on the Design part. Let’s jump straight in, shall we?

### Design

Start a new Visual Basic Windows Forms project and design your form to look like Figure 1:

**Figure 1:** Design

By the looks of the design, you can see that we will be busy today!

Add the following objects that you will use during the course of your program:

I created a Boolean (on/off) flag that will determine whether or not a solution has been found. I then created four arrays identifying each button’s text as well as the grid blocks, as you will see later. The last object holds a random number value that we will use to populate the buttons’ text properties on the grid.

Add the **Form_Load** event:

This is quite straightforward and may seem to be a bit of an overkill. There is method in my madness, however. I didn’t create an array of buttons and calculate each button’s location independently. So, the button array we created holds each button’s (that we created) properties.

Add the next code under the ‘**New Game’** button:

A sub procedure named CreateGrid gets called and the ‘Show Solution’ button becomes enabled. Let’s create the CreatGrid sub procedure now:

Add the **GetValidValues** function that checks to see if the current number is a mathematically valid number to be put in the grid:

Add the remaining events:

## Conclusion

Sudoku is quite a fun game, but being as curious as I am, I just had to see if I could figure out the logic of it. I sincerely hope that you have enjoyed this article. Until next time, cheers!

An online tool to generate and download printable sudoku puzzles with solutions in different difficulty levels and grid sizes. Optionally you can set font style for the numbers and the output like 1 per page, 2 per page or 4 per page for printing.

Recommended: Check out this **Advance Sudoku Generator** to create commercial use printable puzzles.

### Dimensions / Grid

### Difficulty level

### Font Style

Show cell shades

### Download options

Generate New Puzzle

Show controls to play online

Select a number first and then click on the puzzle cell to apply it.

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9

Click ‘generate sudoku’ button to make puzzles

## About Sudoku

Sharpen your mind and improve concentration skills with Sudoku. In order to play this interactive game, you need to fill in the empty spaces of a 9×9 or 4×4 grid. If you select 9×9 grid, you will fill empty spaces in such a manner that every row and column as well as every 3×3 box contains the numbers from 1 to 9.

Similarly, for 4×4 grid, you will need to fill empty spaces in such a way that every row, column and every 2×2 box contains the numbers from 1 to 4.

Depending on the difficulty level you choose, it can be a fun and interactive game for everyone.

## Sudoku 9×9 and 4×4

There are two levels that you can choose to play. 9×9 is a grid with difficult puzzles and 4×4 is an easy game perfect for beginners.

### Printable Sudoku Puzzle

You can play this Sudoku with pencil and paper. Simply download, print and start playing. You can generate as many as Sudoku puzzles as you want with this Sudoku Generator. To see the solution online, click on the *“Show solution”* button.

For the printed version, the solution will be included at the end of the generated PDF file.

### Online Sudoku

There is also an option of playing Sudoku online. To start playing online:

- Open the control panel by checking the
*“Show controls to play online”*option. You will see the display with numbers from 1 through 9. - You can choose from the numbers to fill in the blanks.
- Eraser tool can be used to clear an entry you made.
- Once you are finished with your entries, you can click on the
*“Check Solution”*button to see the results if you have entered the correct entries or not.

**Sudoku Rule № 1: Use Numbers 1-9**

Sudoku is played on a grid of 9 x 9 spaces. Within the rows and columns are 9 “squares” (made up of 3 x 3 spaces). Each row, column and square (9 spaces each) needs to be filled out with the numbers 1-9, without repeating any numbers within the row, column or square. Does it sound complicated? As you can see from the image below of an actual Sudoku grid, each Sudoku grid comes with a few spaces already filled in; the more spaces filled in, the easier the game – the more difficult Sudoku puzzles have very few spaces that are already filled in.

**Sudoku Rule № 2: Don’t Repeat Any Numbers**

As you can see, in the upper left square (circled in blue), this square already has 7 out of the 9 spaces filled in. The only numbers missing from the square are 5 and 6. By seeing which numbers are missing from each square, row, or column, we can use process of elimination and deductive reasoning to decide which numbers need to go in each blank space.

For example, in the upper left square, we know we need to add a 5 and a 6 to be able to complete the square, but based on the neighboring rows and squares we cannot clearly deduce which number to add in which space. This means that we should ignore the upper left square for now, and try to fill in spaces in some other areas of the grid instead.

**Sudoku Rule № 3: Don’t Guess**

Sudoku is a game of logic and reasoning, so you shouldn’t have to guess. If you don’t know what number to put in a certain space, keep scanning the other areas of the grid until you seen an opportunity to place a number. But don’t try to “force” anything – Sudoku rewards patience, insights, and recognition of patterns, not blind luck or guessing.

**Sudoku Rule № 4: Use Process of Elimination**

What do we mean by using “process of elimination” to play Sudoku? Here is an example. In this Sudoku grid (shown below), the far left-hand vertical column (circled in Blue) is missing only a few numbers: 1, 5 and 6.

One way to figure out which numbers can go in each space is to use “process of elimination” by checking to see which other numbers are already included within each square – since there can be no duplication of numbers 1-9 within each square (or row or column).

In this case, we can quickly notice that there are already number 1s in the top left and center left squares of the grid (with number 1s circled in red). This means that there is only one space remaining in the far left column where a 1 could possibly go – circled in green. This is how the process of elimination works in Sudoku – you find out which spaces are available, which numbers are missing – and then deduce, based on the position of those numbers within the grid, which numbers fit into each space.

Sudoku rules are relatively uncomplicated – but the game is infinitely varied, with millions of possible number combinations and a wide range of levels of difficulty. But it’s all based on the simple principles of using numbers 1-9, filling in the blank spaces based on deductive reasoning, and never repeating any numbers within each square, row or column.