Page 1 of 1

Algorithm does not work

PostPosted: Sun Jul 01, 2012 2:52 pm
by lverona
Hey fellas!

Writing an algorithm to have a robot solve a maze. I know the algorithm well and have implemented it some time ago using php. In GE, however, it does not seem to work and I have no idea why the robots behaves that way.

Here is the code:

Code: Select all
short int choice=0;//rand(4)

switch(choice){

case 0:
if(CollisionFree("Event Actor",x,y+1)==1
    && (in_array(memorx, 100, x)==-1 || in_array(memory, 100, (y+1))==-1) ){


//memorizing
memorx[memorx_index]=x;
memory[memory_index]=y+1;
//updating memory index
if(memorx_index+1==100){memorx_index=0;memory_index=0;}
else {memorx_index++;memory_index++;}

y+=1;

break;


}




case 1:
if(CollisionFree("Event Actor",x,y-1)==1
    && (in_array(memorx, 100, x)==-1 || in_array(memory, 100, (y-1))==-1) ){


//memorizing
memorx[memorx_index]=x;
memory[memory_index]=y-1;
//updating memory index
if(memorx_index+1==100){memorx_index=0;memory_index=0;}
else {memorx_index++;memory_index++;}

y-=1;

break;
}


case 2:
if(CollisionFree("Event Actor",x-1,y)==1
    && (in_array(memorx, 100, (x-1))==-1 || in_array(memory, 100, y)==-1) ){


//memorizing
memorx[memorx_index]=x-1;
memory[memory_index]=y;
//updating memory index
if(memorx_index+1==100){memorx_index=0;memory_index=0;}
else {memorx_index++;memory_index++;}

x-=1;

break;

}

case 3:
if(CollisionFree("Event Actor",x+1,y)==1
    && (in_array(memorx, 100, (x+1))==-1 || in_array(memory, 100, y)==-1) ){


//memorizing
memorx[memorx_index]=x+1;
memory[memory_index]=y;
//updating memory index
if(memorx_index+1==100){memorx_index=0;memory_index=0;}
else {memorx_index++;memory_index++;}

x+=1;

break;
}




}



I am not asking to review the code, for now just want to know: am I right in assuming that if condition in case:0 does not work, the code should go to case 2?

Re: Algorithm does not work

PostPosted: Sun Jul 01, 2012 6:39 pm
by skydereign
Using the code you have, yes, it should move on to the next case if the condition isn't met (since the break statement is within the condition). Though, it will next go to case 1 instead of just jumping to case 2.
Code: Select all
switch(var)
{
    case 0: // triggers if var==0
    //  code

    case 1: // triggers if var==1 or var==0
    break; // get out of the switch statement

    case 2: // only triggers if var==2
    break;
}

Re: Algorithm does not work

PostPosted: Sun Jul 01, 2012 8:19 pm
by lverona
Sorry, was a typo, meant case 1...