-
June 7th, 2012, 04:20 PM
#1
Game problem
Hey im a new C# learner and im currently following a guide on a website. In the guide we are currently making a rpg (role playing game) and i just cant seem to get it to work. Now this may see a little wall of textish but im going to post my code for all the classes and stuff. The error i get a lot is The name *____* does not exist in the current context. Also the error member names can not be the same as their enclosing type( this one seems to have gone away with some tweaking). So heres my code thanks in advance to anyone who can help.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RpgTutorial
{
class Damage
{
int DealDamage(Hero hero, Monster monster)
{
int max;
int min;
rand = new Random();
max = hero.AttackDamage - monster.Defense;
if (max <= 0)
{
max = 1;//right now this isn't a concern, but this is a bit
//of future proofing our damage mechanism
}
min = (int)(hero.AttackDamage * .8) - monster.Defense;//**I'll explain this one**
if (min <= 0)
{
min = 1;
}
damage = rand.Next(min, max);//calculate the damage
return damage;//send it on back
}
bool CheckHealth(int health)
{
bool alive;
if (health > 0)
{
alive = true;
}
else
{
alive = false;
}
return alive;
}
}
}
__________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RpgTutorial
{
class PrintChoices
{
string PrintChoice()
{
string choice;
Console.WriteLine();
Console.Write(@"
_____________________
Please choose an action:
(A)ttack:
(D)efend:
_____________________");
Console.WriteLine();
choice = Console.ReadLine();
return choice;
}
}
}
_______________________________________________________________________using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RpgTutorial
{
class ProcessChoices
{
void ProcessChoice(string choice, Hero hero, Monster monster)
{
switch (choice)
{
case "A":
case "a":
Console.WriteLine();
Console.WriteLine("{0} attacks!", hero.Identifier);//print what it is
DealDamage(hero, monster);//send it to our damage method
monster.CurrentHealth -= damage;//take it from the monster
Console.WriteLine("{0} hits the {1} for {2}hp of damage"
, hero.Identifier, monster.Identifier, damage);//show result
break;
case "D":
case "d":
Console.WriteLine();
Console.WriteLine("{0} defends!", hero.Identifier);
//we will make defending more beneficial later
//Many times you will see this shown like below:
//To Do: Add logic to make defending beneficial
break;
default:
Console.WriteLine("I'm sorry, I didn't recognize that.");
Console.WriteLine();//if the choice isn't valid
choice = PrintChoice();//the we'll keep asking till
Console.WriteLine();//they get it right
ProcessChoice(choice, hero, monster);
break;
}
}
}
}
_________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RpgTutorial
{
class Monster
{
public int CurrentHealth, MaxHealth, CurrentMagic;
public int M*****ic, Strength, Defense, Agility;
public int Experience, Gold, AttackDamage;
public string Identifier;
public bool isAlive;
public Monster()
{
CurrentHealth = 8;
MaxHealth = 8;
CurrentMagic = 0;
M*****ic = 0;
Strength = 5;
Defense = 3;
Agility = 4;
Experience = 5;
Gold = 2;
Identifier = "Monster";
isAlive = true;
AttackDamage = Strength;
}
}
}
__________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RpgTutorial
{
class Hero
{
public int CurrentHealth, MaxHealth, CurrentMagic;
public int M*****ic, Strength, Defense, Agility;
public int Experience, Gold, AttackDamage;
public string Identifier;
public bool isAlive;
public Hero()
{
}
public static void Initialize(Hero hero)
{
hero.CurrentHealth = 18;
hero.MaxHealth = 18;
hero.CurrentMagic = 8;
hero.M*****ic = 8;
hero.Strength = 10;
hero.Defense = 3;
hero.Agility = 6;
hero.Experience = 0;
hero.Gold = 0;
Console.WriteLine("What is your Hero's name?");
hero.Identifier = Console.ReadLine();
hero.isAlive = true;
hero.AttackDamage = hero.Strength;
}
}
}
_____________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RpgTutorial
{
class Battle
{
string userchoice;
int damage;
Random rand;
public Battle(Hero hero, Monster monster)
{
Console.WriteLine("{0} is facing a {1}.", hero.Identifier, monster.Identifier);
BattleLoop(hero, monster);
}
public void BattleLoop(Hero hero, Monster monster)
{
do
{
PrintStatus(hero, monster);
userchoice = PrintChoice();
Console.WriteLine();
ProcessChoice(userchoice, hero, monster);
Console.ReadLine();
Console.Clear();//This clears our screen so the next turn is a fresh screen
}
while (hero.isAlive == true && monster.isAlive == true);
}
void PrintStatus(Hero hero, Monster monster)
{
Console.Write(@"
********************************
HP/MaxHP MP/MaxMP
{0}: {1}/{2}hp {3}/{4}mp
{5}: {6}/{7}hp {8}/{9}mp
********************************
", hero.Identifier, hero.CurrentHealth, hero.MaxHealth, hero.CurrentMagic, hero.M*****ic,
monster.Identifier, monster.CurrentHealth, monster.MaxHealth, monster.CurrentMagic, monster.M*****ic);
}
}
}
______________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RpgTutorial
{
class Program
{
static void Main(string[] args)
{
MainGame maingame = new MainGame();
}
}
}
___________________________________________________
and lastly these are all the errors i am currently recieving
Error 2 The name 'damage' does not exist in the current context
C:\Users\Randomized\Documents\Visual Studio 2010\Projects\Restarted\ProcessChoice.cs
19 46 Restarted
Error 3 The name 'damage' does not exist in the current context
C:\Users\Randomized\Documents\Visual Studio 2010\Projects\Restarted\ProcessChoice.cs
21 64 Restarted
Error 8 The name 'damage' does not exist in the current context
C:\Users\Randomized\Documents\Visual Studio 2010\Projects\Restarted\Damage.cs
26 13 Restarted
Error 10 The name 'damage' does not exist in the current context
C:\Users\Randomized\Documents\Visual Studio 2010\Projects\Restarted\Damage.cs
27 20 Restarted
Error 1 The name 'DealDamage' does not exist in the current context
C:\Users\Randomized\Documents\Visual Studio 2010\Projects\Restarted\ProcessChoice.cs
18 21 Restarted
Error 4 The name 'PrintChoice' does not exist in the current context
C:\Users\Randomized\Documents\Visual Studio 2010\Projects\Restarted\Battle.cs
26 30 Restarted
Error 6 The name 'PrintChoice' does not exist in the current context
C:\Users\Randomized\Documents\Visual Studio 2010\Projects\Restarted\ProcessChoice.cs
34 30 Restarted
Error 5 The name 'ProcessChoice' does not exist in the current context
C:\Users\Randomized\Documents\Visual Studio 2010\Projects\Restarted\Battle.cs
28 17 Restarted
Error 7 The name 'rand' does not exist in the current context
C:\Users\Randomized\Documents\Visual Studio 2010\Projects\Restarted\Damage.cs
14 13 Restarted
Error 9 The name 'rand' does not exist in the current context
C:\Users\Randomized\Documents\Visual Studio 2010\Projects\Restarted\Damage.cs
26 22 Restarted
____________________________________________________
Thats it if anyone notices anything else that needs fixing tell me and i will fix it right away
Reply
Last edited by Marc G; June 8th, 2012 at 06:53 AM.
Reason: added code tags
-
June 7th, 2012, 11:16 PM
#2
Re: Game problem
Originally Posted by Randomized
Hey im a new C# learner and im currently following a guide on a website.
. . .
Thats it if anyone notices anything else that needs fixing tell me and i will fix it right away
I do notice that this is VC++ forum.
Besides, when you ultimately in C# forum, please use [code][/code] tags.
Last edited by Igor Vartanov; June 7th, 2012 at 11:22 PM.
Best regards,
Igor
-
June 8th, 2012, 06:53 AM
#3
-
June 8th, 2012, 05:04 PM
#4
Re: Game problem
Where is MainGame()?
Is hero.M*****ic
supposed to be: hero.Magic?
-
June 8th, 2012, 06:01 PM
#5
Re: Game problem
Anyways, I went through it real quick, it definitely needs more optimizing, I would think about that before you get too far or it will get confusing. I combined the choice classes, I didn't see a point to split those, they both have something to do about a "choice." You really aren't passing many things between classes, so I just initialized one so you can use its Methods/Functions. This code returns no errors, and as I said, mentioned beginning of my first reply, had no idea what the asterisks were and I am wondering if you forgot to include another class, MainGame()?
Battle.cs -
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RpgTutorial
{
class Battle
{
string userchoice;
int damage;
Random rand; //??? What is this doing here? Random Battle encounter?
public Battle(Hero hero, Monster monster)
{
Console.WriteLine("{0} is facing a {1}.", hero.Identifier, monster.Identifier);
BattleLoop(hero, monster);
}
public void BattleLoop(Hero hero, Monster monster)
{
Choices c = new Choices();
do
{
PrintStatus(hero, monster);
userchoice = c.PrintChoice();
Console.WriteLine();
c.ProcessChoice(userchoice, hero, monster);
Console.ReadLine();
Console.Clear();//This clears our screen so the next turn is a fresh screen
}
while (hero.isAlive == true && monster.isAlive == true);
}
void PrintStatus(Hero hero, Monster monster)
{
Console.Write(@"
********************************
HP/MaxHP MP/MaxMP
{0}: {1}/{2}hp {3}/{4}mp
{5}: {6}/{7}hp {8}/{9}mp
********************************
", hero.Identifier, hero.CurrentHealth, hero.MaxHealth, hero.CurrentMagic, hero.Magic,
monster.Identifier, monster.CurrentHealth, monster.MaxHealth, monster.CurrentMagic, monster.Magic);
}
}
}
Choices.cs -
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RpgTutorial
{
private class Choices
{
public void ProcessChoice(string choice, Hero hero, Monster monster)
{
Damage d = new Damage();
int _dmg = 0;
switch (choice.ToLower())
{
case "a":
Console.WriteLine();
Console.WriteLine("{0} attacks!", hero.Identifier);//print what it is
_dmg = d.DealDamage(hero, monster);//send it to our damage method
monster.CurrentHealth -= _dmg;//take it from the monster
Console.WriteLine("{0} hits the {1} for {2}hp of damage"
, hero.Identifier, monster.Identifier, _dmg);//show result
break;
case "d":
Console.WriteLine();
Console.WriteLine("{0} defends!", hero.Identifier);
//we will make defending more beneficial later
//Many times you will see this shown like below:
//To Do: Add logic to make defending beneficial
break;
default:
Console.WriteLine("I'm sorry, I didn't recognize that.");
Console.WriteLine();//if the choice isn't valid
choice = PrintChoice();//the we'll keep asking till
Console.WriteLine();//they get it right
ProcessChoice(choice, hero, monster);
break;
}
}
public string PrintChoice()
{
string choice;
Console.WriteLine();
Console.Write(@"
_____________________
Please choose an action:
(A)ttack:
(D)efend:
_____________________");
Console.WriteLine();
choice = Console.ReadLine();
return choice;
}
}
}
Damage.cs -
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RpgTutorial
{
private class Damage
{
public int DealDamage(Hero hero, Monster monster)
{
Random rand = new Random();
int max;
int min;
int finalDmg = 0;
max = hero.AttackDamage - monster.Defense;
if (max <= 0)
{
max = 1;//right now this isn't a concern, but this is a bit
//of future proofing our damage mechanism
}
min = (int)(hero.AttackDamage * .8) - monster.Defense;//**I'll explain this one**
if (min <= 0)
{
min = 1;
}
finalDmg = rand.Next(min, max);//calculate the damage
return finalDmg;//send it on back
}
bool CheckHealth(int health)
{
bool alive;
if (health > 0)
alive = true;
else
alive = false;
return alive;
}
}
}
-
June 11th, 2012, 03:01 PM
#6
Re: Game problem
Just a nitpick because I see this so often. This method:
Code:
bool CheckHealth(int health)
{
bool alive;
if (health > 0)
alive = true;
else
alive = false;
return alive;
}
Could really just be
Code:
bool CheckHealth(int health)
{
return health > 0;
}
Booleans are a type too . Going further, the method name doesn't make it obvious as to what the meaning of its return value is. I would prefer something like an IsDead() method on a character class. This could then take into account states and whatnot.
Also, for min/maxes, you can shorten that code as well, something like
Code:
max = Math.Max(hero.AttackDamage - monster.Defense, 1);
Also, why is the min damage defined to be 1? Why is 0 invalid? You could also benefit by using unsigned numbers for thing like health as they should never be negative. Finally, it may be a bad idea to initialize a new Random object each time the method is called. If it is called frequently enough you will end up using the same pattern of numbers as the seed will be the same (it uses system time, which has a smaller resolution than the speed at which you can call a method).
Last edited by BigEd781; June 11th, 2012 at 03:09 PM.
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Yes; I have a blog too - http://the-angry-gorilla.com/
-
June 11th, 2012, 05:49 PM
#7
Re: Game problem
Originally Posted by BigEd781
Finally, it may be a bad idea to initialize a new Random object each time the method is called. If it is called frequently enough you will end up using the same pattern of numbers as the seed will be the same (it uses system time, which has a smaller resolution than the speed at which you can call a method).
Good point, so just keep Random alive and pass it to the method? or add sleep time?
-
June 11th, 2012, 08:24 PM
#8
Re: Game problem
You can make it an instance level variable.
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Yes; I have a blog too - http://the-angry-gorilla.com/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|