Click here to Skip to main content
15,920,053 members
Home / Discussions / Algorithms
   

Algorithms

 
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 
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 
First off, I didn't vote your last post down. However, I think the brute force approach is not panning out for you here as your logic and code have become somewhat complicated to follow, even for a veteran programmer.

I mentioned earlier a different approach and I'd recommend entertaining the thought that you might have to step back, regroup, and revisit the problems at hand, one at a time (as opposed to all in the same method).

Assumptions about your algorithm...
1) Snake segments and head are all the same size, and each is rectangular.
2) Only one food item will be present at any given moment, and is rectangular.
3) Snake head and segment size is not necessarily the same size as the food item.
4) Collision avoidance should prevent the snake from running through itself.
5) The world coordinate space has no outer boundaries (walls) that we need to be concerned with at the moment.
6) Diagonal movement is allowed.
7) Snake head and body segments are stored in some type of collection.

Ideas on determining direction...
1) Compare the center point of the food rectangle to the center point of the snake head to determine the x and y bias for movement. This should be recalculated each time and the x and y preferred directions should be stored separately.

2) Compute a separate vertical intersection and a separate horizontal intersection for the head and the food to tell you when you would be inline with the food with respect to a particular axis. Since the rectangles might not be the same si ze, this can be used in combination with the centerpoint comparison to prevent constantly jumping back and forth over the center point line as you approach the food.

For example if the y bias is up, but the vertical intersection is not empty, it means you don't need to adjust your vertical direction for this next move. Same goes with the x direction. If both the x and y intersections are not empty, this indicates you've stumbled upon the food. You either end the animation or randomize a new location for a new piece of food and start again after addinga segment to the snake.

3) Start a loop that first checks the full intersection of the proposed head position with each body segment. If the rectangular intersection of any body segment is not empty, the proposed next position is not acceptable and an alternate route will need to be obtained. You would then choose to randomize the next direction choice while keeping track of the possible next steps and the ones you've already tried so that you can detect a stalemate condition (such as wrapping into yourself or something that prevents any legal moves), otherwise it will loop endlessly.

If you determine there is a valid next move, take it and exit this particular loop and repeat the process, starting at step one.

********************************
Again, in a nutshell, the steps are...
1) Which way do you want to go?
2) Which way do you need to go?
3) If you don't get what you want, try a slightly different path.
4) Repeat if necessary.

There are some many variations you can make to this to make it more entertaining but I'm more focused on you coding these steps in multiple methods. Don't try and pack it all into one method.

Prefer to use symbols so we don't have to remember what 1, 2, -1, -2 means...
const int LEFT = 1;
const int RIGHT	= -1;
const int UP = 2;
const int DOWN = -2;
			if (something) {
				direction_comp = UP;
			} else {
				direction_comp = LEFT;
			}

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 
QuestionGood way to detect a useful portion of equivalent expressions Pin
David198718-Mar-11 0:00
David198718-Mar-11 0:00 
AnswerRe: Good way to detect a useful portion of equivalent expressions Pin
CPallini18-Mar-11 1:29
mveCPallini18-Mar-11 1:29 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
David198718-Mar-11 1:41
David198718-Mar-11 1:41 
AnswerRe: Good way to detect a useful portion of equivalent expressions Pin
Luc Pattyn18-Mar-11 2:18
sitebuilderLuc Pattyn18-Mar-11 2:18 

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.