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

Algorithms

 
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 
After looking at the full code you posted in a different branch, there are a few pieces of advice I'd like to give out:

(I) General coding advice

1. Don't put more than one statement on one line. It just makes the code harder to read. This does include control statements such as if or else, but not neccessarily tokens such as curly braces.

2. Don't use magic numbers. As others already pointed out, the constants -2, -1, 1, or 2 may be shorter to write, but they're also harder to read. Also, it's much easier to accidentally omit (or add) a '-' and thus swap an UP with DOWN or LEFT with RIGHT, than accidentally swapping symbols that actually say what they mean, rather than imply.

3. Don't write long functions. If your code (after reformatting according to item 1 above) is longer than ~50 lines it's about time to consider if you shouldn't split this function into multiple parts, or if there are repetitive parts in this function that you could extract into a separate function.

(II) 2D graphical coding advice

4. Don't encode a direction as a single number in a two-dimensional world. If you're traversing a linear array, then it makes sense to encode the direction you're moving in as -1 or 1. In 2D however it is much more practical to denote the X- and Y-components of your movement separately. It is quite easy to take the current position of an object, add to it the movement vector, and then compare it to the positions of objects that it might bump into. It's a lot more complicated to make if statements for each direction that you could move to, and it requires you to duplicate your code for every possible direction! (that is what you obviously did)

5. Provide a simple function or set of functions for collision checks between different types of shapes in your 2D-world. Apparently, snake segments, and food patches all occupy a rectangular box of a fixed size. Just define a struct called box that contains their position (x,y) and size (width, height); then define a function that checks two boxes for overlaps (or collision).

6. A somewhat more sophisticated system with 2D elements and collision checks can be found here. This goes beyond your requirements, but gives a very good insight into coding techniques for the kind of problems you're struggling with.
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 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
David198718-Mar-11 2:24
David198718-Mar-11 2:24 
AnswerRe: Good way to detect a useful portion of equivalent expressions Pin
Luc Pattyn18-Mar-11 3:32
sitebuilderLuc Pattyn18-Mar-11 3:32 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
David198718-Mar-11 3:47
David198718-Mar-11 3:47 
AnswerRe: Good way to detect a useful portion of equivalent expressions Pin
_Erik_18-Mar-11 3:02
_Erik_18-Mar-11 3:02 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
David198718-Mar-11 3:18
David198718-Mar-11 3: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.