Take the 2-minute tour ×
Arqade is a question and answer site for passionate videogamers on all platforms. It's 100% free, no registration required.

In Untrusted, I got to Level 7, where I can get the phone, and create my own JS function. I saw from the demo that I can change the colour! Great! So I figured I'd write a nested if... then statement, saying if I'm one colour, switch to the next! Apparently, that does not work.

I then tried to change my colour to white (#fff), thinking the collision check would accept white as all colours, and open the locks. What can I put in this function to change me as needed?

enter image description here

share|improve this question
    
Your image shows minimal effort. This is the if.. else if.. challenge. White over here will not help you. Try if green.. then red.. if red.. then yellow.. if yellow back to green. :-P –  ヴァイシャリ Apr 10 at 18:42
    
@ヴァイシャリ I realize it now, but I couldn't spoil it all in the question! –  Canadian Luke Apr 10 at 18:55
    
Heh I did not realize it was solved already. I tried to a one place Trusted walkthrough. They over rode me. (Pun intended) :-) –  ヴァイシャリ Apr 10 at 18:58

4 Answers 4

up vote 4 down vote accepted

You can get past all the gates using the code below:

if(player.getColor()=='#f00')
   player.setColor('#ff0');
else if(player.getColor()=='#ff0')
   player.setColor('#0f0');
else
   player.setColor('#f00');

Basically, it makes it so every time you hit Q, you change to the color of the next gate.
Green -> Red -> Yellow -> Green

Finish

share|improve this answer
    
Aaarg! I was using a single equals sign! I'm posting my (much longer) answer as well, but that would definitely work! –  Canadian Luke Apr 8 at 19:44
    
@CanadianLuke I do that all the time too. Now I'll have to play this game the rest of the day. –  Batophobia Apr 8 at 19:47
    
I like the old-style games, like Candybox 1 and 2, as well –  Canadian Luke Apr 8 at 19:49
    
Exactly what I did xD –  Darakath Apr 8 at 20:51
    
Posting solutions –  ヴァイシャリ Apr 10 at 18:48

If you have solved it once, now you can play with it, improving the code. Thinking what else you can do, for example you can do something ugly and funny:

player.setPhoneCallback(function () {
  var i = 66;
  var callback = function(){
    player.setColor("#" + ((--i % 3) > 0 ? 'f' : '0') + ((i + 1) % 3 > 0 ? 'f' : '0') + '0')
  }
  return callback;
}());
(function doNothingHack(){ 
});

Also we can see that f00, ff0 and 0f0 can be represented as 100, 110, and 010, and this, as you can see are just 3 small integers (2, 3, 1) represented as binary. So modulo arithmetic, decimal to binary conversion then replace 1 with f, and we have another such solution.

share|improve this answer
    
Heh nice one. +1 –  ヴァイシャリ Apr 10 at 18:44

I decided to go ahead and post a few solutions of my own.

Switch

This one makes use of switch statements and saves each color as an object literal to improve readability and increase coding speed and efficiency:

var color = { 'green' : '#0f0',
              'red'   : '#f00',
              'yellow': '#ff0'};

switch(player.getColor()) {
    case color.green:
        player.setColor(color.red); break;

    case color.red:
        player.setColor(color.yellow); break;

    case color.yellow:
        player.setColor(color.green); break;

    default:
        player.setColor(color.green);
}

The default case at the end isn't necessary for that particular level, as the player's color is already green by default, but I decided to include it anyways for completion's sake.


Increment and modulo

This solution makes use of the increment operator ++ and the modulo operator %, making the code much, much shorter.

var colors = ['#0f0', '#f00', '#ff0'];
var i = 0;

player.setPhoneCallback(function() {
    player.setColor(colors[++i % 3]);
});

player.setColor(colors[++i]);

Since we can't add code outside the player.setPhoneCallback() method, it has become a bit hacky.

The idea behind this solution, is to define a color array which we will cycle through. For that, the increment and modulo operators are instrumental. For those who don't know: the modulo operator returns the rest of a division.

Since we don't want our variable i to be reset every time we use the phone, we have to redefine the phone method. Once we're done, we call player.setColor() to change our color. That part isn't necessary, but without it, we'd have to use the phone twice to change our color for the first time.

Once you understand the above code, we can shorten it even more:

if (!player.i) player.i = 0;
player.setColor(['#0f0', '#f00', '#ff0'][++player.i % 3]);
share|improve this answer
    
I wanted to post a solution using setInterval, but apparently the method is not allowed. Damn it... –  Nolonar Apr 12 at 22:00

Found the solution! I inserted the following code:

if (player.atLocation(24, 12)) {
player.setColor('#f00');
}
if (player.atLocation(27, 12)) {
player.setColor('#ff0');
}
if (player.atLocation(30, 12)) {
player.setColor('#0f0');
}
if (player.atLocation(33, 12)) {
player.setColor('#f00');
}
if (player.atLocation(35, 12)) {
player.setColor('#ff0');
}

If I am at the x, y coordinates, then it will execute that one colour change for me.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.