What is WRONG with this code?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: What is WRONG with this code?

Hybrid View

  1. #1
    Join Date
    Jun 2011
    Posts
    33

    What is WRONG with this code?

    I've been working on the source code for an RPG game. This class I have here is the MainWorld class, a class designated to control movement in the game. So far, I've gone from about 70 to 13 compile errors. A few of them are syntax errors. The main ones are telling me that my type or namespace names are not found, even though I imported them into my code.

    I'm also getting a message telling me I can't convert from int to string with my random functions. What's the fix for this?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace RpgTutorial
    {
        class MainWorld
        {
            List<Character> Monster;
            DataHandler data = new DataHandler();
            Hero myhero;
    
            public void Mainworld(Hero hero, List<Character> monsters)
            {
                Console.Write(@"
    You are in a valley, not far from the town of Zalgalax.
    Where do you wish to travel?
    _____________________________
    (NW) (N) (NE)
    (w)       (E)
    (SW) (S) (SE)
       (Exit)
    _____________________________
    ");
                Console.ReadLine();
                choice = Console.ReadLine();
                switch (answer)
                {
                    case "NW":
                    case "nw":
                    case "Nw":
                        Northwest northwest = new Northwest(myhero);
                    case "N":
                    case "n":
                        North north = new North(myhero);
                    case "NE":
                    case "ne":
                    case "Ne":
                        Northeast northeast = new northeast(myhero);
                    //NE class will be added later
                    default:
                        Console.WriteLine("I'm sorry, I didn't understand that.");
                        return;
    
                }
    
                int battlechance;
                int setnumber;
                setnumber = 1;
                rand = new Random();
                battlechance = Random.Next(1, 100);
    
                if (battlechance >= setnumber)
                {
                    int randtwo;
                    gibblychance = 10;
                    string monsterchance;
                    Console.WriteLine("Suddenly, a wild monster appears!");
                    monsterchance = randtwo.Next(1, 60);
    
                    if (monsterchance =  1);
                    {
                        Monster.Add(new Zombie());
                        Monster.Add(new Zombie());
                        Monster.Add(new Zombie());
                    }
    
                    if (monsterchance = 2);
                    {
                        Monster.Add(new Borg());
                    }
    
                    if (monsterchance = 3);
                    {
                        Monster.Add(new Garryxx());
                    }
    
                    if (monsterchance = 4);
                    {
                        Console.WriteLine("You've been attacked by a tribal raiding party!");
                        Monster.Add(new Barbarian());
                        Monster.Add(new Barbarian());
                        Monster.Add(new Mage());
                    }
    
                    if (monsterchance = 5);
                    {
                        Monster.Add(new Slime());
                    }
    
                    if (monsterchance = 6);
                    {
                        Console.WriteLine("A vicious warrior of Gorox appears, a Gorox Paladin!");
                        Monster.Add(new GoroxPaladin());
                    }
    
                    Battle battle = new Battle(myhero, Monster);
                }
            }
        }
    }
    I also have the full source code if you want to see the other classes.

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,835

    Re: What is WRONG with this code?

    string monsterchance;
    This should be defined as an int not a string
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Nov 2004
    Posts
    105

    Re: What is WRONG with this code?

    few things I found

    rand = new Random(); // I guiess rand instance you crated properly
    battlechance = Random.Next(1, 100); this should be rand not Random
    randtwo should of type Random not int

    Monster is list of "Character" you are trying to add Zombie, Brog, Garryxxx - If these classes are not derived from Character do that also.

    If(...) will not be ended with ";"
    if - equla condition is ==
    etc..

    Regards
    Ravi
    Ravi.Battula

  4. #4
    Join Date
    Nov 2004
    Posts
    105

    Re: What is WRONG with this code?

    One more thing
    If you use more than one case statement, these case statements should end with break or continue or goto etc.
    ex:
    case "NW":
    case "nw":
    case "Nw":
    Northwest northwest = new Northwest(myhero);
    break;

    Regards
    Ravi
    Ravi.Battula

  5. #5
    Join Date
    Jul 2005
    Location
    Louisville, KY
    Posts
    201

    Re: What is WRONG with this code?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace RpgTutorial
    {
        class MainWorld
        {
            List<Character> Monster;                                 // Zombie? Hmm. What's this type?
            DataHandler data = new DataHandler();
            Hero myhero;
            string choice;
    
            public void Mainworld(Hero hero, List<Character> monsters)
            {
                Console.Write(@"You are in a valley, not far from the town of Zalgalax.
                                            Where do you wish to travel?
                                            _____________________________
                                           (NW) (N) (NE)
                                           (W)            (E)
                                           (SW) (S) (SE)
                                           (Exit)
                                            _____________________________
                                           ");
    
                Console.ReadLine();
                choice = Console.ReadLine();
                choice = choice.ToUpper();
    
                switch (choice)
                {
                    case "NW":
                        Northwest northwest = new Northwest(myhero);
                        break;
                    case "N":
                        North north = new North(myhero);
                        break;
                    case "NE":
                        Northeast northeast = new Northeast(myhero);
                        break;
                    default:
                        Console.WriteLine("I'm sorry, I didn't understand that.");
                        return;
    
                }
    
                int setnumber = 1;
                Random rand = new Random();
                int battlechance = rand.Next(1, 100);
    
                if (battlechance >= setnumber)
                {
                    Random randtwo = new Random();
                    int gibblychance = 10;
                    int monsterchance = randtwo.Next(1, 60);
                    Console.WriteLine("Suddenly, a wild monster appears!");
    
                    if (monsterchance ==  1)
                    {
                        Monster.Add(new Zombie());
                        Monster.Add(new Zombie());
                        Monster.Add(new Zombie());
                    }
    
                    if (monsterchance == 2)
                    {
                        Monster.Add(new Borg());
                    }
    
                    if (monsterchance == 3)
                    {
                        Monster.Add(new Garryxx());
                    }
    
                    if (monsterchance == 4)
                    {
                        Console.WriteLine("You've been attacked by a tribal raiding party!");
                        Monster.Add(new Barbarian());
                        Monster.Add(new Barbarian());
                        Monster.Add(new Mage());
                    }
    
                    if (monsterchance == 5)
                    {
                        Monster.Add(new Slime());
                    }
    
                    if (monsterchance == 6)
                    {
                        Console.WriteLine("A vicious warrior of Gorox appears, a Gorox Paladin!");
                        Monster.Add(new GoroxPaladin());
                    }
    
                    Battle battle = new Battle(myhero, Monster);
                }
            }
        }
    }
    I think most are fixed.

    Hope it helps!

    Regards,
    Quinn

    If this post helps, please rate this post!
    Last edited by QuinnJohns; October 14th, 2011 at 01:41 PM. Reason: Wow, good save TGC.

  6. #6
    Join Date
    Jul 2005
    Location
    Louisville, KY
    Posts
    201

    Re: What is WRONG with this code?

    Quote Originally Posted by Krunchyman View Post
    I've been working on the source code for an RPG game. This class I have here is the MainWorld class, a class designated to control movement in the game. So far, I've gone from about 70 to 13 compile errors. A few of them are syntax errors. The main ones are telling me that my type or namespace names are not found, even though I imported them into my code.

    I'm also getting a message telling me I can't convert from int to string with my random functions. What's the fix for this?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace RpgTutorial
    {
        class MainWorld
        {
            List<Character> Monster;
            DataHandler data = new DataHandler();
            Hero myhero;
    
            public void Mainworld(Hero hero, List<Character> monsters)
            {
                Console.Write(@"
    You are in a valley, not far from the town of Zalgalax.
    Where do you wish to travel?
    _____________________________
    (NW) (N) (NE)
    (w)       (E)
    (SW) (S) (SE)
       (Exit)
    _____________________________
    ");
                Console.ReadLine();
                choice = Console.ReadLine();
                switch (answer)
                {
                    case "NW":
                    case "nw":
                    case "Nw":
                        Northwest northwest = new Northwest(myhero);
                    case "N":
                    case "n":
                        North north = new North(myhero);
                    case "NE":
                    case "ne":
                    case "Ne":
                        Northeast northeast = new northeast(myhero);
                    //NE class will be added later
                    default:
                        Console.WriteLine("I'm sorry, I didn't understand that.");
                        return;
    
                }
    
                int battlechance;
                int setnumber;
                setnumber = 1;
                rand = new Random();
                battlechance = Random.Next(1, 100);
    
                if (battlechance >= setnumber)
                {
                    int randtwo;
                    gibblychance = 10;
                    string monsterchance;
                    Console.WriteLine("Suddenly, a wild monster appears!");
                    monsterchance = randtwo.Next(1, 60);
    
                    if (monsterchance =  1);
                    {
                        Monster.Add(new Zombie());
                        Monster.Add(new Zombie());
                        Monster.Add(new Zombie());
                    }
    
                    if (monsterchance = 2);
                    {
                        Monster.Add(new Borg());
                    }
    
                    if (monsterchance = 3);
                    {
                        Monster.Add(new Garryxx());
                    }
    
                    if (monsterchance = 4);
                    {
                        Console.WriteLine("You've been attacked by a tribal raiding party!");
                        Monster.Add(new Barbarian());
                        Monster.Add(new Barbarian());
                        Monster.Add(new Mage());
                    }
    
                    if (monsterchance = 5);
                    {
                        Monster.Add(new Slime());
                    }
    
                    if (monsterchance = 6);
                    {
                        Console.WriteLine("A vicious warrior of Gorox appears, a Gorox Paladin!");
                        Monster.Add(new GoroxPaladin());
                    }
    
                    Battle battle = new Battle(myhero, Monster);
                }
            }
        }
    }
    I also have the full source code if you want to see the other classes.
    Shouldn't your comparisons also be "==" ?

  7. #7
    Join Date
    Jan 2010
    Posts
    1,099

    Re: What is WRONG with this code?

    Except for the ';' at the if-statements - it should be removed. For example:
    Code:
                    if (monsterchance == 3);     //  <---- (here)
                    {
                        Monster.Add(new Garryxx());
                    }
    Anyway, Krunchyman, go throughout all the post and correct the errors they found. There's a whole bunch of mistakes here that clearly show you really need to read up on C# basics - and honestly, that's the best advice I can give you; there's simply no other way to start coding.

  8. #8
    Join Date
    Jul 2005
    Location
    Louisville, KY
    Posts
    201

    Re: What is WRONG with this code?

    Quote Originally Posted by TheGreatCthulhu View Post
    Except for the ';' at the if-statements - it should be removed. For example:
    Code:
                    if (monsterchance == 3);     //  <---- (here)
                    {
                        Monster.Add(new Garryxx());
                    }
    Anyway, Krunchyman, go throughout all the post and correct the errors they found. There's a whole bunch of mistakes here that clearly show you really need to read up on C# basics - and honestly, that's the best advice I can give you; there's simply no other way to start coding.
    wow, good save. I can't believe I missed that.

  9. #9
    Join Date
    Jun 2011
    Posts
    33

    Re: What is WRONG with this code?

    Ok, thanks guys. I should have all the help I need now.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center