Click here to Skip to main content
15,903,854 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: Ackerman non-recursive function Pin
Ravi Sant5-May-11 0:31
Ravi Sant5-May-11 0:31 
AnswerRe: Ackerman non-recursive function Pin
Luc Pattyn5-May-11 1:48
sitebuilderLuc Pattyn5-May-11 1:48 
GeneralRe: Ackerman non-recursive function Pin
Richard MacCutchan5-May-11 4:18
mveRichard MacCutchan5-May-11 4:18 
QuestionSearch Algorithm Pin
Cyclone_S16-Apr-11 8:22
Cyclone_S16-Apr-11 8:22 
QuestionRe: Search Algorithm Pin
bob1697216-Apr-11 9:28
bob1697216-Apr-11 9:28 
AnswerRe: Search Algorithm Pin
Cyclone_S16-Apr-11 13:10
Cyclone_S16-Apr-11 13:10 
GeneralRe: Search Algorithm Pin
bob1697216-Apr-11 17:52
bob1697216-Apr-11 17:52 
GeneralRe: Search Algorithm Pin
Cyclone_S20-Apr-11 9:58
Cyclone_S20-Apr-11 9:58 
Thanks for taking the time to respond.

The snake is supposed to search for food and check if it collides with it's self. I spent a few days on this and I got it partially working but the snake still intersects with it's self in certain situations(see image). Do you see anything wrong with this code?

http://img163.imageshack.us/i/screengby.jpg/[^]

The first section the snake goes in the direction of the food.
The second section is the code that checks for colision.

left = 1
right = -1
up = 2
down = -2

void comp_direction()
			{
	
				
				// Make The Snake Search For Food
				int test = direction_comp;

				if(segments_comp[head_comp]->panel->Top <= Food->Top)
				{
					if(segments_comp[head_comp]->panel->Left > Food->Left){direction_comp = -2;} else{direction_comp=-1;}

				}
				else
				{	
					if(segments_comp[head_comp]->panel->Left < Food->Left){direction_comp =2;}else{direction_comp=1;}

				}

				


				// Check for Computer Body Collision
				if(direction_comp==1)
				{
					for(int I=0;I<(size_comp-1);I++) // Coliding Left
					{
						if(head_comp2->Left == segments_comp[I]->panel->Right && head_comp2->Top == segments_comp[I]->panel->Top)
						{
							for(int I=0;I<(size_comp-1);I++) // Coliding Left
							{
								if(head_comp2->Bottom == segments_comp[I]->panel->Top && head_comp2->Left == segments_comp[I]->panel->Left) // Coliding Left Bottom Corner
								{direction_comp = 2;break;}
								else if(head_comp2->Top == segments_comp[I]->panel->Bottom && head_comp2->Left == segments_comp[I]->panel->Left) // Coliding Left Top Corner
								{direction_comp = -2;break;}
								else {direction_comp = 2;} // Coliding Left (will be random)
							}
						}
					}
				}

				else if(direction_comp==-1)
				{
					for(int I=0;I<(size_comp-1);I++) // Coliding Right
					{
						if(head_comp2->Right == segments_comp[I]->panel->Left && head_comp2->Top == segments_comp[I]->panel->Top)
						{
							for(int I=0;I<(size_comp-1);I++)
							{
								if(head_comp2->Top == segments_comp[I]->panel->Bottom && head_comp2->Left == segments_comp[I]->panel->Left) // Coliding Left Top Corner
								{direction_comp = -2;break;}
								else if(head_comp2->Bottom == segments_comp[I]->panel->Top && head_comp2->Left == segments_comp[I]->panel->Left) // Coliding Left Bottom Corner
								{direction_comp = 2;break;}
								else{direction_comp =2;} // Coliding Right (will be random)
							}
						}
					}
				}

				else if(direction_comp==2)
				{
					for(int I=0;I<(size_comp-1);I++) // Coliding Up
					{
						if(head_comp2->Top == segments_comp[I]->panel->Bottom && head_comp2->Left == segments_comp[I]->panel->Left)
						{
							for(int I=0;I<(size_comp-1);I++) 
							{
								if(head_comp2->Right == segments_comp[I]->panel->Left && head_comp2->Top == segments_comp[I]->panel->Top) // Coliding Up Right Corner
								{direction_comp = 1;break;}
								else if(head_comp2->Left == segments_comp[I]->panel->Right && head_comp2->Top == segments_comp[I]->panel->Top) // Coliding Up Left Corner
								{direction_comp = -1;break;}
								else{direction_comp = -1;} // Coliding Top (will be random)
							}
							
						}
					}
				}

				else if(direction_comp==-2)
				{
					for(int I=0;I<(size_comp-1);I++) // Coliding Down
					{
						if(head_comp2->Bottom == segments_comp[I]->panel->Top && head_comp2->Left == segments_comp[I]->panel->Left)
						{
							for(int I=0;I<(size_comp-1);I++) // Coliding Down
							{
								if(head_comp2->Right == segments_comp[I]->panel->Left && head_comp2->Top == segments_comp[I]->panel->Top) // Colliding Right Bottom Corner
								{direction_comp =1;break;}
								else if(head_comp2->Left == segments_comp[I]->panel->Right && head_comp2->Top == segments_comp[I]->panel->Top) // Colliding Left Bottom Corner
								{direction_comp =-1;break;}
								else {direction_comp =1;} // Coliding Down (will be random)
							}
						}
					}
				}


				
				if(test==1 && direction_comp==-1){direction_comp=2;}
				else if(test==-1 && direction_comp==1){direction_comp=2;}
				else if(test==2 && direction_comp==-2){direction_comp=1;}
				else if(test==-2 && direction_comp==2){direction_comp=1;}
				
			}

