What's wrong with my level 13 solution? ( "stick to left side" implementation attempt)

What's wrong with my level 13 solution? ( "stick to left side" implementation attempt) - If You Judge People You Have No Time To Love Them text spelled out with pink letter tiles of famous word game against black background

So I'm struggling with Untrusted 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 the "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';          
}


Best Answer

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




Pictures about "What's wrong with my level 13 solution? ( "stick to left side" implementation attempt)"

What's wrong with my level 13 solution? ( "stick to left side" implementation attempt) - Inspirational Message on Blue Concrete Pavement
What's wrong with my level 13 solution? ( "stick to left side" implementation attempt) - Female artist sitting on street and doing sketches
What's wrong with my level 13 solution? ( "stick to left side" implementation attempt) - Close-Up Shot of Scrabble Tiles on a White Surface





Rnbstylerz \u0026 AREES - WHAT




Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

Images: Alesia Kozik, Eva Bronzini, Felicity Tai, Anna Tarazevich