Skip to main content

Excel/VBA Example - Sudoku Teaching Assistant

SUDOKU TEACHING ASSISTANT


Learn Sudoku


Learn how to solve Sudoku puzzles with this Sudoku Teaching Assistant Microsoft Excel workbook.  It is designed to help you solve easy to moderately complex puzzles while teaching you to how to solve them.


To start with, the user may enter their own puzzle, or use one of the built-in puzzles.  The flowchart present a step by step sequence of patterns that one can look for - the user is encouraged to learn the patterns and solve on their own; at the same time the computer can be used to help find patterns that the user may have missed, and optionally solve them.  Tedious work such as determining the candidates (possibilities) for a range of cells in the Sudoku puzzle can be taken care of by the computer.  More advanced users can use the program to automate some of the easier steps that they are already good at, and focus on learning the more difficult patterns. 


Sudoku Teaching Assistant is not intended as a source of puzzles - however, a small number of built-in puzzles are included.  It is intended more as a learning tool and helper program. 


Make sure to enable macros in the workbook on start-up, and to read the 'Getting Started' section of Help.  Start by using it to solve easy puzzles and work your way up to the hard ones.  After you download the Excel workbook and open it, you may see a security warning. Make sure to remove the "protected view" by clicking on "Enable Editing". You may also see another security warning that says "Some active content has been disabled". You need to click on "Enable content" and save the workbook. Note: these two actions should be done only if you trust the source of the workbook - namely me!


It can also be used to enhance your Excel skills via the addition of Visual Basic for Applications (VBA) code to your workbooks to create forms/dialog boxes, buttons etc. and for performing other tasks.  It does not teach VBA, but serves more as a programming example.  The Visual Basic source code can be accessed via the 'Developer' Tab in Excel, and is about 4000 lines.

If you are an Excel Visual Basic developer, then the entire code is also visible to you.


DOWNLOAD THE SUDOKU TEACHING ASSISTANT EXCEL WORKBOOK


HOW TO USE THE SUDOKU TEACHING ASSISTANT


Here is a suggested method to use the Sudoku Teaching Assistant to learn how to solve Sudoku.  Use this to teach yourself Sudoku, or help teach Sudoku to your kids or students.  Once you learn Sudoku, you have a good alternative way to spend the time that you may be wasting otherwise, such as while waiting in a doctor's office.  There are many free Sudoku programs available for your mobile device that can keep you mentally challenged.


Method

  1. Start with an easy built-in Sudoku Puzzle such as puzzle #1.  Click on the  Built-in Puzzle  button and enter 1 to get this puzzle.  You can work  your way up to more advanced puzzles later. People who are already quite familiar with Sudoku puzzles can choose a more advanced puzzle.
  2. Learn what the  Show All Candidates  and  Hide All Candidates  buttons do.  Then learn how to reveal the candidates in a range of cells - such as one row, one column, or one box - by using the  Show Candidates  button.  For example, select one row with your mouse or keyboard, and click on the   Show Candidates  button.
  3. Pick a row that seems to have the most number of solved cells.  Reveal the candidates in this row using the  Show Candidates  button.   See if you can find pattern 1 (single candidate) or pattern 2 (naked single) in this row.   If you do not know what these patterns are, see Sudoku Pattern 1: Naked Singles and Sudoku Pattern 2: Hidden Singles below come back to this step.   If you find the solution to a cell on this row, click on that cell.  A number pad will pop up.   Click on the solution value and then click Solve.
  4. Repeat above step for all rows, columns and boxes till you have found all the Single Candidate and Naked Single patterns that you can find.
  5. Now use the computer to verify that you have not missed any.  Show all candidates on the puzzle - then click on the   1. Naked Singles  button and the   2. Hidden Singles  button.  If the computer finds any, the corresponding cells will be highlighted in the puzzle, and the associated Solve button will be enabled.  You may choose to have computer solve them by clicking on the Solve button, or or you may solve them yourself using the number pad.
  6. Once you have mastery over the two basic patterns above, learn the other patterns (click on the  Help/About/License  button and read the Patterns section of Help or read the SUDOKU PATTERNS section below).  While learning, try to solve it yourself first before getting assistance from the computer.

