Challenge: Ordered Switch Puzzle with Restrictions

in Tutorials

Hi there, it has been a while since the last entry in our Challenge series, but we’ve decided to bring it back. So how does it work? We put up a post, challenging you to figure out a way to do something in RPG Maker. We include how it should work, and then some restrictions, and let you go. Then roughly a week later, we come back, and show you how we did it.

The point is to teach people to think outside the box, to learn new things about eventing and manipulation of the engine.

So today, for our challenge, we’ve got an easy one. Remember our Ordered Switch Puzzle tutorial? Well you are going to do the same thing.

map

4 switches. The door opens when you flip them in the right order. But I can hear what you are saying… well we already know how to do that right?

Here is the challenge:

No switches. No self-switches. Only 2 variables allowed. No scripting/plugins.

And that is it. The whole challenge. Can you do it?

Extra bonus: Can you do it with just 1 variable?

Comments on this entry are closed.

  • Celianna

    Well, the gist of it (spoilers): http://i1068.photobucket.com/albums/u453/celiannauploads/event_switches_order_zpsa7h77cam.png

    If you want the events to have visual cues of a button being pressed, you’d need to move the event to show a different graphic, and reset them all in each else branch if it fails.

  • Elfert

    Easy, a riddle with 8 button with diferent answer. If you touch the right one set a variable = 1 and then open a door if that variable is = 1…

  • Stephen Skwarchuk

    Easy set 4 events that the player needs to unlock and make it if the player has unlocked them in a certin order, other wise reset to 0 making the player go back try and try to figure out the right order to access a room let’s say. A simple puzzle really and might use for my game, maybe

  • William Johnson

    4 switches need to be done in a certain order to open a door. Only two variables allowed.

    variable 001 = the order (this is easier to show than explain)
    variable 002 = is not entirely necessary… but you could use it for the Switch event’s graphical changes… which I will do for this.

    Prep Step:
    let’s assume the following:
    You have Switch 1, Switch 2, Switch 3, & Switch 4.

    Step 1: Create conditional branches that are similar to the following for Switch 1:
    If variable 001 > 0
    switch sound effect
    set variable 002 = 1
    wait(60)
    wrong buzzer sound effect
    reset switch sound effect
    set variable 001 = 0
    set variable 002 = 0
    Else
    switch sound effect
    set variable 001 = 1
    set variable 002 = 1

    Step 2: For Switch 2 add a conditional branch to the following effect:
    If variable 001 > 1
    switch sound effect
    set variable 002 = 2
    wait(60)
    wrong buzzer sound effect
    reset switch sound effect
    set variable 001 = 0
    set variable 002 = 0
    Else
    switch sound effect
    set variable 001 = 2
    set variable 002 = 2

    Step 3: Post the same conditional branch with the slightly modified condition & variable 001/002 settings into both Switch 3 and Switch 4.

    Step 4: On Switch 4 under the Else of the conditional branch add this instead…
    switch sound effect
    set variable 002 = 4
    wait(60)
    correct switch sound effect
    set variable 001 = 4
    door opening sound effect

    Step 5: Set the door event to have a second event page that would be revealed once variable 001 = 4

    Step 6: Set each Switch to have a second event page that is set to be revealed once variable 002 is set to their order (Switch 1 has variable 002 = 1, Switch 2 has variable 002 = 2, etc…) with a graphic of flipped switch

    Step 7: Set each Switch to have a first event page to be revealed once variable 002 is set to 0 or greater (which is all by default).

  • Shionkreth

    I got it with conditional branches: if variable 1 = 0-3(in the right order) increase variable 1 by +1, if else set variable 1 to 0.

    Could let the player know with a chime for the correct order and the buzzer for the wrong order, signalling a reset, but I wanted it to be represented graphically so I used a second variable for the flipped switches in the same way(2nd event pages with a flipped switch if variable 2 = 1-4), couldn’t find a way to do so without a second variable, switches or self switches.

    • Anja

      You can set the second pages to the variable value in question (1 or higher for first button, 2 or higher for second and so on). Like this, you only need a variable and nothing more.

  • Red Mage

    Extra Bonus-
    From left to right, the four switches will use the thousands, hundreds, tens and ones digit.

    e.g. if you switch on the far left switch to start, it sets your variable to 1000. If you choose the far right to start, it sets to 1.

    Now the tricky part. I’m going into this assuming that we’re starting at the far left switch, and the other three switches can be utilized just by changing the place of the important digit. (My variable name is DoorSwitch)

    If DoorSwitch == 0
    Set DoorSwitch = 1000
    end
    If DoorSwitch == 1
    Set DoorSwitch = 2001
    elsif DoorSwitch == 10
    Set DoorSwitch = 2010
    elsif DoorSwitch == 100
    Set DoorSwitch = 2100
    end

    What we’ve done here is we’ve established that if you’ve hit any other switch first, that if this is the second pressed switch, we’re inserting at 2 in the thousands’ place, as it’s the second pressed switch.

    Now here’s where things get fun.

    If DoorSwitch == 21
    Set DoorSwitch = 3021
    elsif DoorSwitch == 201
    Set DoorSwitch = 3201
    elsif DoorSwitch == 12
    Set DoorSwitch = 3012
    elsif DoorSwitch == 102
    Set DoorSwitch = 3102
    elsif DoorSwitch == 120
    Set DoorSwitch = 3120
    elsif DoorSwitch == 210
    Set DoorSwitch = 3210
    else
    DoorSwitch += 4000

    Just like before, we’re basically setting up the third switch’s place, assuming that two switches were flipped prior to it being touched. The Else at the end, if the other three switches were flipped in any other order, won’t much matter because you can just add 4000 to the mix and end up with 4321, 4231, 4213, 4123, or 4132 without needing to do all the coding for it.

    And then of course, you’d need one more little tidbit at the beginning, to check if you’ve actually messed with the switches at all or not..

    If DoorSwitch >= 1000
    Text: This switch has been flipped.
    Exit Event Processing
    end

    On the other switches it’ll be a little bit more intensive to verify the switch has been flipped.

    Switch 2:
    If DoorSwitch >= 3000
    DoorSwitch -= 3000
    If DoorSwitch >= 100
    Text: This Switch has been flipped.
    DoorSwitch +=3000
    Exit Event Processing
    End

    elsif DoorSwitch >= 2000
    DoorSwitch -= 2000
    If DoorSwitch >= 100
    Text: This Switch has been flipped.
    DoorSwitch +=2000
    Exit Event Processing
    end
    elsif DoorSwitch >= 1000
    DoorSwitch -= 1000
    If DoorSwitch >= 100
    Text: This Switch has been flipped.
    DoorSwitch += 1000
    Exit Event Processing
    End
    elsif DoorSwitch >= 100
    Text: This Switch has been flipped.
    Exit Event Processing

    End

    That long string of events basically checks to see if our far left switch has been pressed, and then if so, it stops the processing on the event, with the last bit being if that far left hasn’t been touched at all. Unfortunately this gets a little more intense the further to the right we go.

    Switch 3:
    If DoorSwitch >= 3000
    If DoorSwitch >= 200
    DoorSwitch -= 3200

    If DoorSwitch >= 10
    Text: This Switch has been flipped.
    DoorSwitch += 3200
    End Event Processing
    Else
    DoorSwitch +=3200

    End
    elsif DoorSwitch >= 100
    DoorSwitch -= 3100
    If DoorSwitch >= 10
    Text: This Switch has been flipped.
    DoorSwitch += 3100
    End Event Processing
    Else

    DoorSwitch += 3100

    End
    End
    elsif DoorSwitch >= 2000
    If DoorSwitch >= 300
    DoorSwitch -= 2300
    If DoorSwitch >= 10
    Text: This Switch has been flipped.
    Doorswitch += 2300
    End Event Processing
    Else
    DoorSwitch += 2300

    End
    elsIf Door Switch >= 100
    DoorSwitch -= 2100
    If DoorSwitch >= 10
    Text: This switch has been flipped.
    DoorSwitch += 2100
    End Event Processing
    Else
    DoorSwitch += 2100

    End
    End
    elsif Door Switch >= 1000
    If DoorSwitch >= 300
    DoorSwitch -= 1300
    If DoorSwitch >= 10
    Text: This switch has been flipped.
    End Event Processing
    else
    DoorSwitch += 1300

    End
    elsif DoorSwitch >= 200
    DoorSwitch -= 1200
    If DoorSwitch >= 10
    Text: This switch has been flipped.
    DoorSwitch += 1200
    End Event Processing
    Else
    DoorSwitch += 1200
    End

    End
    elsif Door Switch >= 300
    DoorSwitch -= 300
    If DoorSwitch >= 10
    Text: This switch has been flipped.
    DoorSwitch += 300
    End Event Processing
    Else

    DoorSwitch += 300
    End
    elsif Door Switch >= 200
    DoorSWitch -= 200
    If DoorSwitch >= 10
    Text: This switch has been flipped.
    DoorSwitch += 200
    End event Processing
    Else
    DoorSwitch += 200
    elsif Door Switch >= 100
    DoorSwitch -= 100
    If DoorSwitch >= 10
    Text: This switch has been flipped.
    DoorSwitch: += 100
    End Event Processing
    else
    DoorSwitch += 100
    elsif Door Switch >= 10
    Text: This switch has been flipped.
    End Event Processing

    End

    Whhhheeeeeeewwwwwww. And if you think that’s rough, there’s even more on the far right switch. I’m not going to write out the whole code, but just provide the guidelines for doing so. Now one thing that we can omit is the check for 3000 (was the first switch flipped third), the check for 300 (was the second switch flipped third), and the check for 30 (was the third switch flipped third) because /if/ there was a third switch flipped at all, that would make this last one the 4th switch, which will trigger one last event that we’ve got planned.

    You’ll want to use the above code, but add in another full check for 20/10 rather than just the 10 at the end to make sure that the 3rd switch wasn’t flipped second.

    ===

    Now there’s our switch-flipping events. Pretty crazy, right? (There are some ways to make it a little more organized, such as putting the initial 3000/2000/1000 checks as conditions on page requirements on the event, but that’s strictly organization.)

    Okay, and then at the very end. You’ve flipped four switches. Are you correct in knowing whether or not you did it right? Here’s how you find out. Basically, you chose the number. We’ll call it 3124.

    3124 means that-
    The second switch was flipped first (the 1 is in the hundreds’ place)
    The third switch was flipped second (the 2 is in the tens’ place)
    The first switch was flipped third (the 3 is in the thousands’ place)
    The fourth switch was flipped fourth (the 1 is in the ones’ place)

    So you’ll need a parallel event that basically just checks if DoorSwitch == 3124. If so, it plays a chime, and changes the event that is the door to something open, and then sets DoorSwitch to 5555 so that it meets the condition of every question on whether or not the switches have been flipped.

    And there you have it!

    (Just use switches.)

    • Red Mage

      I also realize I didn’t look at the previous example and went with it assuming you could enter four switches first, and then being told if they were right or wrong. If it immediately will tell you if it’s wrong then that makes things tremendously easier.

      I should’ve looked. Oh well.

  • Merutochan

    Achieving a two variables solution was fairly easy: one containing the counter of the switches being pressed, the other containing a counter that got reset to 0 if the order was messed up.
    Anyway, considering that I couldn’t use self switches to avoid the same switch events being pressed again and again I used the short (and really dirty) solution of erasing events (they respawn after two change map teleports).

    The alternative was addressing to every switch a different value for the variable containing the counter of the switches being pressed : using the powers of 2 it’s possible to identify any combination of the switches being pressed at a given moment; this could be used to constantly control if the switch we’re pressing hasn’t been pressed yet. This costs a lot of conditional branches since we have to make a switch event unpressable for 7 different values (all the combinations that include that specific switch event out of the 4, because it was already pressed), but it’s still doable.

    Using a single variable, I kept using the powers of 2, this time starting from the value 4 instead of 1. This is because I thought that I could add, in the same variable, the value 1 to address the arise of an error in the order:
    If the order was exactly right I’d get (4+8+16+32)= 60 as a result, but if the order was messed up I would get (4+8+16+32 +k) where k is the number of mistakes I’ve made (it’s either 2 or 3 or 4). If I had 60 as a value, the door would open, if I had more than 60, it would be a wrong combination.

    But this system messes up entirely my conditional branches on every switch event: I can no longer check for 7 forbidden values, because I have to check for every one of those the variant value, that is given by the “+1” error mechanism. This means that I have to check for … what is it? 21 values? 28? I’m not even sure, it’s either (7 * 3) or (7 * 4). And those are unique values for every single switch. That sounded insane, so I switched back to the inelegant erase event solution adapting it for a single variable:

    On every switch I check for the variable current value: then I add 4 if so long the order is correct, or I add 5 if the order is wrong, then I erase the event. When the variable is >=16 an event tells me if I got it right or wrong: If wrong I get teleported in another map, the variable is set back to 0 and I get teleported back in the same position.

    I’m pretty sure I was heading in the correct direction with the other method, but I probably kept doing wrong in the conditional branches: there probably are some workarounds to avoid using 28 IFs but all the ones that I thought involved using another variable, therefore nullifying any effort. If the author of the challenge would point me out what I’ve been doing wrong I’d be glad.

  • Mauricio Ribeiro

    Just downloaded the rpgmaker to start my first game, but I guess you can easily manage this with a recursion method and one variable boolean that starts false. If you get one false inside the recursion before finish it starts again and if it’s true the method calls itself. In this way you can save the progress of the player or restart from the first step.

  • Staffan Lindsgård

    A bit late for the party, but here it is anyway.

    I will use a five digit variable to keep track of everything. The positions of the digits are numbered 54321. The fifth position holds the number of rounds, the fourth the round when the first switch was thrown, the third is for the second switch, the second for the third and the first for the fourth.

    For example, if all switches are thrown in the order 3241, the variable will read 44213. In order, 40000 means that we’ve thrown four switches, 04000 means that the first switch was the fourth to be thrown, 00200 means that the second switch was the second to be thrown and so on. Halfway there, when only two switches have been thrown, the variable would read 20210. The zeroes mean that those switches have not yet been thrown.

    In this way, each order of switch throwing has a unique number to it and all we need to do is to check if the variable is set to that number. But how do we set the number? Here’s some pseudo code:

    HAS SWITCH BEEN THROWN?
    YES
    Exit.
    NO
    Add one to counter AND copy the new value of the counter to the position corresponding to this switch.
    HAS COUNTER REACHED 4?
    NO
    Exit.
    YES
    IS VARIABLE IDENTICAL TO SOLUTION?
    YES
    Victory! Open door.
    NO
    So sad… Reset and start over.

    On each event we need to add one to the counter. We do this by adding 10000 to the variable. Each time we throw a switch, the counter will go up.

    Next, we take that number and add it to whatever position the event has as its position. This is done by taking the variable, dividing it by 10000 and using ‘Math.floor()’ to get an integer. Next we take that integer, multiply it with whatever we need to place it into the right position and add it to the variable. The first switch is in the thousand digit position, so in this case we multiply it with 1000. The second would be multiplied with 100, the third with 10 and the fourth with 1 (or not multiplied at all).

    If my variable has an ID of four and I want to copy its value to the fourth position (switch one) I would use the following script:

    (Math.floor($gameVariables.value(0004)/10000))*1000

    I use “Control Variables”, select my variable, set the “Operation” to “Add” and the “Operand” to the script.

    Next, I use the same code I did before in a conditional branch to get the counter and see if it’s set to 4 (or larger). If it isn’t, I exit. If it is, I compare the whole variable to whatever solution I’ve set and either open the door or reset the puzzle.

    But hey, you might think, you skipped the part where we check if the switch is already thrown. Yes, I did. Normally I would probably simply use a self switch to do this, but we can’t do that according to the challenge. Instead we need to get a bit creative with our coding.

    First, divide the value of the variable with the position it’s in and then we use ‘Math.floor()’ to remove any decimals. Then we divide the number by 10 and check it to see if it’s an integer or not. If it’s an integer, the switch has not been thrown.

    It might seem complicated, but it really isn’t. Here’s the first switch event. Graphics and sounds are excluded for clarity, as is the reset code and victory code. My variable is called “Switch variable” and has ID 0004.

    If : Script : Number.isInteger((Math.floor($gameVariables.value(0004)/1000))/10) == true
    Control Variables : #0004 Switch variable += 10000
    Control Variables : #0004 Switch variable += (Math.floor($gameVariables.value(0004)/10000))*1000
    If : Script : (Math.floor($gameVariables.value(0004)/10000)) > 3
    If : Switch variable = 44213

    : Else

    : End
    : Else

    : End
    : Else

    : End

    For each switch, the only thing that needs to be changed is the number ‘1000’. Also, note that the solution could be a variable instead and that the events can easily be modified to use any number of switches.

    And that’s it. One variable, four switches. And for good measure, it can be adapted for N number of switches (where N is one lower than the total number of digits allowed in a variable), it is a compact code (depending on how you set up the reset and victory stuff) that’s easy to follow and it’s fairly easy to set up. You could even set it up to allow for more than one solution and let different solutions lead to different results.