GeneralRe: Search Algorithm Pin
bob1697220-Apr-11 18:06
bob1697220-Apr-11 18:06 
GeneralRe: Search Algorithm Pin
Cyclone_S21-Apr-11 14:45
Cyclone_S21-Apr-11 14:45 
GeneralRe: Search Algorithm Pin
Cyclone_S22-Apr-11 9:57
Cyclone_S22-Apr-11 9:57 
GeneralRe: Search Algorithm Pin
bob1697224-Apr-11 4:59
bob1697224-Apr-11 4:59 
GeneralRe: Search Algorithm Pin
Cyclone_S27-Apr-11 13:43
Cyclone_S27-Apr-11 13:43 
AnswerRe: Search Algorithm Pin
Luc Pattyn20-Apr-11 12:39
sitebuilderLuc Pattyn20-Apr-11 12:39 
AnswerRe: Search Algorithm Pin
AspDotNetDev20-Apr-11 13:12
protectorAspDotNetDev20-Apr-11 13:12 
GeneralRe: Search Algorithm Pin
Cyclone_S20-Apr-11 13:40
Cyclone_S20-Apr-11 13:40 
AnswerRe: Search Algorithm Pin
Stefan_Lang26-Apr-11 4:32
Stefan_Lang26-Apr-11 4:32 
GeneralRe: Search Algorithm Pin
Cyclone_S27-Apr-11 13:50
Cyclone_S27-Apr-11 13:50 
Questionconverting MFC program into web based Pin
shiks11-Apr-11 17:06
shiks11-Apr-11 17:06 
AnswerRe: converting MFC program into web based Pin
Albert Holguin11-Apr-11 18:13
professionalAlbert Holguin11-Apr-11 18:13 
GeneralRe: converting MFC program into web based Pin
shiks12-Apr-11 5:15
shiks12-Apr-11 5:15 
GeneralRe: converting MFC program into web based Pin
Albert Holguin12-Apr-11 5:19
professionalAlbert Holguin12-Apr-11 5:19 
GeneralRe: converting MFC program into web based Pin
Albert Holguin12-Apr-11 5:21
professionalAlbert Holguin12-Apr-11 5:21 
AnswerRe: converting MFC program into web based Pin
Eddy Vluggen12-Apr-11 8:25
professionalEddy Vluggen12-Apr-11 8:25 
GeneralRe: converting MFC program into web based Pin
shiks13-Apr-11 17:02
shiks13-Apr-11 17:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.