Home > Mobile >  I got stuck with C# while creating a player class
I got stuck with C# while creating a player class

Time:12-14

hello I coded a little player script I tried fixing it for hours and I just cant figure it out

namespace PlayerManager {
    public class Player
    {
        public string Name { get; set; }
        public int ClassNum { get; set; }
        public string ClassName { get; set; }
        public int Intel { get; set; }
        public int Agi { get; set; }
        public int Str { get; set; }
        public int Cha { get; set; }
        public int MaxHealth { get; set; }
        public int Armor { get; set; }
        public int Level { get; set; }
        public int Balance { get; set; }
        public double Health { get; set; }
        public double Exp { get; set; }

        public Player(string name, int classnum, string classname, int intel, int agi, int str, int cha, int maxhealth, int armor, int level, int balance, double health, double exp) {
            Name = name;
            ClassNum = classnum;
            ClassName = classname;
            Intel = intel;
            Agi = agi;
            Str= str;
            Cha = cha;
            MaxHealth = maxhealth;
            Armor = armor;
            Level = level;
            Balance = balance;
            Health = health;
            Exp = exp;
        }

        void Stats(){}

        void createPlayer()
        {
            var P1 = new Player(Console.ReadLine(), Convert.ToInt32(Console.ReadLine()), null, 0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0.0);
            Console.WriteLine("Enter Your Heros Name :");
            P1.Name = Console.ReadLine();
            Console.WriteLine("Choose your heros class : (1.Rouge/2.Fighter/3.Wizard)");
            P1.ClassNum = Convert.ToInt32(Console.ReadLine());

            if (P1.ClassNum == 1) 
            {
                P1.ClassName = "Rouge";
                P1.Intel = 2;
                P1.Agi = 4;
                P1.Str = 3;
                P1.Cha = 1;
                P1.MaxHealth = 12;
                P1.Armor = 2;
            }
            else if (P1.ClassNum == 2)
            {
                P1.ClassName = "Fighter";
                P1.Intel = 1;
                P1.Agi = 2;
                P1.Str = 5;
                P1.Cha = 2;
                P1.MaxHealth = 15;
                P1.Armor = 3;
            }
            else if (P1.ClassNum == 3)
            {
                P1.ClassName = "Wizard";
                P1.Intel = 4;
                P1.Agi = 2;
                P1.Str = 1;
                P1.Cha = 3;
                P1.MaxHealth = 12;
                P1.Armor = 2;
            }

            P1.Level = 0;
            P1.Balance = 0;
            P1.Health = P1.MaxHealth;
            P1.Exp = 0.0;

            Console.WriteLine("your heros name is : "   P1.Name);
            Console.WriteLine("your heros class is : "   P1.ClassName);
            Console.WriteLine("hes intel is : "   P1.Intel);
            Console.WriteLine("hes strenght is : "   P1.Str);
            Console.WriteLine("hes charisma is : "   P1.Cha);
            Console.WriteLine("he got max health of : "   P1.MaxHealth);
            Console.WriteLine("he got a armor that can block : "   P1.Armor);
            Console.WriteLine("he currently is level : "   P1.Level);
            Console.WriteLine("he got : "   P1.Balance   " gold coins");
            Console.WriteLine("and he got : "   P1.Health   " health points");
            Console.WriteLine("Expiriance : "   P1.Exp);

            void Main()
        {

            createPlayer();

        }

        }

        void takeDamage(int eDamage, int pHealth)
        {
            pHealth = pHealth - eDamage;
        }

        void giveDamage(int pDamage, int eHealth)
        {
            eHealth = eHealth - pDamage;
        }

        void healPlayer(int heal, int pHealth)
        {
            pHealth = pHealth   heal;
        }

        void healEnemy(int eHeal, int eHealth)
        {
            eHealth = eHealth   eHeal;
        }

        void gainExp(double expA, double pExp)
        {
            pExp = pExp   expA;

            Console.WriteLine(pExp   " exp Gained");
        }

        void gainLevel(int pLevel)
        {
            pLevel = pLevel   1;
        }
    }
}

there are some not needed { and } I was just playing with every problem the console showed me and I really don't know what to do any more I have a player class and its properties then some unfinished functions but the main problem the console shows me right now is that the main needs to be in the public class

CodePudding user response:

class Program
    {
        static void Main(string[] args)
        {
            Player p = new Player();
            p.createPlayer();
        }
    }

Make Methods as public & add one more default constructor

   public class Player
{

    public string Name { get; set; }

    public int ClassNum { get; set; }
    public string ClassName { get; set; }

    public int Intel { get; set; }

    public int Agi { get; set; }

    public int Str { get; set; }

    public int Cha { get; set; }

    public int MaxHealth { get; set; }

    public int Armor { get; set; }

    public int Level { get; set; }

    public int Balance { get; set; }

    public double Health { get; set; }

    public double Exp { get; set; }

    public Player()
    {

    }

    public Player(string name, int classnum, string classname, int intel, int agi, int str, int cha, int maxhealth, int armor, int level, int balance, double health, double exp)
    {
        Name = name;
        ClassNum = classnum;
        ClassName = classname;
        Intel = intel;
        Agi = agi;
        Str = str;
        Cha = cha;
        MaxHealth = maxhealth;
        Armor = armor;
        Level = level;
        Balance = balance;
        Health = health;
        Exp = exp;
    }
    public void Stats() { }
    public void createPlayer()
    {
        var P1 = new Player(Console.ReadLine(), Convert.ToInt32(Console.ReadLine()), null, 0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0.0);
        Console.WriteLine("Enter Your Heros Name :");
        P1.Name = Console.ReadLine();
        Console.WriteLine("Choose your heros class : (1.Rouge/2.Fighter/3.Wizard)");
        P1.ClassNum = Convert.ToInt32(Console.ReadLine());

        if (P1.ClassNum == 1)
        {
            P1.ClassName = "Rouge";
            P1.Intel = 2;
            P1.Agi = 4;
            P1.Str = 3;
            P1.Cha = 1;
            P1.MaxHealth = 12;
            P1.Armor = 2;
        }

        else if (P1.ClassNum == 2)
        {
            P1.ClassName = "Fighter";
            P1.Intel = 1;
            P1.Agi = 2;
            P1.Str = 5;
            P1.Cha = 2;
            P1.MaxHealth = 15;
            P1.Armor = 3;
        }

        else if (P1.ClassNum == 3)
        {
            P1.ClassName = "Wizard";
            P1.Intel = 4;
            P1.Agi = 2;
            P1.Str = 1;
            P1.Cha = 3;
            P1.MaxHealth = 12;
            P1.Armor = 2;
        }

        P1.Level = 0;
        P1.Balance = 0;
        P1.Health = P1.MaxHealth;
        P1.Exp = 0.0;

        Console.WriteLine("your heros name is : "   P1.Name);
        Console.WriteLine("your heros class is : "   P1.ClassName);
        Console.WriteLine("hes intel is : "   P1.Intel);
        Console.WriteLine("hes strenght is : "   P1.Str);
        Console.WriteLine("hes charisma is : "   P1.Cha);
        Console.WriteLine("he got max health of : "   P1.MaxHealth);
        Console.WriteLine("he got a armor that can block : "   P1.Armor);
        Console.WriteLine("he currently is level : "   P1.Level);
        Console.WriteLine("he got : "   P1.Balance   " gold coins");
        Console.WriteLine("and he got : "   P1.Health   " health points");
        Console.WriteLine("Expiriance : "   P1.Exp);

        void Main()
        {

            createPlayer();

        }

    }

    void takeDamage(int eDamage, int pHealth)
    {
        pHealth = pHealth - eDamage;
    }

    void giveDamage(int pDamage, int eHealth)
    {
        eHealth = eHealth - pDamage;
    }

    void healPlayer(int heal, int pHealth)
    {
        pHealth = pHealth   heal;
    }

    void healEnemy(int eHeal, int eHealth)
    {
        eHealth = eHealth   eHeal;

    }

    void gainExp(double expA, double pExp)
    {
        pExp = pExp   expA;

        Console.WriteLine(pExp   " exp Gained");

    }

    void gainLevel(int pLevel)
    {
        pLevel = pLevel   1;

    }
}

CodePudding user response:

problem the console shows me right now is that the main needs to be in the public class

Actually, I suspect the error message is something like

Program does not contain a static 'Main' method suitable for an entry point

Your line of code:

void Main()

Should minimally read:

static void Main()

That will mean that the program does now contain a static entry point, but you cannot call createPlayer (which is not static), from a static environment.

As written you can only call createPlayer on an existing instance of a Player, but you don't have an instance, and createPlayer creates one, so realistically, createPlayer needs to be static too to escape the chicken-and-egg situation

Note the next confusing thing you'll hit is that the very first thing createPlayer does is ReadLine(), to supply a value for the first argument of the Player( constructor, which means your program will sit there with a completely blank black window, waiting for your input. It would perhaps be better to use the pattern found further down, where you print a message, and then do a ReadLine, so the user knows what to do.

Make the line:

var P1 = new Player("", 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0.0);

So you aren't going straight to a ReadLine() and you can get some joy out of your code (as in, it runs and does something)

Then, consider carefully what you actually need that constructor to be. We use constructors to ensure that a class cannot be created without the bare minimum set of info it needs to do its work properly. From what is written it seems that the bare minimum is a name and a class, so shorten the constructor:

    public Player(string name, int classnum)
    {
        Name = name;
        ClassNum = classnum;
    }

And then rework the code to collect answers into temp variables until it has enough info to make a player:

    static void createPlayer()
    {
        
        Console.WriteLine("Enter Your Heros Name :");
        var name = Console.ReadLine();
        Console.WriteLine("Choose your heros class : (1.Rouge/2.Fighter/3.Wizard)");
        var classNum = Convert.ToInt32(Console.ReadLine());

        var p1 = new Player(name, classNum);

Next think on that it's perhaps not really the job of something outside *(I'll come back to this) the Player to set these parameters:

        if (P1.ClassNum == 1)
        {
            P1.ClassName = "Rouge";
            P1.Intel = 2;

         ...

They're things the constructor can do because it has enough info to set them up, so put those inside the constructor and then take all that createPlayer code, which doesn't just create a player but actually is the start of something that runs an entire game script, and put it outside the Player class. I would recommend to make another class called Program (it;s conventional) and put both the static void Main and the static void createPlayer into that class, and have Player as a separate thing because it better separates the concerns of the code: your player is just modelling a game character, it shouldnt be holding the entire battle script too

It means you end up with something like:

public class Player
{

    public string Name { get; set; }
    ...more props...
    public double Exp { get; set; }



    public Player(string name, int classnum)
    {
        Name = name;
        ClassNum = classnum;

        if (ClassNum == 1)
        {
            ClassName = "Rouge";
            Intel = 2;
            Agi = 4;
            Str = 3;
            Cha = 1;
            MaxHealth = 12;
            Armor = 2;
        }

        else if (ClassNum == 2)
        {
            ClassName = "Fighter";
            Intel = 1;
            Agi = 2;
            Str = 5;
            Cha = 2;
            MaxHealth = 15;
            Armor = 3;
        }

        else if (ClassNum == 3)
        {
            ClassName = "Wizard";
            Intel = 4;
            Agi = 2;
            Str = 1;
            Cha = 3;
            MaxHealth = 12;
            Armor = 2;
        }

        Level = 0;
        Balance = 0;
        Health = MaxHealth;
        Exp = 0.0;
    }

}

class Program{

    static void RunGameScript()
    {

        Console.WriteLine("Enter Your Heros Name :");
        var name = Console.ReadLine();
        Console.WriteLine("Choose your heros class : (1.Rouge/2.Fighter/3.Wizard)");
        var classNum = Convert.ToInt32(Console.ReadLine());

        var p1 = new Player(name, classNum);

        Console.WriteLine("your heros name is : "   p1.Name);
        Console.WriteLine("your heros class is : "   p1.ClassName);
        Console.WriteLine("hes intel is : "   p1.Intel);
        Console.WriteLine("hes strenght is : "   p1.Str);
        Console.WriteLine("hes charisma is : "   p1.Cha);
        Console.WriteLine("he got max health of : "   p1.MaxHealth);
        Console.WriteLine("he got a armor that can block : "   p1.Armor);
        Console.WriteLine("he currently is level : "   p1.Level);
        Console.WriteLine("he got : "   p1.Balance   " gold coins");
        Console.WriteLine("and he got : "   p1.Health   " health points");
        Console.WriteLine("Expiriance : "   p1.Exp);


    }


    static void Main()
    {

        RunGameScript();

    }
}

Next you need to attend to those methods:

enter image description here

The name looks dim because it's never used. The variable on the left hand side of the = looks dim because it's a non-op; the value is not subsequently used, and assigning a value to an int passed into a method does nothing to the int value that was supplied outside the method


Lastly, and it might seem like a really petty thing, but you'll see the importance of it the more code you read.. In C# we have particular conventions for names of things. All your methods are camelCase, but the convention is PascalCase. Your P1 local variable is init caps, but we name them with camelCase. It means your code should be like:

CreatePlayer() 
^

var p1 = new Player(...)
    ^

Avoid abbreviations too, especially on public members as other people may one day use them

  •  Tags:  
  • c#
  • Related