-4

5 つのクラスで作成したゲームを実行しようとしていますが、最も重要な部分が実行されていません。まだ c# の初心者であることを覚えておいてください。5 つのクラスはすべて別個のものですが、混乱を避けるためにここでは最初のクラスの名前空間のみを入力しました。それらは各クラスにあります。プログラムは実行されますが、Battle クラスに到達すると、BattleLoop を完全にスキップし、もう一度プレイするかどうか尋ねられます...助けてください。

ファーストクラス(私のエントリーポイント):

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");
    }
}

3 番目のクラス:

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;
    }
  }

5 番目のクラス:

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.BasicGameLoopbattle.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 に答える