Entering Your own Puzzle

To enter your own Sudoku Puzzle, click on the  Clear Puzzle  button and use the Quick entry method described in the dialog box.  Alternately, you can enter values into the cells directly. After entering all values, click on the  Set As New Puzzle  button.

Using the Snapshot Feature

At any point if you want to "try something" but be able to later discard your changes, click on  Save Snapshot . You can restore to the last saved snapshot by clicking on  Restore Snapshot . You can go back multiple levels.

Using the Auto-Solve Feature

Once you have mastered some of the easy steps (example: steps 1 and 2), you may wish to have the computer automatically perform these steps for you. To do this, click on the   Auto-Solve  button. By default it will solve steps 1 and 2 but this may be changed by clicking on the   Set Auto-Solve Level  button. You can solve the harder steps manually and continue using Auto-Solve to handle the easier steps.

SUDOKU PATTERNS

Naked Singles (Single Candidate in a cell)

Anyone can learn how to play Sudoku!   Start with this first step. 

In this very basic pattern, one looks for cells in the Sudoku puzzle that can have only one candidate in it. For a given cell, eliminate as candidates all the numbers that are already known on the same row, column or box. If only one possibility is left, then we have a single candidate pattern. 

In the example below, the cell on row 9, column 7 (shown in green) can have only the value 3, because all the other numbers are 'used up' on the row, column, or box that this cell is in.  Thus '3' is the solution to this cell.


In the second example below, the cell on row 4, column 3 (shown in green) can have only the value 8, because all the other numbers are 'used up' on the row, column, or box that this cell is in.  Thus '8' is the solution to this cell.



Hidden Singles

In this basic pattern, one looks for a row, column or box where a candidate occurs in only one cell.  One first determines the candidates for all the cells in the row, column or box of interest.  Use paper and pencil to do this, or in Sudoku Teaching Assistant, select the row, column or box using the mouse or keyboard and click on the   Show Candidates  button.  

If a candidate occurs on only one cell in a row, column or box, then the solution to that cell must be that candidate since the row, column or box must contain every number from 1 to 9.

In the example below, the cell on row 5, column 5 (shown in green) can have only the value 5, because '5' occurs on row 5 only in this cell.  Thus '5' is the solution to this cell.

Sudoku-Help: Naked Single Pattern

In the example below, the cell on row 1, column 7 (shown in green) can have only the value 7, because '7' occurs on column 7 only in this cell.  Thus '7' is the solution to this cell.


In the example below, the cell on row 7, column 6 (shown in green) can have only the value 4, because '4' occurs in the 'bottom middle' box only in this cell.  Thus '4' is the solution to this cell.



Locked Candidates

The three cases below are easy to recognize once all the candidates are revealed.

A. Candidates in a row that are constrained to one box

To find this pattern, looks at all the candidates in a row.  If any of the candidates in the row are all contained in (i.e. constrained to) a single box, then within that box this candidate can only be present in that row.  This allows us to eliminate that candidates from other rows within that box.

Look at the highlighted rows in the two examples given below.  In each row, try to find a candidate that exists only in one box.

In the first example, on row 8, the candidate '1' exists only in the bottom middle box - hence it must be in the middle row within that box.  This allows us to eliminate '1' from the top row and bottom row within that box.  In the second example, on row 2, the candidate '5' exists only in the top middle box - hence it must be in the middle row within that box.  We can eliminate '5' from the top row within that box.



B. Candidates in a column that are constrained to one box

To find this pattern, looks at all the candidates in a column.  If any of the candidates in the column are all contained in (i.e. constrained to) a single box, then within that box this candidate can only be present in that column.  This allows us to eliminate that candidates from other columns within that box.

Look at the highlighted columns in the two examples given below.  In each column, try to find a candidate that exists only in one box. 

In the first example, on column 7, the candidate '9' exists only in the middle right box - hence it must be in the left column within that box.  This allows us to eliminate '9' from the middle and right columns within that box.  In the second example, on column 9, the candidate '4' exists only in the top right box - hence it must be in the right column within that box.  We can eliminate '4' from the left and middle columns within that box.   (Notice that the same is true for candidate '3' but there is no '3' elsewhere in that box that can be eliminated.)



