Algorithm [modified]
-
Hi, I am coding an algorithm that searches for a box on the screen. So far I got the snake to find the food which works great except that for example. If the snake is moving right it will sometimes move left and move ontop of it's self. I've spent awhile trying to figure it out on my own with no success. I think it has to do with the two blocks of if statements but I'm not sure. Maybe they need to be combined somehow? This is the last thing I need to figure out and my program is finished. Thanks in advance. I tried.
if(direction_comp!=1){direction_comp=-1;} else {direction_comp=2;} // If snake is not moving left then move it right else move it up to avoid it from moving on top of it's self.
direction_comp of 1 = left;
direction_comp of -1 = right;
direction_comp of 2 = up;
direction_comp of -2 = down;void comp_direction()
{if(segments\_comp\[head\_comp\]->panel->Left <= Food->Left) { if(segments\_comp\[head\_comp\]->panel->Top >= Food->Top){direction\_comp =2;} else{direction\_comp=-1;} } else if(segments\_comp\[head\_comp\]->panel->Left >= Food->Left) { if(segments\_comp\[head\_comp\]->panel->Top >= Food->Top){direction\_comp =2;} else{direction\_comp=1;} } if(segments\_comp\[head\_comp\]->panel->Top <= Food->Top) { if(segments\_comp\[head\_comp\]->panel->Left <= Food->Left){direction\_comp =-1;} else{direction\_comp=-2;} } else if(segments\_comp\[head\_comp\]->panel->Top >= Food->Top) { if(segments\_comp\[head\_comp\]->panel->Left >= Food->Left){direction\_comp =1;} else{direction\_comp=2;} } }
modified on Friday, April 15, 2011 7:47 PM
-
Hi, I am coding an algorithm that searches for a box on the screen. So far I got the snake to find the food which works great except that for example. If the snake is moving right it will sometimes move left and move ontop of it's self. I've spent awhile trying to figure it out on my own with no success. I think it has to do with the two blocks of if statements but I'm not sure. Maybe they need to be combined somehow? This is the last thing I need to figure out and my program is finished. Thanks in advance. I tried.
if(direction_comp!=1){direction_comp=-1;} else {direction_comp=2;} // If snake is not moving left then move it right else move it up to avoid it from moving on top of it's self.
direction_comp of 1 = left;
direction_comp of -1 = right;
direction_comp of 2 = up;
direction_comp of -2 = down;void comp_direction()
{if(segments\_comp\[head\_comp\]->panel->Left <= Food->Left) { if(segments\_comp\[head\_comp\]->panel->Top >= Food->Top){direction\_comp =2;} else{direction\_comp=-1;} } else if(segments\_comp\[head\_comp\]->panel->Left >= Food->Left) { if(segments\_comp\[head\_comp\]->panel->Top >= Food->Top){direction\_comp =2;} else{direction\_comp=1;} } if(segments\_comp\[head\_comp\]->panel->Top <= Food->Top) { if(segments\_comp\[head\_comp\]->panel->Left <= Food->Left){direction\_comp =-1;} else{direction\_comp=-2;} } else if(segments\_comp\[head\_comp\]->panel->Top >= Food->Top) { if(segments\_comp\[head\_comp\]->panel->Left >= Food->Left){direction\_comp =1;} else{direction\_comp=2;} } }
modified on Friday, April 15, 2011 7:47 PM
You may get a better response by posting here: http://www.codeproject.com/Forums/326859/Algorithms.aspx[^]
Best wishes, Hans