• Advertisement
Sign in to follow this  

Simple Class Problem

This topic is 2202 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[code]
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;
}
}
[/code]

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

[CODE]
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");
[/CODE]

[i]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.[/i]

[i]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[/i]

[i][b]Edit:[/b] Added a getter for strength.[/i] Edited by Deprecated

Share this post


Link to post
Share on other sites
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 [color=#0000cd]this[/color] reference is new to me. Looks like I have more reading tonight.

Share this post


Link to post
Share on other sites
Oh sorry!

[font=courier new,courier,monospace]this[/font] 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 [font=courier new,courier,monospace]this[/font] or not.

Think of it like [font=courier new,courier,monospace]the_variables_that_are_defined_on_the_very_top_of_the_class.strength[font=arial,helvetica,sans-serif] , it is the same as when you're using [font=courier new,courier,monospace]numGen[font=arial,helvetica,sans-serif] to call [/font]numGen.Next(...)[/font] but as there is no name for the instance yet you can use the keyword [font=courier new,courier,monospace]this[/font].[/font][/font]

Share this post


Link to post
Share on other sites
[quote name='Deprecated' timestamp='1326010955' post='4900578']
[i]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[/i]
[/quote]

Or [url="http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx"]properties[/url] in C#.
[code]
class Entity {
// ......
// ......

public int Strength {
get { return this.strength; }
}
}
[/code]

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 [b]get [/b]part and not [b]set[/b].

Share this post


Link to post
Share on other sites
[quote name='Froggggy' timestamp='1326009932' post='4900576']
[code]
public static void Good Guy(Hero hero)


public static void Bad Guy(Nemesis nem1)
[/code]
[/quote]

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
[code]
public static void GoodGuy(Hero hero)

public static void BadGuy(Nemesis nem1)
[/code]

Share this post


Link to post
Share on other sites
[quote name='Deprecated' timestamp='1326011871' post='4900581']
Oh sorry!

[font=courier new,courier,monospace]this[/font] 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 [font=courier new,courier,monospace]this[/font] or not.

Think of it like [font=courier new,courier,monospace]the_variables_that_are_defined_on_the_very_top_of_the_class.strength[font=arial,helvetica,sans-serif] , it is the same as when you're using [font=courier new,courier,monospace]numGen[font=arial,helvetica,sans-serif] to call [/font]numGen.Next(...)[/font] but as there is no name for the instance yet you can use the keyword [font=courier new,courier,monospace]this[/font].[/font][/font]
[/quote]

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.

Share this post


Link to post
Share on other sites
[quote name='brx' timestamp='1326119227' post='4900946']
[quote name='Froggggy' timestamp='1326009932' post='4900576']
[code]
public static void Good Guy(Hero hero)


public static void Bad Guy(Nemesis nem1)
[/code]
[/quote]

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
[code]
public static void GoodGuy(Hero hero)

public static void BadGuy(Nemesis nem1)
[/code]
[/quote]

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement