views:

293

answers:

3

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??

+5  A: 

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).

Toad
+1  A: 

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). Example:

class SuperPower
{
   public static SuperPower omNomNom(Food f)
   {
      if(f==InvisibilityFruit) /*You can use switch but it depends on language*/
      { 
         return InvisibilityPower()
      }
      else if
      {
         ...
      }

   }

}

class InvisibilityPower extends SuperPower{...}
Daniel Fath
+1  A: 

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.

GrayWizardx