0

I'm working with C# and I need advice on the following code I'm trying to make it so that I can calculate magic and physical damage according to the List below the Code. When I try to do the second example the Damage is 167.16782 instead of 199.3007 (I'm using Leauge of legends damage calculation system).

class Player
{
    float HP;
    float Armor;
    float PhysicalAttack;
    float MagicAttack;

    public void TakePhysicalDamage(float HP, float Armor, float PhysicalDamage)
    {
        float Multiplier;
        if (Armor >= 0)
        {
            Multiplier = 100 / (100 + Armor);
        }
        else
        {
            Multiplier = 2 - 100 / (100 - Armor);
        }
        PhysicalDamage = PhysicalDamage * Multiplier;
        float HP_Value = HP - PhysicalDamage;
        Console.WriteLine("Spelaren tog " + PhysicalDamage + " Fysisk skada");
        Console.WriteLine("Spelarens liv är " + HP_Value);
    }

    public void TakeMagicDamage(float HP, float MR, float MagicDamage)
    {
        float Multiplier;
        if (MR >= 0)
        {
            Multiplier = 100 / (100 + MR);
        }
        else
        {
            Multiplier = 2 - 100 / (100 - MR);
        }
        MagicDamage = MagicDamage * Multiplier;
        float HP_Value = HP - MagicDamage;
        Console.WriteLine("Spelaren tog " + MagicDamage + " Magisk Skada");
        Console.WriteLine("Spelarens liv är " + HP_Value);
    }

    public void TakeMagicAndPhysicalDamage(float HP, float MR, float MagicDamage, float Armor, float PhysicalDamage)
    {
        float PhysicalMultiplier;
        if (MR >= 0)
        {
            PhysicalMultiplier = 100 / (100 + MR);
        }
        else
        {
            PhysicalMultiplier = 2 - 100 / (100 - MR);
        }
        float MagicMultiplier;
        if (Armor >= 0)
        {
            MagicMultiplier = 100 / (100 + Armor);
        }
        else
        {
            MagicMultiplier = 2 - 100 / (100 - Armor);
        }
        PhysicalDamage = PhysicalDamage * PhysicalMultiplier;
        MagicDamage = MagicDamage * MagicMultiplier;
        float Damage = MagicDamage + PhysicalDamage;
        float HP_Value = HP - Damage;

        Console.WriteLine("Spelaren tog " + Damage + " Skada");
        Console.WriteLine("Spelarens liv är " + HP_Value);
    }

    public static void Main()
    {
        Player player = new Player();
        Console.WriteLine("Skriv spelarens Liv");
        float HP = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Skriv Spelarens Fysiska försvar");
        float Armor = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Skriv Spelarens Magiska försvar");
        float MR = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Skriv motståndarens Fysiska skada");
        float PhysicalDamage = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Skriv motståndarens Magiska skada");
        float MagicDamage = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Skriv in Fysisk skada, Magisk skada eller Båda");
        string input = Console.ReadLine();
        if (input == "Fysisk skada")
        {
            player.TakePhysicalDamage(HP, Armor, PhysicalDamage);

        }
        else if (input == "Magisk skada")
        {
            player.TakeMagicDamage(HP, MR, MagicDamage);
        }
        else if (input == "Båda")
        {
            player.TakeMagicAndPhysicalDamage(HP, MagicDamage, MR, Armor, PhysicalDamage);
        }
    }
}
Magicdmg  physicaldmg  MagicResist   Armor            Total Damage
100           100       100           100               =    100
200           100        30            120              =  199.3007
679           60         30            120              =  549.5804
100           350       -10            100              =  284.0909
Pradeep Kumar
  • 1,193
  • 1
  • 9
  • 21
Realgame
  • 1
  • 2
  • No offence, but your code has many problems, so it is hard to answer. Please learn a little bit more about C#. – Thorberg Nov 06 '22 at 22:58
  • 3
    There is something off with the code you pasted in the question. In the error message that you mentioned, it said that `Player.TakePhysicalDamage` takes 4 `float` params, including `HP` as first, but in the code, this method doesn't take any parameter. Please review – Rodrigo Rodrigues Nov 06 '22 at 22:58
  • Yeah sorry it's suposed to be public void TakePhysicalDamage(float HP, float Armor, float PhysicalDamage, float HP_Value) – Realgame Nov 07 '22 at 15:41
  • [Make the arguments optional](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments#optional-arguments) or create a class with the values that need to be set, give them default values, set them as needed, and use an instance of that class as the argument instead. – Jesse Nov 07 '22 at 18:32
  • Does this answer your question? [How can you use optional parameters in C#?](https://stackoverflow.com/questions/199761/how-can-you-use-optional-parameters-in-c) – Jesse Nov 07 '22 at 18:35

1 Answers1

1

Although there are still many changes required in your code. But i have restructured it a bit to make it runnable. You can make your own changes as required.

class Player
    {
        float HP;
        float Armor ;
        float PhysicalAttack;
        float MagicAttack;
        public void TakePhysicalDamage(float HP, float Armor, float PhysicalDamage)
        {
            float Multiplier;
            if (Armor >= 0)
            {
                Multiplier = 100 / 100 + Armor;
            }
            else
            {
                Multiplier = 2 - 100 / 100 - Armor;
            }
                PhysicalDamage = PhysicalDamage * Multiplier;
                float HP_Value = HP - PhysicalDamage;
                Console.WriteLine("Spelaren tog" + PhysicalDamage + "Fysisk skada");
                Console.WriteLine("Spelarens liv är" + HP_Value);
        }
        public void TakeMagicDamage(float MR, float MagicAttack, float MagicDamage)
        {
            float Multiplier;
            if (MR >= 0)
            {
                Multiplier = 100 / 100 + MR;
            }
            else
            {
                Multiplier = 2 - 100 / 100 - MR;
            }
                MagicDamage = MagicAttack * Multiplier;
                float HP_Value = HP - MagicDamage;
                Console.WriteLine("Spelaren tog" + MagicDamage + "Magisk Skada");
                Console.WriteLine("Spelarens liv är" + HP_Value);
        }
       
        public static void Main()
        {
            Player player = new Player();
            Console.WriteLine("Skriv spelarens Liv");
            float HP = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Skriv Spelarens Fysiska försvar");
            float Armor = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Skriv Spelarens Magiska försvar");
            float MR = Convert.ToInt32(Console.ReadLine());
    
            Console.WriteLine("Skriv motståndarens Fysiska skada");
            float PhysicalDamage = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Skriv motståndarens Magiska skada");
            float MagicDamage = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Skriv in Fysisk skada eller Magisk skada");
            string input = Console.ReadLine();
            Console.WriteLine("input for MagicAttack");
            float MagicAttack = Convert.ToInt32(Console.ReadLine()); // input for MagicAttack
            if (input == "Fysisk skada")
            {
                player.TakePhysicalDamage(HP,Armor,PhysicalDamage);
    
            }
            else if (input == "Magisk skada")
            {
                player.TakeMagicDamage(MR,MagicAttack,MagicDamage);
            }
        }
    }
HarrY
  • 607
  • 4
  • 14