What is the best way to increase the number of transfers?

Hey guys. I was hoping that someone could help me figure out how to increase the score. I have a game that uses enumeration to get points for killing an enemy, I want the value of the enemy to increase by 10 times every time one of the enemies was killed. Here is the code I have for listing:

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 a method to get an estimate 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 sophisticated ways to do this that I don't think even works.

In addition, I was interested, I have a high-rated shortcut, which is updated if it is less than the rating, so the result becomes a score, but when the application restarts, if the game is completed or if the player runs out of life, the records are reset back to zero Is there a way to keep the record value in it, so that the highest score is always there?

I appreciate all your help with my questions, guys, I really do.

Thanks!

+7
source share
5 answers

This design is not very object oriented. A better approach would be to have an IEnemy interface. This interface will require the GetScore method (presumably among others). Then this method could 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 advantages over using an enumeration, for example, you may have enemies with the same rating, but with different attributes.

+6
source

In this case, I will not store it as an enumeration - it looks more like range markers than secret values. I may 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 increasing the enumeration: you can apply it to the base type (usually int ):

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

Why use an enumeration? Why not create an IScorable interface that has only the GetScore property and implement it in the Enemy classes. Then you can check if your enemy is IScorable, and if so, read this property. Or, if all of your enemy classes stem from one, then put it there.

Also the way you use Enumeration is incorrect. Enumerations are for completely different purpouses.

+2
source

I would create an abstract class called Enemy with the KillScore property (or whatever you like). Derive each of your enemy types from this:

 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 simply create your own 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 simple:

 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; } 

(The interface will also work, but it gives you some control over the default values, allows you to perform some common functions in the base class, which may or may not be overridden in child classes, etc.).

+2
source

It is probably best to use Polymorphism. This way you will have a base class called the enemy, and it will be abstract.

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

Then you inherit 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 need to create the right 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. 
+2
source

All Articles