Stack Overflow is a useful resource for programmers — a repository of individual questions and collective responses, by and for programmers.

Someone recently asked a Snake Game Design Question:

I’m trying to make a snake game with additional functionality where snake can eat different types of food some of the foods will give it special powers for some time. like that

For designing diff. food i’m making a food interface. And all types of food implement it. So that using only Food’s ref. i can create any type of food.

The only power is representing a power. I can represent it either on board or in the snake. Snake is the best option as it seems to be more logical. Can any one tell me how am i suppose to represent it??

Snake is a classic game — one of my favorites.
Snake
Wikipedia states: “Snake is a video game first released during the mid 1970s in arcades and has maintained popularity since then, becoming somewhat of a classic. After it became the standard pre-loaded game on Nokia phones in 1998, Snake found a massive audience.”

The Stack Overflow responses include:

You might want to take a look at the Template Pattern or the Decorator Pattern.

The basic idea would be that your “Snake” would have its operations exported into a module tree which are called. So for instance Snake.Move() would really just check to see if there was a move modifier (as provided from your “powers”) otherwise it would default to its own internal move object. Depending on how you implement it the power could replace, temporarily override, or cascade its effects.

And:

You could create a base powerclass of which every food holds a reference. Every food this way can have a certain power.

For every power, you inherit form this base powerclass.

the moment you eat the food, the power class is transferred to the snake. The snake could hold one reference (to the last eaten power), or even a list of powers (if multiple powers can be active at the same time).

And:

Yes, per snake is more flexible. If you were to make it a multiplayer game then each snake would have its power.

What you seem to be missing is Power->Food mapping. But that really depends on whether or not one Food gives many Powers or one Power can have different powers.

Well there are many ways how you can do this. Most basic I can think of is having a static method that will produce a different powers when passed different type of food. Whenever your snake eats something you call
SuperPower.onNomNom(FoodEaten)

Leave a Reply