Jump to content

  • Log In with Google      Sign In   
  • Create Account

Snake in C


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 kaktusas2598   Members   -  Reputation: 883

Like
0Likes
Like

Posted 08 November 2012 - 07:38 AM

Hi, I have goal to create snake in C using SDL for graphics and input. I have already experience with C/++, C# and SDL. So I want to know where I have to start. How to know what data structures and algorithms to use to program Snake. It would be nice if you can point me to some resources of doing it. I also need refresh on SDL, because I haven't used it for some quite time already. Also it can be for games like PacMan, Pong or Tetris. Thanks in advance. I have already created these games in C++/SDL, but it was about 2 years ago, and now I need to use just C and SDL. I would be greatful for any reply.

Deltron Zero and Automator.


Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 9267

Like
0Likes
Like

Posted 08 November 2012 - 08:01 AM

How to know what data structures and algorithms to use to program Snake.

A linked list with head and tail could be appropriate, as each step you could just remove the tail if the snake hasn't grown, and add a new head depending on the direction the snake is facing, I guess. And then use an ordinary list to store the apples on the map and check if the snake's head is on any apple or on any part of its body (by iterating through the linked list - this might not be ideal). At least that's what springs to mind at first - there are possibly better ways to do it.

A simple 2D array which keeps track of the snake's head and tail would probably work better here anyway, in fact.

Edited by Bacterius, 08 November 2012 - 08:11 AM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 SuperVGA   Members   -  Reputation: 1118

Like
0Likes
Like

Posted 08 November 2012 - 09:51 AM

I'd say you only need to check the apples against the head once the head enters a new tile.
Just don't spawn apples on the snake's tail.
That's fewer checks upon snake movement, at least.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS