First C# Program- Need a Little Help
Hey. I'm working on my first C# program, and I really haven't read any tutorials or anything like that...which means that my code is probably a HUGE mess...
The program is a text-based game, and currently only the basic combat system and a demonstration of said system has been written, but I'm really worried that it may be "written incorrectly." I've merely written what has made it work without any research into anything. So... =/
That said, I'd really appreciate anyone's help if they'd like to look at the code and tell me what's wrong and how to go about fixing it. All in all, it's currently 309 lines...but a decent bit of that is stuff like output for the end-user and the like...
Help...please. :(
TylerPosted Jun 1, 2007, 8:12 PM
I've tried using enums for things such as the monster's type...but then it says I can't use that enum in a switch() because...well...because I can't. =/
The only way to achieve the goal is to use endless IF statements...if I use enums, anyway. Here's a sample of the switch statement so you know what I'm trying to do.
switch (Monsters.monsterType)
{
case Monsters.monsterType.Goblin:
MonsterAttemptHit();
break;
}
Any ideas to avoid the inefficient chain of IF (or Else-If) statements? =/
Okay...so I've fixed it...sort of...by creating the following method
public void ChooseMonsterAttack(Monsters.monsterType e) { switch (e) { case Monsters.monsterType.Goblin: MonsterAttemptHit(); break; } }
I have one more problem that I'm currently working through. When it's finished, I'll upload the revised source!
Thanks for your help, Jan. :)
TylerPosted Jun 1, 2007, 7:13 AM
About the static variable thing...in program.cs, it sets the monster type just before the battleEncounter() method is called. But if I don't have the variable set up with the public static keywords (I think that's what they're called), then I get an error during build.
Jan MontanoPosted May 31, 2007, 10:58 PM
using switches is a better practice than numerous ifs. Let's take for example your numerous ifs... What will happen if the monster type is a gargoyle(1)? Sure it will satisfy the first condition. But after it exits it, what now? we still have 2 ifs condition which we just don't need. and it will only eat processing time. You could use if else if else if, but that's way no better practice. using switches, aside from having a more readable code, results in more efficiency.
for Properties, Have a look first on it
http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx
Regarding the static, "the variables couldn't be accessed outside of their class." can you show us an example?
If you use static variables in your monster class. and you have monster1 and monster2. setting pHealth=5 for monster1 will also set monster2.phealth=5.
additional reference: http://www.eggheadcafe.com/articles/20020206.asp
TylerPosted May 31, 2007, 10:20 PM
I use "static" because without using static, for some reason, the variables couldn't be accessed outside of their class.
Now that I've got a little more time...
I understand the reasoning behind #2. Thanks. Regarding number 3, is it generally better practice to use a switch instead of numerous IFs? I thought that it was, which is why some of the code is written as switch statements while others are written as a series of IFs. The series of IFs were written before I discovered switches.
I somewhat understand number 4, but number 5 throws me off completely.
I understand number 6.
I will rewrite the appropriate portions of the code once I get more time...that is, when I get off of work.
Something I just thought of...
Why would I want to use enums when I could just do
monsterType = gargoyle;
if (monsterType == gargoyle)
Jan MontanoPosted May 31, 2007, 10:15 PM
or resource file.
if (pToHit > 5) should be something like
...
private const int GargoyleArmor = 5;
if (pToHit > GargoyleArmor)
7. Last but not the least, I don't know how the game really works, But if I were to design it, I'll consider having a separate class for each type of monsters. Class Gargoyle, Class Imp etc... and this Classes inherirts from a base Class Monster.
Here's my Idea...
to be continued...
Jan MontanoPosted May 31, 2007, 9:43 PM
Ok here we go. I'm no best practices guru, so anybody please feel free to add comments and suggestions. I'll just apply my knowledge of best practices which I accumulated from every job hop. Funny thing is, I always end up presenting best practices in my previous jobs. And i just found out last week that there was already a book about it. hehe.
Ok. Enough talk.
1. I've noticed that you're fond of using static modifier in your methods and public variables. Why? Do you know the difference between a static variable and a non-static variable? Just a Question.
2. In your naming convention, there's an mType, and there's a pHealth, pArmor, and mHealth. What's with the 'p' and what's with the 'm'? if your p stands for Player, don't abbreviate it. Putting playerHealth is easier to understand. Just remember that when you abbreviate or put acronyms, you should consider first if it is generally known by the general population (e.g. CPU, ID etc). fName is not acceptable, while firstName is.
3. you have monster.mType == 1, ==2, == 3, also consider using switch statements.
That's it for now. I'll be adding some more later :)
4. monster.mType is an int. You could also use enumerations
instead of public static int mType, you could use
public MonsterType Type
public enum MonsterType
{
Gargoyle = 1,
Minotaur = 2,
Imp = 3
};
5. Also instead of settings variables as public. You could use Properties instead.
private MonsterType type;
public MonsterType Type
{
}
your if (monster.mtype == 1), becomes if (monster.Type == MonsterType.Gargoyle). It's easier to read now.
TylerPosted May 31, 2007, 2:59 PM
Well, first, there's the combat system that I'm currently using. It has a lot of placeholders for methods I haven't written yet, or for if statements that I haven't completed yet but wanted to have an outline of. Most of the code is functional, though.
http://vcpro.zendurl.com/combat.cs
Then there's the main file, which has the main method and introduces the player to a little history and initiates the first battle encounter with the method of the same name.
http://www.zendurl.com/v/vcpro//Program.cs
Yes, I know that even if you die, it still says you made it out blah blah blah. A simple
if (Monster.pHealth <= 0)
{
endProgramCode
}
would fix that. Don't ask why pHealth is in the monster class. A number of variables are going to be moved to the XP/Level up class when I finish writing it.
If there are any questions, go ahead and ask! Help me help you to help me! :)
Again, I'm just concerned about "poor practices" or "doing things incorrectly." I'll worry about code efficiency after the first release.
Jan MontanoPosted May 31, 2007, 5:11 AM
And here's to help you start with http://www.dotnetspider.com/tutorials/BestPractices.aspx
this bool also seems interesting if you're really after best practices.
Practical Guidelines and Best Practices for Microsoft Visual Basic and Microsoft Visual C# Developers
http://search.barnesandnoble.com/booksearch/isbninquiry.asp?ISBN=0735621721&pdf=y
TylerPosted May 31, 2007, 4:56 AM
Jan MontanoPosted May 31, 2007, 4:20 AM