-4

我正在尝试运行由 5 个类组成的游戏,但最重要的部分没有运行。请记住,我仍然是 C# 的初学者。所有 5 个类都是独立的,但我只在第一个类中键入命名空间以避免混淆,它们在每个类中都存在。该程序运行,但是当它到达战斗类时,它会完全跳过战斗循环并询问我是否想再次玩......请帮助我。

头等舱(我的入口点):

class Program
{
    static void Main(string[] args)
    {
        MainGame maingame = new MainGame();
    }
}

二等:

class MainGame
{
    Hero myhero;
    Battle battle;
    string answer; 

    public MainGame()
    {
        myhero = new Hero();
        Hero.Initialize(myhero);
        BasicGameLoop();

    }

    void BasicGameLoop()
    {
        do 
        {
            Monster monster = new Monster();
            battle = new Battle(myhero, monster);
            Console.WriteLine("Do you want to Play again??");
            answer= Console.ReadLine();
        }
        while(answer == "Y" || answer == "y");
    }
}

第三类:

class Battle
{
    string choice;
    Random rand;
    int healing, fleechance, hitchance;

    public Battle(Hero hero, Monster monster)
    {
        Console.WriteLine("{0} is facing a {1}.", hero.Identifier, monster.Identifier);
    }

    public void BattleLoop(Hero hero, Monster monster)
    {                                                                     
        do
        {
            rand = new Random();
            DisplayChoices();
            choice = Console.ReadLine();
            hitchance = rand.Next(0, 100);
            switch (choice)
            {
                case "a":
                case "A"://this way a or A work
                    hitchance = rand.Next(0, 100);
                    if (hitchance > 30)
                    {
                        hero.AttackDamage = GetHeroDamage(hero);
                        Console.WriteLine("{0} Attacks!", hero.Identifier);
                        hero.AttackDamage -= monster.Defense;
                        Console.WriteLine("The Monster Loses {0}hp", hero.AttackDamage);
                    }
                    else
                    {
                        Console.WriteLine("{0} Missed!", hero.Identifier);
                    }
                    break;
                case "d":
                case "D":
                    Console.WriteLine("{0} Defends", hero.Identifier);
                    break;
                case "h":
                case "H":
                    healing = 400;
                    hero.CurrentHealth += healing;
                    Console.WriteLine("{0} uses a Potion!", hero.Identifier);
                    Console.WriteLine("{0} heals himself for {1} Points",hero.Identifier,healing);
                    break;
                case "f":
                case "F":
                    fleechance = rand.Next(0, 100);
                    if (fleechance > 40)
                    {
                        Console.WriteLine("{0} fled!",hero.Identifier);
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    else
                    {
                        Console.WriteLine("Fleeing Failed");
                        Console.ReadLine();

                    }
                    break;
                default:
                    Console.WriteLine("Sorry that choice was invalid and the monster took a cheap shot!");
                    break;
            }

            Console.WriteLine();
            if (monster.isAlive == true)
            {
                hitchance = rand.Next(0, 100);
                if (hitchance > 30)
                {
                    monster.Strength = GetMonsterDamage(monster);
                    Console.WriteLine("The Monster Attacks!");
                    if (choice == "d" || choice == "D")
                    {
                        monster.Strength /= 2;
                    }
                    monster.Strength -= hero.Defense;
                    Console.WriteLine("The Hero loses {0}hp", monster.Strength);
                }
                else
                {
                    Console.WriteLine("The Monster Missed!");

                }
                Console.WriteLine("Press Enter to Continue");
                Console.ReadLine();
                Console.Clear();
            }
        }
        while (hero.isAlive == true && monster.isAlive == true);

        if (hero.CurrentHealth > 0)
        {
            Console.WriteLine("{0} emerged Victorious!", hero.Identifier);
        }
        else
        {
            Console.WriteLine("{0} has been defeated :(", hero.Identifier);
        }
        Console.ReadLine();
    }

    public 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.MaxMagic,
    monster.Identifier, monster.CurrentHealth, monster.MaxHealth, monster.CurrentMagic,
    monster.MaxMagic);

    }
    string DisplayChoices()
    {
        string choice;
        Console.Write(@"
  __________________________
   Please Choose an action:
   (A)ttack
   (D)efend
   (H)eal
   (F)lee
   __________________________");
        Console.WriteLine();
        choice = Console.ReadLine();
        return choice;
    }

    public int GetHeroDamage(Hero hero)// 2nd Method to calculate the hero's Damage during battle.
    {
        int attackdamage;
        attackdamage = hero.AttackDamage;
        return attackdamage;
    }
    int GetMonsterDamage(Monster monster) // 3rd Method to calculate the monster's damage during the battle.
    {
        int attackdamage;
        attackdamage = monster.Strength;
        return attackdamage;
    }

}

四级”

class Hero
{
    public int CurrentHealth, MaxHealth, CurrentMagic;
    public int MaxMagic, 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.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;
    }
  }

第五课:

class Monster
{
    public int CurrentHealth, MaxHealth, CurrentMagic;
    public int MaxMagic, Strength, Defense, Agility;
    public int Experience, Gold, AttackDamage;
    public string Identifier;
    public bool isAlive;

    public Monster()
    {
        CurrentHealth = 8;
        MaxHealth = 8;
        CurrentMagic = 0;
        MaxMagic = 0;
        Strength = 5;
        Defense = 3;
        Agility = 4;
        Experience = 5;
        Gold = 2;
        Identifier = "Monster";
        isAlive = true;
        AttackDamage = Strength;
    }
}
4

2 回答 2

5

看起来你错过了对 BattleLoop 的调用。尝试在 MainGame.BasicGameLoop 中battle.BattleLoop()的行之后添加。battle = new Battle(myhero, monster);

于 2012-07-27T14:41:09.843 回答
3
 Monster monster = new Monster();
 battle = new Battle(myhero, monster);
 battle.BattleLoop(myhero, monster);
 Console.WriteLine("Do you want to Play again??");
 answer= Console.ReadLine();
于 2012-07-27T14:42:22.740 回答