Hey there guys. I was hoping someone would be able to help me figure out how to increment enumeration. I have a game that uses enumeration to get the points for killing an enemy, 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 utilieses 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;
}
Any suggestions? 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 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 any way to keep the highscore value in there so the highest score is always there?
I appreciate all your help with my questions guys, I really do.
Thanks!
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With