Algorithm does not work

Non-platform specific questions.

Algorithm does not work

Postby lverona » Sun Jul 01, 2012 2:52 pm

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?
lverona
 
Posts: 221
Joined: Tue Apr 24, 2012 11:54 am
Score: 1 Give a positive score

Re: Algorithm does not work

Postby skydereign » Sun Jul 01, 2012 6:39 pm

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;
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Algorithm does not work

Postby lverona » Sun Jul 01, 2012 8:19 pm

Sorry, was a typo, meant case 1...
lverona
 
Posts: 221
Joined: Tue Apr 24, 2012 11:54 am
Score: 1 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron