CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2011
    Posts
    33

    Post RpgTutorial.MainWorld does not contain a constructor that takes 1 arguments

    I'm having some issues with arguments, particularly this class. For this class, I get 4 errors that tell me " RpgTutorial does not contain a constructor that takes 1 argument.

    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;
            string choice;
    
            public void Mainworld(Hero hero, List<Character> monsters)
            {
                do
                {
                    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);
    
                    }
                }
                while (choice != "E" && choice != "e");
                Console.WriteLine("Press enter to continue....");
                Console.ReadLine();
            }
        }
    }


    This is my maingame class, another class that gives me an error, saying that "Mainworld does not contain a constructor that takes one arguments".

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace RpgTutorial
    {
        class MainGame
        {
            List<Character> Monster;
            DataHandler data = new DataHandler();
            Hero myhero;
            Battle battle;
            string answer;
    
            public MainGame()
            {
                Console.WriteLine("Welcome to the world of Zalgalax!");
                myhero = new Hero();
                Hero.Initialize(myhero);
                Monster = new List<Character>();
                BasicGameLoop();
            }
            void BasicGameLoop()
            {
                do
                {
                    Console.WriteLine();
                    Console.Write(@"
    What would you like to do?
    _____________________________
    (W)orld
    (F)ight
    S(T)ore
    (I)nn
    (V)iew
    (L)oad
    (S)ave
    (Q)uit
    _____________________________
    F,S,I,V,L,A or Q?");
                    Console.WriteLine();
                    answer = Console.ReadLine();
                    Console.WriteLine();
                    switch (answer)
                    {
                        case "W":
                        case "w":
                            MainWorld mainworld = new MainWorld(myhero);
                            break;
                        case "L":
                        case "l":
                            data.Load(myhero);
                            break;
                        case "S":
                        case "s":
                            data.Save(myhero);
                            break;
                        case "T":
                        case "t":
                            Store store = new Store(myhero);
                            break;
                        case "I":
                        case "i":
                            Inn.Sleep(myhero);
                            break;
                        case "v":
                        case "V":
                            View.PrintStats(myhero);
                            break;
                        case "F":
                        case "f":
                            string done = "";
                            do
                            {
                                Console.Write(@"
    Which monster do you want to fight?
    (S)lime:
    (Borg):
    (B)arbarian:
    (M)age:
    (Z)ombie:
    (G)arryxx:
    _________________________");
    
                                Console.WriteLine();
                                string choice = Console.ReadLine();
                                
                                if (choice == "g" || choice == "G")
                                {
                                    Monster.Add(new Garryxx());
                                }
    
                                if (choice == "Borg" || choice == "borg")
                                {
                                    Monster.Add(new Borg());
                                }
    
                                if (choice == "S" || choice == "s")
                                {
                                    Monster.Add(new Slime());
                                }
    
                                else if (choice == "B" || choice == "b")
                                {
                                    Monster.Add(new Barbarian());
                                }
                                else if (choice == "Sharkkill" || choice == "sharkkill")
                                {
                                    Monster.Add(new Sharkkill());
                                }
    
                                else if (choice == "M" || choice == "m")
                                {
                                    Monster.Add(new Mage());
                                }
                                else if (choice == "Z" || choice == "z")
                                {
                                    Monster.Add(new Zombie());
                                }
    
                                else
                                {
                                    
                                }
                                Console.WriteLine("Would you like to fight more monsters?");
                                Console.WriteLine();
                                done = Console.ReadLine();
                            }
                            while (done == "Y" || done == "y");
                            battle = new Battle(myhero, Monster);
    
                            if (myhero.CurrentHealth <= 0)
                            {
                                Console.WriteLine("Your game is over!");
                                continue;
                            }
                            else if (myhero.fled == false)
                            {
                                int gold = 0;
                                int experience = 0;
                                foreach (Character monster in Monster)
                                {
                                    if (monster.fled == false)
                                    {
                                        experience += monster.Experience;
                                        gold += monster.Gold;
                                    }
                                }
                                Console.WriteLine("{0} gets {1} gold and {2} experience"
                                    , myhero.Identifier, gold, experience);
                                myhero.Experience += experience;
                                myhero.Gold += gold;
                                Monster.Clear();
                                Console.WriteLine("Press enter to continue....");
                                Console.ReadLine();
                            }
                            else
                            {
                                myhero.fled = false;
                            }
                            break;
                        case "Q":
                        case "q":
                            Console.WriteLine("Goodbye {0}", myhero.Identifier);
                            break;
                    }
                }
                while (answer != "Q" && answer != "q");
            }
        }
    }
    I'm trying to make it so that when the user selects case "w" in the MainGame class, he is taken to the MainWorld class where he can choose to go to the North, Northeast and Northwest classes, and randomly enters a monster battle.

    What am I doing wrong? Am I missing brackets? Is there a broken loop here? The code won't compile properly with these constructor errors getting in the way.

  2. #2
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: RpgTutorial.MainWorld does not contain a constructor that takes 1 arguments

    This line
    Code:
    MainWorld mainworld = new MainWorld(myhero);
    Method:
    Code:
    public void Mainworld(Hero hero, List<Character> monsters)
    As you can see from your code that method contains 2 arguments so you must provide both when you create a new instance of it.
    Code:
    MainWorld mainwolrd = new MainWorld(myhero, mosterList);

    You could also create another method named the same with less or more arguments in it.

    For example:
    Code:
    public void Mainworld()
    {
        // Basic method no arguments
    }
    
    public void Mainworld(Hero hero)
    {
        // Includes hero
    }
    
    public void Mainworld(List<Character> monsters)
    {
        // Includes monster list
    }
    
    public void Mainworld(Hero hero, List<Character> monsters)
    {
        // Includes hero and monster list
    }
    
    public void Mainworld(Hero hero, List<Character> monsters, List<Character> npcs)
    {
        // Includes hero, monster list and an NPCs list
    }
    Then whenever you start a new instance of Mainworld you would have 5 options available to use.

    You don't have to do all that but I hope you get an idea of what I'm talking about.

  3. #3
    Join Date
    Jun 2011
    Posts
    33

    Re: RpgTutorial.MainWorld does not contain a constructor that takes 1 arguments

    Quote Originally Posted by Ubiquitous View Post
    This line
    Code:
    MainWorld mainworld = new MainWorld(myhero);
    Method:
    Code:
    public void Mainworld(Hero hero, List<Character> monsters)
    As you can see from your code that method contains 2 arguments so you must provide both when you create a new instance of it.
    Code:
    MainWorld mainwolrd = new MainWorld(myhero, mosterList);

    You could also create another method named the same with less or more arguments in it.

    For example:
    Code:
    public void Mainworld()
    {
        // Basic method no arguments
    }
    
    public void Mainworld(Hero hero)
    {
        // Includes hero
    }
    
    public void Mainworld(List<Character> monsters)
    {
        // Includes monster list
    }
    
    public void Mainworld(Hero hero, List<Character> monsters)
    {
        // Includes hero and monster list
    }
    
    public void Mainworld(Hero hero, List<Character> monsters, List<Character> npcs)
    {
        // Includes hero, monster list and an NPCs list
    }
    Then whenever you start a new instance of Mainworld you would have 5 options available to use.

    You don't have to do all that but I hope you get an idea of what I'm talking about.
    I believe I do. Thanks for your help.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured