Jump to content

  • Log In with Google      Sign In   
  • Create Account


Simple Class Problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Froggggy   Members   -  Reputation: 100

Like
0Likes
Like

Posted 08 January 2012 - 02:05 AM

class Hero
	{
		public int health, strength, weaponStrength, defence;
		public bool isAlive;
		public string identity;

		public static void Good Guy(Hero hero)
		{
			hero.health = 25;
			hero.strength = 6;
			hero.weaponStrength = 15;
			hero.defence = 7;
			hero.isAlive = true;
			hero.identity = "Good Guy";
		}
	}

class Nemisis
	{
		public int health, strength, weaponStrength, defence;
		public bool isEvil, isAlive;
		public string identity;

		public static void Bad Guy(Nemesis nem1)
		{
			nem1.health = 20;
			nem1.strength = 10;
			nem1.weaponStrength = 10;
			nem1.defence = 10;
			nem1.isEvil = true;
			nem1.isAlive = true;
			nem1.identity = "Mean Guy";
		}
	}

class Caculations
	{
		public int Calculate()
		{
			Random numGen = new Random();
			double attackValue, defenceValue, reduction;
			int damage;

			attackValue = (strength * weaponStrength) * (numGen.Next(75, 126) * 0.01);
			defenceValue = defence * 2;
			reduction = (int)attackValue / defenceValue;
			damage = (int)attackValue - (int)reduction;
			return damage;
		}
	}

I've got a fairly simple problem in C#. Thank you in advance.

I'm having trouble linking the attributes (strength & weaponStrength) to the calculations to deal the damage in another class that determines the outcome of the battle. I could call the attributes from both the hero and nemesis, but that would require me to write the same formula twice for both classes, is there a better way?

Sponsor:

#2 Deprecated   Members   -  Reputation: 107

Like
1Likes
Like

Posted 08 January 2012 - 02:22 AM

Even though there is a great difference between a hero and his/hers nemesis in real life, if you look at your code there is (almost) no difference at all. I'd suggest that you create a class Creature/Human/Entity instead and create two separate instances instead.

class Entity {
	private int health, strength, weaponStrength, defence;
	private bool isAlive, isEvil;
	private string identity;
	public void Entity(int health, int strength, int weaponStrength, int defence, bool isAlive, bool isEvil, string identity) {
		this.health = health;
		this.strength = strength;
		this.weaponStrength = weaponStrength;
		this.defence = defence;
		this.isAlive = isAlive;
		this.isEvil = isEvil;
		this.identity = identity;
	}

    public int getStrength() {
        return this.strength;
    }

	public Calculate(Entity enemy) {
		return this.strength - enemy.getStrength();
	}
}

.....
Entity hero = Entity(100, ..., "Good Guy");
Entity nemesis = Entity(100, ..., "Nemesis");

Heads up, I've never written a single line of code in C# so the syntax may be a little off.. Took a guess from the code you provided. Although the concept should be correct.

P.S You should be using getters and setters instead of public variables, gives you more control over you code; google "getters and setters C#" and it should be one of the first results. D.S

Edit: Added a getter for strength.

Edited by Deprecated, 08 January 2012 - 02:25 AM.

Those who can do, do; those who can't do, do teach.

#3 Froggggy   Members   -  Reputation: 100

Like
0Likes
Like

Posted 08 January 2012 - 02:29 AM

Thanks. I'm haven't learned a great deal in C# as of yet. I'm new to programming, so I haven't come across anything beyond basic class, objects, and methods, so the this reference is new to me. Looks like I have more reading tonight.

#4 Deprecated   Members   -  Reputation: 107

Like
0Likes
Like

Posted 08 January 2012 - 02:37 AM

Oh sorry!

this will refer to the actual instance created from your class. You don't have to use it, but since I used the same names for parameters as instance variables it depends on the implementation of that particular language whether it'll work without this or not.

Think of it like the_variables_that_are_defined_on_the_very_top_of_the_class.strength , it is the same as when you're using numGen to call numGen.Next(...) but as there is no name for the instance yet you can use the keyword this.
Those who can do, do; those who can't do, do teach.

#5 Tom KQT   Members   -  Reputation: 1542

Like
1Likes
Like

Posted 09 January 2012 - 04:03 AM

P.S You should be using getters and setters instead of public variables, gives you more control over you code; google "getters and setters C#" and it should be one of the first results. D.S


Or properties in C#.
class Entity {
	// ......
	// ......

	public int Strength {
		get { return this.strength; }
	}
}

You then use it as if it was a public variable and not a method. And you can only read from the property if you define only the get part and not set.

#6 brx   Members   -  Reputation: 694

Like
0Likes
Like

Posted 09 January 2012 - 08:27 AM

public static void Good Guy(Hero hero)


public static void Bad Guy(Nemesis nem1)


Just a note: I assume this is only in your post and not in your actual code, but spaces in identifiers are not allowed (in C# and any other programming language I know of). So this would have to be
public static void GoodGuy(Hero hero)

public static void BadGuy(Nemesis nem1)


#7 NightCreature83   Crossbones+   -  Reputation: 2705

Like
0Likes
Like

Posted 10 January 2012 - 10:54 AM

Oh sorry!

this will refer to the actual instance created from your class. You don't have to use it, but since I used the same names for parameters as instance variables it depends on the implementation of that particular language whether it'll work without this or not.

Think of it like the_variables_that_are_defined_on_the_very_top_of_the_class.strength , it is the same as when you're using numGen to call numGen.Next(...) but as there is no name for the instance yet you can use the keyword this.


Don't think of it as a variable declared at the top of the class because that's not what it is or how it works, think of it as a reference to the current instance of the object. But you do use it like a normal instance of that object however. It is also only valid within the scope of a class and nowhere else, this matters for languages that aren't purely OO, like C/C++.

Each language that supports this has it's own way of how it is implemented and when you start to think of it as something it is not you can get in to trouble.
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#8 Froggggy   Members   -  Reputation: 100

Like
0Likes
Like

Posted 11 January 2012 - 02:00 AM


public static void Good Guy(Hero hero)


public static void Bad Guy(Nemesis nem1)


Just a note: I assume this is only in your post and not in your actual code, but spaces in identifiers are not allowed (in C# and any other programming language I know of). So this would have to be
public static void GoodGuy(Hero hero)

public static void BadGuy(Nemesis nem1)


It was a simple mistake on my end when posting the topic.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS