An ASCII puzzle for an escape room challenge
countBits
The countBits
function (lines 21-29) returns the number of 1s in a binary representation of the number n
, which it figures out through a series of binary operations. After defining iCount
, the function sets up a while
loop with the condition n
. In C, a while
loop runs while its condition is not zero. Because the provided condition here is just a number, it will run until the number becomes zero.
In the binary AND (&
) of the number n
with 1, if the low-order bit (the least significant bit, or bit 1 in this case) is set, this operation evaluates to 1
; otherwise, it evaluates to
. Therefore, iCount
increments by either 1 or 0 depending on whether the bit is set. The right shift operator ( >>=
) shifts iCount
, discarding the low-order bit and moving everything over one place. The loop repeats, checking the new low-order bit again. Eventually, when the entire value has been right shifted, only 0s are left in n
. At that point, n
= 0, the while
loop exits, and return iCount
returns the number of bits that are set. You'll see how this is used in the next function.
rotateOutputs
The rotateOutputs
function (lines 31-67) picks a random number until it finds one with the appropriate number of bits; then, it turns on the associate output pins (the lower row of electrical connections in the puzzle). These connections are used in the current round of the game, indicated by LEDs.
The three variables are the number of bits to be set (iNeededBits
), the number of bits in the generated random number (iCurrentBits
), and the current random number (iRandom
).
The number of bits needed (iNeededBits
) is set by calling countBits
with the current character the players seek. The while
(lines 38-41) loops until iNeededBits
is not equal to countBits(iRandom)
, which was initially set to zero, but is set to random(0,255)
within the loop. In this way, random numbers are chosen until one is found in which the number of set bits matches the goal; then the loop exits.
Line 43 prints the random number to the serial monitor for debugging, and lines 44-66 update all of the electrical connections so that they are all either HIGH
or LOW
, depending on the value of iRandom
set above. The binary ANDs (&
) with 1
(line 44), 2
(line 47), …, 128
(line 65), checking only one bit in the number at a time. For each bit, digitalWrite
either turns the appropriate pin on or off.
ledYES and ledNO
The ledYES
and ledNO
utility functions (lines 69-80) just display YES or NO on the display. One or the other is called when the player checks to see whether the current wiring is correct. In each case, the showDigit
function is called with position (first argument) and character (second argument) hard coded.
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.