Jump to content

using list<>

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
serpant

serpant

    Newbie

  • Members
  • PipPip
  • 14 posts

namespace RpgGame

{

    class Fight

    {

    

        Battle battle;

        int monsterSELECT;

        List<Character> Monster;

 

        


        public void fight(Hero myhero)


        {

            bool KeepFighting = false;

            int NumMonster = 0, NumMonster2;

            Random rand = new Random();

            NumMonster2 = rand.Next(1, 4);

            Monster = new List<Character>();


            do

            {

                monsterSELECT = rand.Next(1, 4);



                switch (monsterSELECT)

                {

                    /*case 0:

                        Monster monster = new Monster();

                        Monster.Add = new Monster

                        battle = new Battle(myhero, monster);

                       break;*/

                    case 1:

                        //Barbarian barbarian = new Barbarian();

                        Barbarian barbarian = new Barbarian();

                       [COLOR="Red"] Monster.Add = barbarian;[/COLOR]

                        break;

                    case 2:

                        //Slime slime = new Slime();

                        [COLOR="Red"]Monster.Add = (new Slime());[/COLOR]

                        break;

                    case 3:

                        //Mage mage = new Mage();

                       [COLOR="Red"] Monster.Add = (new Mage());[/COLOR]

                        break;


                }

                NumMonster++;

            } while (KeepFighting = false && NumMonster != NumMonster2);


            battle = new Battle(myhero, Monster);


        }

    }

}


In my SWTICH i get a this error, it underlines the red part.

****Cannot assign 'Add' because it is a 'method group'*****

My program works completely different than the one in the tutorial i've been reading so it's only so helpful.


class Mage : Character

    {


        public Mage()

        {

That should give you the basic idea of what i'm trying to send to Add. If you need any other info let me know. And thanks for any help :)

Edited by serpant, 13 June 2009 - 01:02 AM.
adding info


#2
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
I don't know if this makes a difference but instead of
class Mage : Character
try
public partial class Mage : Character


#3
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
sorry to ask, but can you show us the entire code, such as the other classes you made barbarian, monster etc. and also tell us what this program is suppose to do, from my understand you are making a battle that is adding the two classes (barbarian, monster, wizard etc) and they are suppose to duke it out right?, is this RPG :) love rpg's.

Thanks

#4
serpant

serpant

    Newbie

  • Members
  • PipPip
  • 14 posts
namespace RpgGame
{

    class Mage : Character
    {

        public Mage()
        {

            base.AiAttack = 10;

            base.AiDefend = 20;

            base.AiSpell = 100;



            base.CurrentHealth = 15;

            base.MaxHealth = 15;

            base.CurrentMagic = 10;

            base.MaxMagic = 10;

            base.Strength = 5;

            base.Defense = 2;

            base.Agility = 4;

            base.Experience = 7;

            base.Gold = 8;

            base.Identifier = "Mage";

            base.isAlive = true;

            base.AttackDamage = Strength;

            base.Monster = true;


        }



        public override string AI()
        {

            string choice;

            int ainumberchoice;

            rand = new Random();

            ainumberchoice = rand.Next(1, 100);

            if (ainumberchoice < base.AiAttack)
            {

                choice = "A";

            }

            else if (ainumberchoice <= base.AiDefend && ainumberchoice >= base.AiAttack)
            {

                choice = "D";

            }

            else if (ainumberchoice < base.AiSpell && ainumberchoice > base.AiDefend)
            {

                choice = "S";

            }

            else
            {

                choice = "F";

            }

            return choice;

        }

        public override string SpellAI()
        {
            string SpellChoice = "";

            Random rand = new Random();
            int SC;
            SC = rand.Next(0, 3);
            switch (SC)
            {
                case 0:
                    SpellChoice = "F";
                    break;
                case 1:
                    SpellChoice = "M";
                    break;
                case 2:
                    SpellChoice = "H";
                    break;
            }
            return SpellChoice;
        }

    }

}

That's the mage class. Post entire thing? We upload the program somewhere for people to look at right? I think it was this forum i was reading about that's how things are done. I've been searching lots for answers, most things i find deal with using C# for web services. I'm going to try the partial things right now.

EDIT: Got error. Then original results after i switch character to public also.

Yeah i am making a rpg game, but it's only a practice one. I had like a million sweet ideas for it but i'm trying to get the basics down then starting a new game. Since i started making it before i knew all of the basics the design isn't the best for putting in certain new features. I would make it different if i made it again. And try to figure out exactly what i want before i start.

EDIT: Actually, you are supposed to fight against multiple monsters of different types. That is why i need list, to store all opponents.

Edited by serpant, 13 June 2009 - 08:11 PM.


#5
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
hello serpant, cool, if i may ask ya can you post the entire thing, i can then look at it and see what i can do to try and fix it or atleast getting it working, but one other question is what is suppose to happen in this line of code, is it suppose to be a battle of some sort?

thanks


public void fight(Hero myhero)


        {

            bool KeepFighting = false;

            int NumMonster = 0, NumMonster2;

            Random rand = new Random();

            NumMonster2 = rand.Next(1, 4);

            Monster = new List<Character>();


            do

            {

                monsterSELECT = rand.Next(1, 4);



                switch (monsterSELECT)

                {

                    /*case 0:

                        Monster monster = new Monster();

                        Monster.Add = new Monster

                        battle = new Battle(myhero, monster);

                       break;*/

                    case 1:

                        //Barbarian barbarian = new Barbarian();

                        Barbarian barbarian = new Barbarian();

                        Monster.Add = barbarian;

                        break;

                    case 2:

                        //Slime slime = new Slime();

                        Monster.Add = (new Slime());

                        break;

                    case 3:

                        //Mage mage = new Mage();

                        Monster.Add = (new Mage());

                        break;


                }

                NumMonster++;

            } while (KeepFighting = false && NumMonster != NumMonster2);


            battle = new Battle(myhero, Monster);


        }

    }



#6
serpant

serpant

    Newbie

  • Members
  • PipPip
  • 14 posts
I got like 15 or more classes total. I guess i can post the whole thing. It's gonna be rough looking through it all in the little code window. This class here selects how many monsters, and which type you fight. Be advised. This code is sloppy and poorly desgined as it is right now. Adding in the new features has messed it up bad. I just want to get it running again so i can start fixing it. This is my first game so it's been difficult. And i just started learning C# on monday so be nice :) lol. It's going well, i think ...

namespace RpgGame
{
    class Program
    {

        static void Main()
        {
            MainGame maingame = new MainGame();
        }
    }
}

namespace RpgGame
{

    class MainGame
    {

        Hero myhero;
        string answer;

        public MainGame()
        {

            myhero = new Hero();
            Hero.Initialize(myhero);  
            BasicGameLoop();

        }

        public void BasicGameLoop()
        {

            //int gold=0, xp=0;
            string action;
            Fight fight;
            Inn inn;

            do
            {
                MainMenu Menu = new MainMenu();
                action = Menu.PrintMainMenu();


                if (action == "F")
                {
                    fight = new Fight();
                    fight.fight(myhero);
                }
                if (action == "R")
                {
                    inn = new Inn();
                    inn.inn(myhero);
                }
                    
                



                answer = Console.ReadLine();

            }

            while (myhero.isAlive==true);

        }

    }

}

namespace RpgGame
{
    class MainMenu
    {

        string MenuChoice;

        public string PrintMainMenu()
        {
            Console.Clear();
            Console.Write(@"
***********************************************
R(rest/inn)    F(fight)     B(buy)      I(inventory)
************************************************");
            Console.WriteLine("");
            MenuChoice = Console.ReadLine();

            switch (MenuChoice)
            {
                case "r":
                case "R":
                    MenuChoice = "R";
                    break;
                case "f":
                case "F":
                    MenuChoice = "F";
                    break;
                case "b":
                case "B":
                    MenuChoice = "B";
                    break;
                case "i":
                case "I":
                    MenuChoice = "I";
                    break;
                default:
                    Console.WriteLine("??????????");
                    Console.ReadLine();
                    PrintMainMenu();
                    break;

            }
            return MenuChoice;
        }

    }
}


namespace RpgGame
{
    class Fight
    {
    
        Battle battle;
        int monsterSELECT;
        List<Character> Monster;
 
        

        public void fight(Hero myhero)

        {
            bool KeepFighting = false;
            int NumMonster = 0, NumMonster2;
            Random rand = new Random();
            NumMonster2 = rand.Next(1, 4);
            Monster = new List<Character>();

            do
            {
                monsterSELECT = rand.Next(1, 4);


                switch (monsterSELECT)
                {
                    /*case 0:
                        Monster monster = new Monster();
                        Monster.Add = new Monster
                        battle = new Battle(myhero, monster);
                       break;*/
                    case 1:
                        //Barbarian barbarian = new Barbarian();
                        Barbarian barbarian = new Barbarian();
                        Monster.Add = barbarian;
                        break;
                    case 2:
                        //Slime slime = new Slime();
                        Monster.Add = (new Slime());
                        break;
                    case 3:
                        //Mage mage = new Mage();
                        Monster.Add = (new Mage());
                        break;

                }
                NumMonster++;
            } while (KeepFighting = false && NumMonster != NumMonster2);

            battle = new Battle(myhero, Monster);

        }
    }
}

namespace RpgGame
{

    class Battle
    {

        int flee = 0;

        string userchoice;

        string monsterchoice;
     

        public Battle(Hero hero, List<Character> monsters)
        {

            Console.WriteLine("{0} is facing: ", hero.Identifier);

            foreach (Character monster in monsters)
            {
                Console.WriteLine("{0}, ", monster.Identifier);
            }
            BattleLoop(hero, monsters);
          
          
        }



        public void BattleLoop(Hero hero, List<Character> monsters)
        {

            bool someonetofight = true;

            do
            {
                BattleHelper battleHelper = new BattleHelper();

                battleHelper.PrintStatus(hero);

                userchoice = battleHelper.PrintChoice();

                if (userchoice == "f" || userchoice == "F")
                    continue;

                Console.WriteLine();
                if (userchoice == "a" || userchoice == "A")
                {
                    int MonsterTarget = 0;
                    MonsterTarget = Target.target(monsters);

                    battleHelper.ProcessChoice(userchoice, hero, monsters[MonsterTarget]);
                }
                if (userchoice == "d" || userchoice == "D")
                {
                    hero.defended = true;
                }
                if (userchoice == "s" || userchoice == "S")
                {
                    Spell spell = new Spell();
                    string SpellChoice = "";

                    SpellChoice = spell.SpellPrint();

                    if (SpellChoice == "F" || SpellChoice == "M")
                    {
                        foreach (Character defender in monsters)
                        {
                            battleHelper.ProcessChoice(SpellChoice,hero, defender);
                        }
                    }
                }

                

                foreach (Character monster in monsters)
                {
                    monster.isAlive = battleHelper.CheckHealth(monster.CurrentHealth);


                    if (monster.isAlive == true)
                    {

                        monsterchoice = monster.AI();

                        battleHelper.ProcessChoice(monsterchoice, monster, hero);

                    }

                }

                hero.isAlive = battleHelper.CheckHealth(hero.CurrentHealth);

                Console.ReadLine();

                Console.Clear();//This clears our screen so the next turn is a fresh screen

                someonetofight = true;
                    someonetofight = Isalive.isalive(monsters);

            } while (hero.isAlive == true && someonetofight == true);

            if (hero.isAlive == true)
                Console.WriteLine("You won!");
            if (hero.isAlive == false)
                Console.WriteLine("You Died!");

        }



        

    }

}

namespace RpgGame
{
    class BattleHelper
    {

        int damage;
        Random rand;

