deucalion0
deucalion0

Reputation: 2440

What is the best way to increment an enumeration?

How can I increment an enumeration?

I have a game that uses enumeration to get the points for killing an enemy, and I want the value of the enemy to increment by 10 every time one of the enemies is killed. Here is the code I have for the enumeration:

public enum gamescore // Enumeration to hold the score
                      // values of the enemies
{
    Martian = 10,
    Vesuvian = 20,
    Mercurian = 30,
    Meteor = 50,
    MotherShip = 100,
    Destroyer = 200
}

and the method to get the score called from another class when an enemy dies:

    public int GetScore() // The method that uses the enumeration to
                          // get the score for the enemy killed
    {
        if (this is Martian)
        {
            return (int)gamescore.Martian;
        }
        else if (this is Vesuvian)
        {
            return (int)gamescore.Vesuvian;
        }
        else if (this is Mercurian)
        {
            return (int)gamescore.Mercurian;
        }
        else if (this is Destroyer)
        {
            return (int)gamescore.Destroyer;
        }
        else if (this is Meteor)
        {
            return (int)gamescore.Meteor;
        }
        else if (this is Mothership)
        {
            return (int)gamescore.MotherShip;
        }
        return 0;
    }

How can I do it? I can only come up with complicated ways to do this, that I don't think even works.

Also I was wondering, I have a highscore label that is updated if it is less than score, so the highscore becomes score. But when the application restarts, if the game is completed or if the player runs out of lives, the highscore resets back to zero. Is there a way to keep the highscore value in there, so the highest score is always there?

Upvotes: 7

Views: 8761

Answers (5)

Corith Malin
Corith Malin

Reputation: 1525

Probably your best bet would be to use polymorphism. So you'd have a base class called enemy and it'd be abstract.

public abstract class Enemy
{
  public abstract int GetScore();
}

Then you'd inherit from it for each time.

public class Martian : Enemy
{
  public int GetScore() { return 10; }
}

public class Vesuvian : Enemy
{
  public int GetScore() { return 20; }
}

Then in your main method you just have to create the correct type of enemy:

Enemy blah = new Martian();
Enemy blah1 = new Vesuvian();

int x = blah.GetScore(); // This is 10.
int y = blah1.GetScore(); // This is 20.

Upvotes: 2

sgriffinusa
sgriffinusa

Reputation: 4221

This design is not very object-oriented. A better approach would be to have an IEnemy interface. That interface would require a method of GetScore (presumably among others). That method could then return the value of the enemy. Then you have a separate class for each of your enemies that implements the IEnemy interface.

public interface IEnemy{
   int GetScore();
}

public class Martian : IEnemy{
   int GetScore(){ return 10; }
}

public class Vesuvian : IEnemy{
   int GetScore(){ return 20; }
}
...

This has many benefits over using an enumeration, for example you can have enemies that have the same score but different other attributes.

Upvotes: 5

Wonko the Sane
Wonko the Sane

Reputation: 10813

I would create an Abstract class called Enemy, with a property called KillScore (or whatever you like). Derive each of your enemy types from that:

public abstract class Enemy
{
    virtual public int KillScore
    {
        get { return 0; }
    }
}

public class Martian : Enemy
{
    public override int KillScore
    {
        get { return 10; }
    }
}

// All the other classes here

public class Destoyer : Enemy
{
    public override int KillScore
    {
        get { return 200; }
    }
}

You just instantiate your individual classes, for example:

    Martian martian = new Martian();
    Destoyer destroyer = new Destoyer();
    int score = GetScore(martian);
    score = GetScore(destroyer);

Then your GetScore function becomes very easy:

public int GetScore(Enemy enemy)
{
    // Debug Statement to show you what this enemy actually is
    Console.WriteLine("{0} {1}", enemy.GetType(), enemy.KillScore);
    return enemy.KillScore;
}

(An interface would work as well, but this gives you some control over default values, allows you to do some common functionality in the base class that may or may not be overridden in the child classes, etc.).

Upvotes: 2

Euphoric
Euphoric

Reputation: 12849

Why using enumeration? Why not either create interface IScorable, that has only property GetScore and implement it in your "Enemies" classes. Then you can test if your enemy is IScorable and if yes, read this property. Or if all your enemy classes derive from one, then put it in there.

Also The way you use Enumeration is wrong. Enumerations are intended for completly different purpouses.

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062895

In this case I wouldn't store it as an enum - it sounds more like range markers than discreet values. I might have some constants and a method that checks

if(score>100) return "awesome";
if(score>40) return "meh";

Etc

But to answer the question about incrementing an enum: you can cast it to the base-type (usually int):

myEnumValue = (MyEnum)((int)myEnumValue + 10);

Upvotes: 7

Related Questions