Puzzle X is a fun logic game available on the web. In this post we see how to create a Puzzle X solver with Excel VBA macros. The macro converts the Puzzle X pattern into 1s and 0s that are added along the puzzle in the worksheet. It then loops through rows (or columns) using worksheet functions to search for clues to solve each X in the puzzle. It’s a good example of logic programming. Play Puzzle X online HERE.
Puzzle X
Puzzle X is a logic
game where players need to fill cells in a grid with an X so that it is unique
within its row and column. The grid can be of different sizes, the larger the
grid the more Xs to fill, and therefore the more challenging it is. The goal of
the game is to fit all Xs in the shortest time possible. This makes Puzzle X a
great exercise to improve cognitive abilities and reduce stress.
The game was created
by Excel Macro Mania in 2023. As many of its games and products, Puzzle X was
first created in Excel. The puzzle
allowed for almost infinite number of rows and columns but only grids up to 30
rows and columns were used. The layout of a simple puzzle in Excel looks as
follows.
The web version of
Puzzle X was published in 2024 and looks as shown below. Players can choose
from six grids of increasing difficulty starting with ten rows and columns
(i.e., 10X) up to 20X. It has a timer to track the time it takes to complete.
When completed, players can save their nickname and see other users’ scores in
a table that pops up underneath the puzzle grid.
Layout and pattern
The macro converts the
Puzzle X pattern into 1s and 0s that are added along the puzzle in the
worksheet. Well, it’s not necessary to add the 0s though, it is enough to count
1s for rows or columns to get the number of empty cells. The following macro
loops through the puzzle and adds number 1 for black cells.
For Each cell In rng 'rng depends on puzzle size (10X, 12X, ...)
If cell.Interior.Color = vbBlack Then cell.Value = 1
Next cell
Numbers are not
visible unless changing the font color. They could also be added to another
worksheet or to an array, but having the numbers in the worksheet allows to
leverage worksheet functions that help identify clues to solve each X in the
puzzle (see next).
Solving Loops
We need to loop
through rows (or columns) and find the gaps where a unique X can be placed in
order to solve the entire puzzle. See below the first loop in the solver macro.
As we will see later, that will all be put inside another loop to repeat the
process (see later Solving Whole Puzzle).
'solving steps and 2nd loop here (see next)
We use a variable
inside the loop to track the number of Xs that have been solved while looping.
This is used later to select which row (or column) needs to be targeted next.
Additionally, a conditional statement using the CountIf worksheet function ensures
the solver continues only if the row (or column) is empty (no X yet solved). Another loop checks next whether the row has
the number of black cells for the corresponding number of blanks using the
count of cells with content (number 1 previously added to each black cell) and
the variable solved. The first time it loops, it looks for a row (or column)
with only 1 blank (which is equivalent to a row with 9 black cells for a 10X
puzzle).
If WorksheetFunction.CountIf(rngRow, "X") = 0 Then
If WorksheetFunction.CountA(rngRow) = n - 1 - solved Then 'where n is the number of Xs to solve
End If
End If
Finally, when all
those conditions are met, a second loop moves through each blank cell in that
row (or column) and checks whether an X has been placed along the corresponding
column (or row) for each cell. If there’s no other X, the macro solves the X in
that cell and exits that loop.
Cells(cell.Row, cell.Column).Value = "X"
Exit For
End If
Next cell
Solving Whole Puzzle
The whole process
needs to repeat as many times as needed to solve the whole puzzle. The first
loop above moves through all rows (or columns) just once. Depending on the
design/configuration of the puzzle, that will solve one or more Xs, but most
likely not all of them. This loop uses the variable “solved” to check whether
the whole puzzle has been solved.
Loop
This solver can be
leveraged to solve puzzles of any size. There are hundreds of puzzles with
solutions available in the books below. Additionally, you can play Puzzle X
online here. See below the solver Excel file for download, which comes with a
few examples and allows to recreate there any puzzle and solve it.
Download Excel Puzzle X
Solver





No comments:
Post a Comment