        public void PrintStatus(Character hero)
        {

            Console.Write(@"

********************************

         HP/MaxHP   MP/MaxMP

{0}:   {1}/{2}hp    {3}/{4}mp

********************************

", hero.Identifier, hero.CurrentHealth, hero.MaxHealth, hero.CurrentMagic, hero.MaxMagic);

        }



        public string PrintChoice()
        {

            string choice;

            Console.WriteLine();

            Console.Write(@"

_____________________

Please choose an action:

(A)ttack:

(D)efend:

(S)pell:

(F)lee:

_____________________");

            Console.WriteLine();

            choice = Console.ReadLine();

            return choice;

        }
       
        public void ProcessChoice(string choice, Character attacker, Character defender)
        {

            switch (choice)
            {

                case "A":
                case "a":

                    Console.WriteLine();
                    Console.WriteLine("{0} attacks!", attacker.Identifier);

                    DealDamage(attacker, defender);
                    defender.CurrentHealth -= damage;

                    Console.WriteLine("{0} hits the {1} for {2}hp of damage"
                        , attacker.Identifier, defender.Identifier, damage);

                    attacker.defended = false;

                    break;

                case "D":
                case "d":

                    Console.WriteLine();

                    Console.WriteLine("{0} defends!", attacker.Identifier);
                    attacker.defended = true;
  

                    break;

                case "F":
                case "f":

                    Console.WriteLine();
                    Console.WriteLine("{0} flees!", attacker.Identifier);

                    break;

                case "S":
                case "s":

                    int SpellDamage;
                    Spell spell = new Spell();
                    string SpellChoice="";

                    if (attacker.Monster == false)
                    SpellChoice = spell.SpellPrint();

                    if (attacker.Monster == true)
                    SpellChoice = attacker.SpellAI();
                       
                    SpellDamage = spell.SpellProcess(SpellChoice, attacker, defender);
                    defender.CurrentHealth -= SpellDamage;

                    break;

                default:

                    Console.WriteLine("I'm sorry, I didn't recognize that.");
                    Console.WriteLine();
                    choice = PrintChoice();
                    Console.WriteLine();
                    ProcessChoice(choice, attacker, defender);

                    break;

            }

        }

        public int DealDamage(Character attacker, Character defender)
        {

            int max;

            int min;

            rand = new Random();

            max = attacker.AttackDamage - defender.Defense;
            if (attacker.defended == true)
                max = (int)(max * 1.8);

            if (defender.defended == true)
                max /= (int) 2;

            if (max <= 0)
            {

                max = 1;

            }

            min = (int)(attacker.AttackDamage * .8) - defender.Defense;

            if (attacker.defended == true)
                min = (int)(min * 1.2);
            if (defender.defended == true)
                min = (int)(min / 2);

            if (min <= 0)
            {

                min = 1;

            }

            damage = rand.Next(min, max);//calculate the damage

            return damage;//send it on back

        }

        public bool CheckHealth(int health)
        {

            bool alive;

            if (health > 0)
            {

                alive = true;

            }

            else
            {

                alive = false;

            }

            return alive;

        }
    }
}



}

namespace RpgGame
{
    class Inn
    {
        public void inn(Hero myhero)
        {
            string answer;
            Console.Clear();
            Console.Write(@"
************************************
{0}/{1} HP     {2}/{3}MP
************************************

(R)Rest for 20 gold?
(E)Eat  for 5 gold?

*************************************",myhero.CurrentHealth,myhero.MaxHealth,myhero.CurrentMagic,myhero.MaxMagic);
            answer = Console.ReadLine();
            switch (answer) {
                case "r":
                case "R":
                    Rest(myhero);
                    break;
                case "e":
                case"E":
                    Eat(myhero);
                    break;
            }


        }

        public void Rest(Hero myhero){
            myhero.CurrentHealth = myhero.MaxHealth;
                Console.WriteLine("Fully Healed!!!");
                Console.ReadLine();
        }

            public void Eat(Hero myhero){
                myhero.CurrentHealth += 6;
                Console.WriteLine("Gain 6HP!!");
                if (myhero.CurrentHealth > myhero.MaxHealth)
                    myhero.CurrentHealth = myhero.MaxHealth;
                Console.ReadLine();
            }
        }

    }

namespace RpgGame

{

    public class Character

    {

        protected Random rand;

        protected int AiAttack, AiDefend, AiSpell;

        public int CurrentHealth, MaxHealth, CurrentMagic;

        public int MaxMagic, Strength, Defense, Agility;

        public int Experience, Gold, AttackDamage;

        public string Identifier;

        public bool isAlive, defended = false, Monster;

 

        public Character()

        {           

        }

 

        public virtual string AI()

        {

            string choice = "";

            return choice;

        }

        public virtual string SpellAI()
        {
        string SpellChoice = "";

        return SpellChoice ;
        }

    }

}
    

namespace RpgGame
{

    class Hero : Character
    {

        public Hero()
        {
        }

        public static void Initialize(Hero hero)
        {

            hero.CurrentHealth = 18;

            hero.MaxHealth = 18;

            hero.CurrentMagic = 8;

            hero.MaxMagic = 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;

            hero.Monster = false;


        }

    }

}

namespace RpgGame
{

    class Barbarian : Character
    {

        public Barbarian()
        {

            base.AiAttack = 75;

            base.AiDefend = 100;

            base.AiSpell = 0;



            base.CurrentHealth = 25;

            base.MaxHealth = 25;

            base.CurrentMagic = 0;

            base.MaxMagic = 0;

            base.Strength = 8;

            base.Defense = 3;

            base.Agility = 4;

            base.Experience = 10;

            base.Gold = 5;

            base.Identifier = "Barbarian";

            base.isAlive = true;

            base.AttackDamage = Strength;

            base.Monster = true;

        }



        public override string AI()
        {

            string choice;

            int ainumberchoice;

            rand = new Random();

            ainumberchoice = rand.Next(1, 100);

            if (ainumberchoice < base.AiAttack)
            {

                choice = "A";

            }

            else if (ainumberchoice <= base.AiDefend && ainumberchoice >= base.AiAttack)
            {

                choice = "D";

            }

            else if (ainumberchoice < base.AiSpell && ainumberchoice > base.AiDefend)
            {

                choice = "S";

            }

            else
            {

                choice = "F";

            }

            return choice;

        }

    }

}

namespace RpgGame
{

