TransWikia.com

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

Arqade Asked by ltw on September 11, 2020

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';          
}

One 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:

Answered by Simon Meusel on September 11, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP