Jump to content

C# help


JBags

Recommended Posts

Hey all,

 

I'm writing a silly little text-based turn-based fighting game for a class project, based on the internet's favorite rivalry, Pirates vs ninjas lol.

 

Basically, I have 3 classes:

the parent Character, and the children Pirate and Ninja.

and two objects, one of Pirate and one of Ninja, named player and opponent, based on what the player chooses.

 

Basically what I want to is, say, in the attack function of either class i want to be able to access the other class's HP attribute without specifically using the other class.

 

Example:

 

public class Ninja

{

public void Attack(Pirate opponent)

...

}

 

I dont want to do this, in case I decide to play around with it later on and add more character classes (which would all still be children of Character)

 

Is it possible to do something like this?

 

public class Ninja

{

public void Attack(Character opponent)

...

}

 

Is this possible as both Ninja and Pirate are derived from Character?

 

Any and All assistance is appreciated!

Share this post


Link to post
Share on other sites

I'm pretty sure it would work, as opponent is still a character. Pirates and Ninjas are simply special cases of the character class. Just try it and you'll see!

Edited by The Smith

Share this post


Link to post
Share on other sites

Hey, thanks for the insight, but I never got a chance to try it out..

 

it wouldn't let me declare player and opponent either a ninja or pirate, in an if-else, depending on player input...

 

ie:

 

if (player chooses pirate)

{

pirate player= new pirate();

ninja opponent- new opponent();

}

else

{

ninja player= new ninja();

pirate opponent = new pirate();

}

 

 

if I tried to use a public function of either object, VS 2010 said that the object didnt exist in the current context, and wouldn't compile.

 

So, i scraped the inheritance idea, for now at least. I just used the parent Character and added attributes for identification purposes (like string name= "Pirate"), and modified the respective functions to switch outputs depending on the name attribute.

 

ie: (in pseudo-code)

 

switch(name)

{

case "Pirate":

Output "you attacked ninja for " x "damage"

case "Ninja":

Output "you attacked pirate for " x "damage"

}

 

and that has got the job done for me.

 

I actually finished it for the most part. Anything I do to it from now on is really perfecting maintenance.

 

Though I havent really wrote an algorithm for the AI's battle choices. I just wanted to get a working version running so I used a not-so-random number generator to choose AI battle options.

 

I'll write up a more intelligent approach later..

Share this post


Link to post
Share on other sites

tsk tsk tsk. Why avoiding the problem? That's not how you will learn. Also the inherited classes would have made for a nice chunk of code. It's ugly the way you did it now. :(:P

 

In character, were your functions virtual? Then in your constructor of each inherited class, did you define these functions? It's though to see where the problem is with just these few lines of code, but if your functions were virtual but not defined yet in your constructor, it sure will not compile.

 

Now, do virtual functions exist in C#? At least in C++ they do. :lol:

Share this post


Link to post
Share on other sites

tsk tsk tsk. Why avoiding the problem? That's not how you will learn. Also the inherited classes would have made for a nice chunk of code. It's ugly the way you did it now. :(:P

 

In character, were your functions virtual? Then in your constructor of each inherited class, did you define these functions? It's though to see where the problem is with just these few lines of code, but if your functions were virtual but not defined yet in your constructor, it sure will not compile.

 

Now, do virtual functions exist in C#? At least in C++ they do. :lol:

 

haha yeah I know... :(

 

I think I figured out the problem though. In adding a function for a new battle move, i noticed I had stupidly misplaced a closing bracket. ("}")... turns out both children, Ninja and Pirate were defined INSIDE Character :smack:

 

So i fixed that, as well as making the code neater for easier code reading..

 

I left the children in the code, just didnt use them, so it shouldn't be too hard to re-implement them..

So all I'll have to do is copy the appropriate functions from Character into respective children and dispose of the name switches in them.

 

And no, functions were not virtual, if I happened to think of a new function, I went and wrote it right after.

Share this post


Link to post
Share on other sites

Sorry for the DP.

 

I've gone back and changed a few things, namely, I stupidly had a function in class Character named Configure(); and completely left out a constructor. So I renamed Configure() to Character(). And made all the necessary adjustments, removed the calls to Configure() as the new Character(); call should auto run the Constructor.

 

And now, VS 2010 compiler is giving me an Inconsistent Accessibility error, saying field type Character is less accessible than field(s) player and opponent.

 

 

I declared both player and opponent as such:

 

namespace NvP
{
  class Program
  {
  static public Character player, opponent;

  ...(insert Main and other functions)

  public class Character
  {...}
  public class Ninja : Character
  {...}
  public class Pirate : Character
  {...}
}
}

 

I've been googling, but I still don't understand how the accessibility is inconsistent...

 

 

EDIT: nvm.. it works now for some reason :rolleyes:

Edited by JBags

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...