2

So I'm struggling with this game as a (super fun!) homework. I got stuck at level 13, after 2 hours I glanced at the other topic about it and tried to implement "stick to the left side" method, but it doesn't seem to work.

Could you guys tell me what's wrong? There might be some pretty fundamental mistakes as my first encounter with JavaScript was on Tuesday.

var direction;

if ( me.getX() == 1 & me.getY() == 1 ){

    if ( me.canMove('right') )
        direction = 0;
    else
        direction = 1;
}

moveRobot( direction );
direction = getDirection();  

function moveRobot( direction ) {

    if ( me.canMove( getDirectionName( direction ) ) ){
        me.move( getDirectionName( direction ) ) ;
    }
    else if ( !me.canMove( getDirectionName( direction ) )
    & me.canMove( getDirectionName( (direction + 1) % 4) ) ){
            me.move( getDirectionName( (direction + 1) % 4) );
            direction = (direction + 1) % 4;
    }
    else {
        me.move( getDirectionName( (direction + 2) % 4) );
        direction = (direction + 2) % 4;
    }

}

function getDirection( ){

    if ( me.canMove( getDirectionName( direction ) ) ){
        return direction;
    }
    else if ( !me.canMove( getDirectionName( direction ) )
    & me.canMove( getDirectionName( (direction + 1) % 4) ) ){
        return (direction + 1) % 4;
    }
    else
        return (direction + 2) % 4; 
}

function getDirectionName( direction ){

    if ( direction == 0 )
        return 'right';
    else if ( direction == 1 )
        return 'down';
    else if ( direction == 2 )
        return 'left';  
    else    
        return 'up';          
}
  • This is on-topic on Stack Overflow. – Anthony Pham Nov 12 '15 at 0:15
  • 1
    @PythonMaster, just to clarify, this is still on topic, here. While StackOverflow can help you with just about any programming-related issue, we still consider it on topic when the problem is also game-related. If you need to solve the programming issue to progress in the game, it is most certainly game-related. vim-adventures is another example of a code-based game – user106385 Nov 12 '15 at 0:31
  • @PythonMaster I thought that this question is difficult to be stated on Stack Overflow properly. After all you need to play this game to be able to see what's inside level 13. – ltw Nov 12 '15 at 0:38
  • Oh, it's a game... Wish I could retract my downvote... – Anthony Pham Nov 12 '15 at 0:39
  • 1
    The game is tagged in the question, @PythonMaster. The question still looks like it could do with a tidy edit, once edited you can change your vote – user106385 Nov 12 '15 at 0:54
0

1. I think a stick to left side implementation of this problim is not possible. If you want to stick to the left side you have to know where the left side is. So yout have to know what the direction of your last move was. Since the variables of the function

'behavior': function (me) {...}

"reset" every time the function gets called, this is not easy to implement.

EDIT: See comment section.

2. You are only setting the value of direction when x = 1 and y = 1 so it only deos something if those coords are right. Since the robot moves the coords change.

if ( me.getX() == 1 & me.getY() == 1 ){

if ( me.canMove('right') )
    direction = 0;
else
    direction = 1; }

Else direction is unset and nothing will work.

You would change it to the following:

moveRobot( direction ); direction = getDirection();

Note: If you cannot solve a level, don't search for the answers of others, because you will lose the fun of that game. If necessary cheat through and solve the level later:

1. Open console (F12) 2. Enter: "localStorage.levelReached = LEVEL_NUMBER" 3. Reload page

  • 1
    This isn't true; the behavior function gets called with the same object every time, so you can use that object to store data. – pppery Aug 18 at 23:39
  • Agree with @pppery. I successfully implemented a stick-to-left-side solution by simply adding a property to the me object. Start with something like if(!me.leftSide) {me.leftSide = 'up';}, and then me.leftSide will be defined and will remember its value between behavior calls. – BenM Dec 2 at 3:43
  • Thank you for your corrections, I updated the answer to point to the comment section. – Simon Meusel Dec 2 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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