C. Candidates in a box that are constrained to one row or column

To find this pattern, looks at all the candidates in a box.  If any of the candidates in the box are all contained in (i.e. constrained to) a single row or single column within that box, then within that row or that column this candidate can only be present inside that box.  This allows us to eliminate that candidates from cells outside the box within that row or column.

Look at the highlighted boxes in the two examples given below.  In each box, try to find a candidate that exists only in one row or one column within that box. 

In the first example, in the middle-middle box, the candidate '5' exists only in the middle row of that box (row 6 of the puzzle) - hence it must be in the three cells of row 6 that are within that box.  This allows us to eliminate '5' from outside the box in row 6.  In the second example, in the middle left box, the candidate '6' exists only in the left column (column 1 of the puzzle) - hence it must be in the three cells of column 1 that are within that box.  We can eliminate '6' from outside the box in column 1.  (Notice that the same is true for candidate '9' but there is no '9' elsewhere in column 1 that can be eliminated.)




Naked Pairs

The Naked Pair pattern is defined here as "two cells (in a row, column or box) having a total of only two candidates".  This is an easy to recognize pattern.  Naked pairs allow us to narrow down the list of candidates in cells elsewhere in that row, column or box.

If only two candidates occur in such a pair of cells, these two candidate numbers must go into these two cells - we cannot say which cell contains which, but we can say that these candidates cannot exist anywhere elsewhere on that row, column or box.


Examples:

In the first example below, row 9 contains the naked pair [5,9] (marked in red).  Since these two cells can have only the two candidates 5 and 9, if '5' is in one cell, then '9' is in the other, and vice versa.  These two candidates have to be in these two cells and cannot be elsewhere on that row.  Thus we can eliminate 5 and 9 elsewhere in that row.

In the second example below, column 8 contains the naked pair [5,8]. We can eliminate 5 and 8 as candidates elsewhere in that column.  (Note: Only the area of interest is shown in the puzzle.)



Note that a given pair of cells can form a naked pair on both a row and a box, or both a column and a box.

In the first example below, the pair [3,4] are in both row 3 and the top middle box.  We can eliminate 3 and 4 elsewhere in row 3 and in the top middle box.   In the second example, the pair [1,9] are in both column 8 and the middle right box.  We can eliminate 1 and 9 elsewhere in column 8 and in the middle right box.



Note that, technically speaking,  the following pattern [3,4] is also a naked pair (even though we would normally solve the single candidate first).



Hidden Pairs

Hidden Pairs have both similarities and differences with naked pairs.  They are harder to see because of the presence of other candidates in the two cells involved.  Unlike naked pairs, the "pair of candidates" exist only in two cells, plus there are other candidates besides the pair in these two cells.  Since these two candidates do not exist anywhere else in the row, column or box, the two cells involved must have these two candidates as the solution - hence we can eliminate the other candidates in these two cells.

The difference between the naked pair and hidden pair is illustrated in the two row examples below.   Look at the cells with candidates marked in red.   In the naked pair, the candidate pair [1,7] are the only candidates in the two cells but exist elsewhere on the row (we eliminate the candidates 1 and 7 elsewhere in the row).  In the hidden pair, the candidates in the pair [1,7] are only in two cells but these cells have other candidates (we eliminate the extra candidates 3, 4 and 7 in these two cells).

Naked Pair:


Hidden Pair:


Hidden Pairs can form in rows, columns or boxes.  Formation in a row has already been illustrated above.  Formation in a column and a box are illustrated below.

In the first example below, the column contains the hidden pair [5,6] in the two cells marked in red.  We can eliminate 1, 3, 4 and 9 from these two cells.   In the second example below, the box contains the hidden pair [4,6].  We can eliminate 3,5,8 and 9 from these two cells.




Enjoy playing Sudoku!



Comments

Popular posts from this blog

Lightweight C++ class library to store program settings (configuration) with Unicode (UTF-8) support

Drupal CMS - Creating sub-administrators that can view or edit a restricted set of users