    class Slime : Character
    {

        public Slime()
        {

            base.AiAttack = 80;

            base.AiDefend = 100;

            base.AiSpell = 0;



            base.CurrentHealth = 10;

            base.MaxHealth = 10;

            base.CurrentMagic = 0;

            base.MaxMagic = 0;

            base.Strength = 3;

            base.Defense = 5;

            base.Agility = 4;

            base.Experience = 5;

            base.Gold = 3;

            base.Identifier = "Slime";

            base.isAlive = true;

            base.AttackDamage = Strength;

            base.Monster = true;

        }



        public override string AI()
        {

            string choice;

            int ainumberchoice;

            rand = new Random();

            ainumberchoice = rand.Next(1, 100);

            if (ainumberchoice < base.AiAttack)
            {

                choice = "A";

            }

            else if (ainumberchoice <= base.AiDefend && ainumberchoice >= base.AiAttack)
            {

                choice = "D";

            }

            else if (ainumberchoice < base.AiSpell && ainumberchoice > base.AiDefend)
            {

                choice = "S";

            }

            else
            {

                choice = "F";

            }

            return choice;

        }

    }

}

namespace RpgGame
{

    public partial class Mage : Character
    {

        public Mage()
        {

            base.AiAttack = 10;

            base.AiDefend = 20;

            base.AiSpell = 100;



            base.CurrentHealth = 15;

            base.MaxHealth = 15;

            base.CurrentMagic = 10;

            base.MaxMagic = 10;

            base.Strength = 5;

            base.Defense = 2;

            base.Agility = 4;

            base.Experience = 7;

            base.Gold = 8;

            base.Identifier = "Mage";

            base.isAlive = true;

            base.AttackDamage = Strength;

            base.Monster = true;


        }



        public override string AI()
        {

            string choice;

            int ainumberchoice;

            rand = new Random();

            ainumberchoice = rand.Next(1, 100);

            if (ainumberchoice < base.AiAttack)
            {

                choice = "A";

            }

            else if (ainumberchoice <= base.AiDefend && ainumberchoice >= base.AiAttack)
            {

                choice = "D";

            }

            else if (ainumberchoice < base.AiSpell && ainumberchoice > base.AiDefend)
            {

                choice = "S";

            }

            else
            {

                choice = "F";

            }

            return choice;

        }

        public override string SpellAI()
        {
            string SpellChoice = "";

            Random rand = new Random();
            int SC;
            SC = rand.Next(0, 3);
            switch (SC)
            {
                case 0:
                    SpellChoice = "F";
                    break;
                case 1:
                    SpellChoice = "M";
                    break;
                case 2:
                    SpellChoice = "H";
                    break;
            }
            return SpellChoice;
        }

    }

}

namespace RpgGame
{
    class Target
    {

        public static int target(List<Character> monsters)
        {

            int target = 0;
            string StringTarget = "";
            int count = 0;
            foreach (Character monster in monsters)
            {
                Console.WriteLine(
@"****************************
       Pick a target
******************************");
                Console.WriteLine("{0} **** {1}", monster.Identifier, count);
                count++;
            }
            StringTarget = Console.ReadLine();
            target = int.Parse(StringTarget);
            return target;

        }
    }
}


namespace RpgGame
{
    class Isalive
    {
        public static bool isalive(List<Character> monsters)
        {
            bool left = false;
            foreach (Character monster in monsters)
            {
                if (monster.CurrentHealth > 0)
                {
                    left = true;
                }
            }
            return left;
        }
        
    }
}


#7
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
instead of:
Monster.Add = barbarian;
I think you mean:
Monster.Add(barbarian);
This will add the item to the list you created called Monster. If this isn't your intention I am not sure what you are trying to do. Same goes for the rest of the Add methods as well.

#8
serpant

serpant

    Newbie

  • Members
  • PipPip
  • 14 posts
Thank you very much!! It's working! even though the monsters are kicking my ass at my own game.

Edited by serpant, 14 June 2009 - 05:35 AM.


#9
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
How the hell did I miss that :@

#10
serpant

serpant

    Newbie

  • Members
  • PipPip
  • 14 posts
Yeah, so simple. I should have looked up how to use list properly instead of trying to lookup the error code. I wonder why the material i read uses the = sign. I'm guessing its a little out of date or something?
I try not to post if i can find the answer myself and that is something i definetly could have if i used my head a little

#11
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Probably because it's not a list, or they used the index for example myList[1] = blablabla .. if that can be done with a list, or maybe they used arrays.