如何使用switch实例化一个类,然后访问并更改其属性

时间:2018-04-25 16:10:56

标签: c# oop object switch-statement

为最终项目选择你的冒险游戏,但是当我试图让玩家选择一个班级时,我遇到了一个问题。当玩家选择他们的类时,我希望switch语句实例化该类的新对象以供我访问并随后更改其属性(即在击中时降低HP或在使用药剂时提高它)。现在,在更改后,属性不会保存。

玩家类的定义:

public class Player
{
    internal int maxPlayerHealth;
    internal int currentPlayerHealth;
    internal int playerDamage;
    internal int playerDef;
    internal int playerAgility;
    internal int playerStrength;
    internal int playerExperience;

    public Player()
    {
    }

    public Player(int maxHp, int currentHp, int dmg, int def, int agil, int str, int exp)
    {
        maxPlayerHealth = maxHp;
        currentPlayerHealth = currentHp;
        playerDamage = dmg;
        playerDef = def;
        playerAgility = agil;
        playerStrength = str;
        playerExperience = exp;
    }


    private int MaxPlayerHealth { get => maxPlayerHealth;}
    private int CurrentPlayerHealth { get => currentPlayerHealth; set => currentPlayerHealth = value;}
    private int PlayerDamage { get => playerDamage;}
    private int PlayerDef { get => playerDef; }
    private int PlayerAgility { get => playerAgility; }
    private int PlayerStrength { get => playerStrength;}
    private int PlayerExperience { get => playerExperience;}

    public override string ToString()
    {
        return "Health: " + currentPlayerHealth;
    }

    public int DamageTaken(int monsterDMG)
    {
        currentPlayerHealth = currentPlayerHealth - monsterDMG;
        return currentPlayerHealth;
    }
}

让玩家选择班级的代码:

using System;
using static System.Console;

namespace _350_final_project
{
    public class Story
    {
        static Player chosenClass;

        public Player ChooseClass(string choice)
        {
            switch (choice.ToLower())
            {
                case "warrior":
                    chosenClass = new Player()
                    {
                        maxPlayerHealth = 150,
                        currentPlayerHealth = 150,
                        playerDamage = 35,
                        playerDef = 120,
                        playerStrength = 95,
                        playerAgility = 30,
                        playerExperience = 0
                    };
                    WriteLine("You have chosen the path of the Warrior. Live by the sword. Die by the sword! \n");
                    return chosenClass;

                case "theif":
                    chosenClass = new Player()
                    {
                        maxPlayerHealth = 85,
                        currentPlayerHealth = 85,
                        playerDamage = 27,
                        playerDef = 43,
                        playerStrength = 20,
                        playerAgility = 125,
                        playerExperience = 0
                    };
                    WriteLine("You have chosen to lurk in the shadows as a theif \n");
                    return chosenClass;

                case "gunner":
                    chosenClass = new Player()
                    {
                        maxPlayerHealth = 70,
                        currentPlayerHealth = 70,
                        playerDamage = 40,
                        playerDef = 75,
                        playerStrength = 60,
                        playerAgility = 110,
                        playerExperience = 0
                    };
                    WriteLine("You have chosen the way of the gunner, shoot with your mind not your eye \n");
                    return chosenClass;

                default:
                  return null;
        }
    }

    public void Beginning(string playerName, string playerClass)
    {
        WriteLine("Valiant {0}, It is time to begin your journey", playerName);
        WriteLine("We begin in the hall of heroes, the queen has tasked you with destorying the dragon \n" +
                  " that resides at the tower on the edge of the kingdom. To bein your journey pick a direction in which to go. \n" +
                  "Will you venture north, east or west?");
        string directionDecision = ReadLine();

        WriteLine("You head {0}, steadily making your way toward {1}", directionDecision, Randomize.RandomizeLocation().Name);
        WriteLine("After several long miles you come across a fork in the road with no signs pointing you to your destination. \n " +
                  "Your map is useless and due to your illpreparedness supplies are running short. Which direction will you go? \n" +
                  "North, East, or West?:");
        directionDecision = ReadLine();

        WriteLine("Setting your sights to the {0} you soldier on. As you take a look at your surroundings and you suddenly " +
                  "notice you're not alone", directionDecision);
        Encounter.StartBattle(playerClass, chosenClass);
     }
   }
}

开始战斗的代码:

using System;
using static System.Console;
using System.Threading;

namespace _350_final_project
{
    public class Encounter
    {
        private static int encounterCounter = Randomize.NumberGenerator(1, 10) % 2;

    //public static void GenerateEncounter (string playerClass,Player classChosen)
    //{
    //    if (encounterCounter == 0)
    //    {
    //        StartBattle(playerClass, classChosen);
    //    }
    //}

    public void StartBattle(string playerClass, Player chosenClass)
    {
        Monster currentMonster = Randomize.RandomizeMonster();
        string action;
        WriteLine("AMBUSH! A {0} appears to challenge you adventurer!", currentMonster.Name);

        switch (playerClass)
        {
            case "warrior":
                WriteLine("You quickly draw your greatsword and stand at the ready");
                action = "You drag your sword in a great arc!";
                EncounterBattle(currentMonster, chosenClass, action);
                break;
            case "theif":
                WriteLine("You swiftly load your gun, eyes glaring over the barrell");
                action = "You fire your weapon with extreme accuracy!";
                EncounterBattle(currentMonster, chosenClass, action);
                break;
            case "gunner":
                WriteLine("You pull both daggers from your cloak and being to toss them from palm to palm");
                action = "You dash forward, daggers a silver blur!";
                EncounterBattle(currentMonster, chosenClass, action);
                break;
            default:
                break;
        }

    }

    public void EncounterBattle(Monster monster, Player chosenClass, string action)
    {
        while (monster.CurrentHp > 0 || chosenClass.currentPlayerHealth > 0)
        {
            int monsterHitMissCounter = Randomize.NumberGenerator(1, 10) % 2;

            WriteLine("{0}{1}", monster.Name, monster.Attack);

            if (monsterHitMissCounter != 0)
            {
                WriteLine("{0}'s attack hits, you take {1} damage", monster.Name, monster.MaxDamage);

                WriteLine("Your current health is {0}", chosenClass.DamageTaken(monster.MaxDamage));
                ReadKey();
            }

            else
            {
                Thread.Sleep(3000);
                WriteLine("\n {0} missed!", monster.Name);
                ReadKey();
            }
            int playerHitMissCounter = Randomize.NumberGenerator(1, 10) % 2;
            if (playerHitMissCounter != 0)
            {
                WriteLine("Your attacked missed!");
                ReadKey();
            } else
            {
                WriteLine("You {0}, {1} takes {2} damage", action, monster.Name, chosenClass.playerDamage);

                WriteLine("{0}'s current health is {1}", monster.Name, monster.MonsterDamageTaken(chosenClass.playerDamage));
                ReadKey();
            }
         }
      }
   }
}

0 个答案:

没有答案