Friday, October 14, 2022

Excel Nonogram Maker and Player

Do you like logic games? In this post we see how to create nonograms in Excel using VBA macros. The macros described here and the file attached allow to create custom nonograms or generate random puzzles, and then save them to another (hidden) sheet. A different tab allows to play existing (saved) nonograms or randomly generated puzzles. The nonogram player checks the progress and allows to confirm the result or display the solution. We can also change the size of the nonogram puzzles to reach different levels of difficulty. Continue reading this post to learn how the program works and download the file to play nonograms in Excel.


Nonogram Maker

The first tab allows to create a nonogram in Excel by selecting the target cells within the board range. We achieve that with Excel VBA through an event procedure (Worksheet_ SelectionChange) that triggers when the selection changes. If the selected cell falls within the nonogram board, it changes the interior color (black if it was empty or white, and white if black).

   
  Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      If Target.Cells.Count < 2 Then
          If Target.Row > 5 And Target.Column > 3 And Target.Borders.LineStyle = xlContinuous Then
              If Target.Interior.Color = vbWhite Then
                  Target.Interior.Color = vbBlack
              Else
                  Target.Interior.Color = vbWhite
              End If
              Call ReadNono
          End If
      End If
  End Sub
 

 

Whenever the cell status changes, the event procedure calls another macro (ReadNono) to check the move and update the numbers. “ReadNono” loops through each row and column in the nonogram and updates the numbers accordingly.


When complete, we can save the nonogram to another sheet that remains hidden and stores all saved nonograms along with the number of rows and columns, and date created. We can also change the dimensions of the nonogram to a maximum of nine rows and nine columns.


Nonogram Player

The second tab allows to play and resolve existing nonograms in Excel. That could be one of the nonograms previously created and saved, or a completely random nonogram puzzle when triggered. A number of buttons in the Excel worksheet facilitate the different options in the nonogram player.

  • Load a saved nonogram puzzle
  • Create a random nonogram puzzle
  • Change the size of the nonogram board
  • Submit the nonogram to check if is correct
  • Solve the nonogram entirely
  • Clear the nonogram fields

 

 

Excel VBA event procedures are used to capture the moves while resolving the nonogram. We can either fill a cell in a nonogram (as explained earlier when creating a nonogram), or add an “X” to the cell by using the mouse right-click. When submitting the solution, another macro checks the result against the original solution. In case of an incorrect solution, the macro will highlight in red the cells that were wrongly selected. Similarly, when choosing to solve the nonogram, it will display the original solution of the puzzle.

Loading a nonogram is only possible if nonogram puzzles have been previously created and saved. In such case, we can see the list of saved nonograms with the date and time created, and the size of the nonogram in a userform.

 


A hidden sheet stores the list of saved nonograms. For each of them, there is information about the date and time created, number or columns and rows, and a string of values representing the status of each cell in the nonogram. Those values are either 1 or x to represent a cell filled or empty respectively.

It is important to emphasize that this is not a nonogram solver. The solution of the nonograms is stored when created and saved, regardless if the nonogram is custom made or randomly generated. Similarly, a random generated nonogram puzzle in the nonogram player will be created in a separate hidden sheet where the solution is temporarily stored. That solution remains there until a new puzzle is added or the fields are cleared. Thus, we can continue playing a nonogram even if we started earlier or another day and the file was closed and saved.


Download Excel Nonogram Maker and Player

 

No comments:

Post a Comment

Popular Posts