简体   繁体   中英

C# Using other class method variables in Main

I try to make a simple text based game and just finished to write a code of character creation menu in Main. Now i decide to make library with separate class Player and make the CharacterCreation method there with this code. The problem is - i dont understand how to make Player.CharCreation method variables(like player name and attributes) now visible in Main programm to use them after.

public class Player
{
    public static void CharCreation()
    {
        Console.WriteLine("\t=== Character creation menu ===\n");
        Console.Write("Write the name and press Enter: ");
        string name = Console.ReadLine();
        string Again;
        string RaceChoise;
        Console.Write("\nChoose your race(print number and press Enter):\n 1.Human\n 2.Dwarf\n 3.Elf\n");

        // Race choise

        do
        {
            Again = "No";
            RaceChoise = Console.ReadLine();
            switch (RaceChoise)
            {
                case "1":
                    Console.WriteLine("\nYou was born as human (Charisma +1)\n");
                    RaceChoise = "Human";
                    break;
                case "2":
                    Console.WriteLine("\nYou was born as dwarf(Constitution +1)\n");
                    RaceChoise = "Dwarf";
                    break;
                case "3":
                    Console.WriteLine("\nYou was born as elf (Agility +1)\n");
                    RaceChoise = "Elf";
                    break;
                default:
                    Console.WriteLine("Wrong number");
                    Again = RaceChoise;
                    break;
            }
        } while (Again != "No");

        // Base attributes according to the race choise

        Console.WriteLine("Character attributes\n");
        int Str = 1;
        int Agi = 1;
        int Int = 1;
        int Con = 1;
        int Cha = 1;
        Agi = RaceChoise == "Elf" ? (Agi + 1) : (Agi);
        Con = RaceChoise == "Dwarf" ? (Con + 1) : (Con);
        Cha = RaceChoise == "Human" ? (Cha + 1) : (Cha);
        Console.Write($"Strengh: {Str}\nAgility: {Agi}\nIntelligence: {Int}\nConstitution: {Con}\nCharisma: {Cha}\n\n");
        Console.WriteLine("Above is your character base attributes. Now distribute 5 points more");

        // Attributes points destribution

        int Counter = 5;
        for (int i = 0; i < 1; i++)
        {
            Console.Write("To strengh: ");
            int StrChange = Convert.ToInt32(Console.ReadLine());
            if (StrChange > Counter) { StrChange = Counter; }
            Counter = Counter - StrChange;
            Str = Str + StrChange;
            if (Counter == 0)
            {
                break;
            }
            else
            {
                Console.WriteLine($"Points remains: {Counter}");
                Console.Write("To agility: ");
                int AgiChange = Convert.ToInt32(Console.ReadLine());
                if (AgiChange > Counter) { AgiChange = Counter; }
                Counter = Counter - AgiChange;
                Agi = Agi + AgiChange;
                if (Counter == 0)
                {
                    break;
                }
                else
                {
                    Console.WriteLine($"Points remains: {Counter}");
                    Console.Write("To intelligence: ");
                    int IntChange = Convert.ToInt32(Console.ReadLine());
                    if (IntChange > Counter) { IntChange = Counter; }
                    Counter = Counter - IntChange;
                    Int = Int + IntChange;
                    if (Counter == 0)
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine($"Points remains: {Counter}");
                        Console.Write("To constitution: ");
                        int ConChange = Convert.ToInt32(Console.ReadLine());
                        if (ConChange > Counter) { ConChange = Counter; }
                        Counter = Counter - ConChange;
                        Con = Con + ConChange;
                        if (Counter == 0)
                        {
                            break;
                        }
                        else
                        {
                            Console.WriteLine($"Points remains: {Counter}");
                            Console.Write("To charisma: ");
                            int ChaChange = Convert.ToInt32(Console.ReadLine());
                            if (ChaChange > Counter) { ChaChange = Counter; }
                            Cha = Cha + ChaChange;

Well i guess its might be some kind of constructor but tryed a several options and its not working. My code looks like clumsy but i learning C# for a month only so dont judge strictly:)

public class Player
{
    public string Name { get; set; }
    public string Race { get; set; }

    public static Player CharCreation()
    {
        // do your stuff...
        string name = "Player";
        string race = "Human";
        return new Player()
        {
            Name = name,
            Race = race,
        };
    }
}

class Program
{
    static void Main()
    {
        Player player = Player.CharCreation();
        // do something with player.Name or player.Race
    }
}

I try to make a simple text based game and just finished to write a code of character creation menu in Main. Now i decide to make library with separate class Player and make the CharacterCreation method there with this code. The problem is - i dont understand how to make Player.CharCreation method variables(like player name and attributes) now visible in Main programm to use them after.

public class Player
{
    public static void CharCreation()
    {
        Console.WriteLine("\t=== Character creation menu ===\n");
        Console.Write("Write the name and press Enter: ");
        string name = Console.ReadLine();
        string Again;
        string RaceChoise;
        Console.Write("\nChoose your race(print number and press Enter):\n 1.Human\n 2.Dwarf\n 3.Elf\n");

        // Race choise

        do
        {
            Again = "No";
            RaceChoise = Console.ReadLine();
            switch (RaceChoise)
            {
                case "1":
                    Console.WriteLine("\nYou was born as human (Charisma +1)\n");
                    RaceChoise = "Human";
                    break;
                case "2":
                    Console.WriteLine("\nYou was born as dwarf(Constitution +1)\n");
                    RaceChoise = "Dwarf";
                    break;
                case "3":
                    Console.WriteLine("\nYou was born as elf (Agility +1)\n");
                    RaceChoise = "Elf";
                    break;
                default:
                    Console.WriteLine("Wrong number");
                    Again = RaceChoise;
                    break;
            }
        } while (Again != "No");

        // Base attributes according to the race choise

        Console.WriteLine("Character attributes\n");
        int Str = 1;
        int Agi = 1;
        int Int = 1;
        int Con = 1;
        int Cha = 1;
        Agi = RaceChoise == "Elf" ? (Agi + 1) : (Agi);
        Con = RaceChoise == "Dwarf" ? (Con + 1) : (Con);
        Cha = RaceChoise == "Human" ? (Cha + 1) : (Cha);
        Console.Write($"Strengh: {Str}\nAgility: {Agi}\nIntelligence: {Int}\nConstitution: {Con}\nCharisma: {Cha}\n\n");
        Console.WriteLine("Above is your character base attributes. Now distribute 5 points more");

        // Attributes points destribution

        int Counter = 5;
        for (int i = 0; i < 1; i++)
        {
            Console.Write("To strengh: ");
            int StrChange = Convert.ToInt32(Console.ReadLine());
            if (StrChange > Counter) { StrChange = Counter; }
            Counter = Counter - StrChange;
            Str = Str + StrChange;
            if (Counter == 0)
            {
                break;
            }
            else
            {
                Console.WriteLine($"Points remains: {Counter}");
                Console.Write("To agility: ");
                int AgiChange = Convert.ToInt32(Console.ReadLine());
                if (AgiChange > Counter) { AgiChange = Counter; }
                Counter = Counter - AgiChange;
                Agi = Agi + AgiChange;
                if (Counter == 0)
                {
                    break;
                }
                else
                {
                    Console.WriteLine($"Points remains: {Counter}");
                    Console.Write("To intelligence: ");
                    int IntChange = Convert.ToInt32(Console.ReadLine());
                    if (IntChange > Counter) { IntChange = Counter; }
                    Counter = Counter - IntChange;
                    Int = Int + IntChange;
                    if (Counter == 0)
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine($"Points remains: {Counter}");
                        Console.Write("To constitution: ");
                        int ConChange = Convert.ToInt32(Console.ReadLine());
                        if (ConChange > Counter) { ConChange = Counter; }
                        Counter = Counter - ConChange;
                        Con = Con + ConChange;
                        if (Counter == 0)
                        {
                            break;
                        }
                        else
                        {
                            Console.WriteLine($"Points remains: {Counter}");
                            Console.Write("To charisma: ");
                            int ChaChange = Convert.ToInt32(Console.ReadLine());
                            if (ChaChange > Counter) { ChaChange = Counter; }
                            Cha = Cha + ChaChange;

Well i guess its might be some kind of constructor but tryed a several options and its not working. My code looks like clumsy but i learning C# for a month only so dont judge strictly:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM