Home > other >  New coder needs help solving parameter problem for text RPG
New coder needs help solving parameter problem for text RPG

Time:08-09

I'm new to coding and am trying to make an instance of a text RPG in the console, however I'm running into a problem where I have a parameter called "hitpoints" created in my characters class but when I put it in Main the value is 0 because it is never assigned to??? and I don't know how to change it, I feel like I've tried everything I can think of. Any help would be great or if you have other tips for my code that would be helpful as well. Hopefully this isn't too long.

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Goblin
    {

        public int hitpoints;
        public int experienceGiven;
        public Goblin(int hitpoints, int experienceGiven)
        {
            hitpoints = 50;
            experienceGiven = 120;
        }
        public void gobAttack()
        {
            int enemyDamage = new Random().Next(1, 10);
            Console.WriteLine("The Goblin attacked for "   enemyDamage   " Damage");
        }




    }

    class Wizard
    {

        public string name;
        public string favoriteSpell;
        private int spellSlots;
        private float experience;
        public int hitpoints;
        public static int Count;
        
       
        

        public Wizard(string _name, string _favoriteSpell, int hitpoints)
        {
            spellSlots = 3;
            experience = 0f;
            hitpoints = 150;
            name = _name;
            favoriteSpell = _favoriteSpell;

            Count  ;
        }

        public void CastSpell()
        {
            if (spellSlots > 0)
            {
                int fireballDamage = new Random().Next(10, 30);
                Console.WriteLine(name   " cast "   favoriteSpell);
                Console.WriteLine("The spell hit for "   fireballDamage   " Damage");
                spellSlots--;
                experience  = 0.3f;
             
            }
            else
            {
                Console.WriteLine(name   " is out of mana");
            }

        }

        public void Meditate()
        {
            Console.WriteLine(name   " meditates to regain spell slots.");
            spellSlots = 3;
        }

    }


    class Program
    {
        static void Main(string[] args)
        {

            string characterName = "John  Doe";
            Console.WriteLine("What would you like your character's name to be?");
            characterName = Console.ReadLine();

            Wizard wizard = new Wizard(characterName, "fireball", 150 );
            Goblin goblin = new Goblin(50,120);



            Console.WriteLine("Hello, "   characterName   ". As a wizard your first mission is to kill the goblin "  
               "terrorizing the town.");
            Console.WriteLine("-------------");

            Console.WriteLine(characterName   " encounters the goblin in the middle of attacking a group of children:");   

            while (wizard.hitpoints > 0 && goblin.hitpoints > 0)
            {

                int enemyDamage = new Random().Next(1, 10);
                int fireballDamage = new Random().Next(10, 30);
                Console.WriteLine("The Goblin attacked for "   enemyDamage   " Damage");
            }
            wizard.CastSpell();

            


            Console.ReadKey();
        }

    }
}

Thanks!

CodePudding user response:

Let's use your Goblin class as an example:

class Goblin
{
    public int hitpoints;
    public int experienceGiven;
    public Goblin(int hitpoints, int experienceGiven)
    {
        hitpoints = 50;
        experienceGiven = 120;
    }
    public void gobAttack()
    {
        int enemyDamage = new Random().Next(1, 10);
        Console.WriteLine("The Goblin attacked for "   enemyDamage   " Damage");
    }
}

Note that I have removed the pointless whitespace to make it more readable. That's the sort of thing that you should be doing for us.

In that constructor, you are completely ignoring the value passed in via the hitpoints parameter. You are assigning 50 to that parameter, which has no effect on the field. Ignoring the fact that you should be using properties, that constructor should be this:

public Goblin(int hitpoints, int experienceGiven)
{
    this.hitpoints = hitpoints;
    this.experienceGiven = experienceGiven;
}

The value passed in via the parameter is assigned to the field in each case.

Ideally, you should use common naming conventions and you should use properties to expose data publicly. That would look like so:

class Goblin
{
    public int HitPoints { get; set; }
    public int ExperienceGiven { get; set; }

    public Goblin(int hitPoints, int experienceGiven)
    {
        HitPoints = hitPoints;
        ExperienceGiven = experienceGiven;
    }

    public void Attack()
    {
        var enemyDamage = new Random().Next(1, 10);

        Console.WriteLine($"The Goblin attacked for {enemyDamage} Damage");
    }
}

Because the property names and constructor parameter names using different casing, there's no need to use this to refer explicitly to the members. When the casing is the same, the fields are hidden by the parameters in the context of the constructor, so you need to use this in order to refer to the fields rather than the parameters.

  •  Tags:  
  • c#
  • Related