Code Door
Before this: Bits
This project uses Bits for a door with four switches beside it. Set the switches to the secret code and the door opens. A light beside the switches glows brighter for each switch that is set right.
Contents
switch1 Enabled -> chip s0
switch2 Enabled -> chip s1
switch3 Enabled -> chip s2
switch4 Enabled -> chip s3
chip solid -> changer "Collision Enabled"
chip solid -> changer Visibility
chip glow -> light Brightness
switch5 Enabled -> light Enabled
Code
const CODE = 0b1011
@left in s0: bool
@left in s1: bool
@left in s2: bool
@left in s3: bool
let code = s0 | (s1 << 1) | (s2 << 2) | (s3 << 3)
let right = 4 - BitCount(code ^ CODE)
@right out solid = code != CODE
@right out glow: float = right
const CODE = 0b1011is the secret code, written in binary so each digit is one switch:s3on,s2off,s1on,s0on, reading left to right.codepacks the four switches into one number, as in Bits.solidis on while the code is wrong. Wired to the door, it keeps the door there until the code matches.code ^ CODEis XOR: a bit of it is on wherever a switch differs from the code.BitCountcounts those wrong switches, so4 - BitCount(...)is the number of right ones.glow: floatmakes the output a decimal number, the kind a light’s Brightness takes.rightis a whole number, and the light reads it as a decimal.
All switches start off. How many are right before you touch anything? Which switches do you flip to open the door?
Run
- Compile the chip and paste it in-game. Build a door from a few bricks and give one of them a Brick Property Changer. Place four Switches in a row beside it, and a Point Light.
- Wire each Switch’s Enabled to
s0,s1,s2ands3. Puts3on the left, so the row of switches reads like the binary number. - Wire
solidto the Brick Property Changer’s Collision Enabled and Visibility inputs. - Wire
glowto the Point Light’s Brightness, and give the light’s Enabled a Switch of its own, flipped on. - With every switch off, one switch is right:
s2, which the code wants off. Flips0,s1ands3on. The light grows brighter at each one, and on the last the door vanishes.
Under the Hood
Open the chip. The packing is the same three Shift Left and three OR
(Bitwise) gates as before. After it come an XOR (Bitwise), a Bit
Count, a Subtract for 4 - ..., and a Not Equal for solid.
There is no gate for CODE. A const is written into the gates that use
it: the number 11 is typed straight into an input of the XOR and of the Not
Equal. Anyone who opens the chip can read the code there.
The light gives the code away too. Flip any switch. If the light gets brighter, leave it, and if it gets dimmer, flip it back. Four tries find the code.
Modify
-
Choose your own code: change
CODEto another four-bit number, from0b0000to0b1111. Add a sign that shows the hint as text,@right out hint = "${right} of 4 right", wired to a Text Decal’s Text. -
Make the hint something you ask for. Add
@left in peek: boolfrom a Button’s Held, and make the light dark unless it is held:@right out glow: float = if peek then right else 0. -
Make the door wait for a button. Here it opens only when someone presses Try, and it stays as it is until the next press, whatever the switches do in between:
const CODE = 0b1011 @left in s0: bool @left in s1: bool @left in s2: bool @left in s3: bool @left in attempt: exec let code = s0 | (s1 << 1) | (s2 << 2) | (s3 << 3) var shut: bool = true @right out solid = shut.Value on attempt { shut = code != CODE }Wire a Button’s Held to
attempt. Now no light tells you which switches are right, and flipping a switch after the door opens does not close it.
Make
Make a door with eight switches and two codes: a guest code and an owner code, where either one opens it. Then add a light that shows which of the two was used.
switch1 Enabled -> chip s0
switch2 Enabled -> chip s1
switch3 Enabled -> chip s2
switch4 Enabled -> chip s3
switch5 Enabled -> chip s4
switch6 Enabled -> chip s5
switch7 Enabled -> chip s6
switch8 Enabled -> chip s7
chip solid -> changer "Collision Enabled"
chip solid -> changer Visibility
chip owner -> light Enabled
Good to Know
- A door needs both Collision Enabled (players cannot walk through) and
Visibility (players can see it). Wire
solidto both. - A Switch’s Enabled stays on until it is flipped back, so in the first version the door closes again as soon as any switch moves.
- An unwired switch pin reads as off. With
s3unwired, the code0b1011can never match.
Next: pick a lesson from the tree on the Learn page.