Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

math equation for fighting

I have the following code for a fight but the user will win around 99.9% of the time (tested with 5000 random loops)

I have the following variables that affect the fight

strength | defence | dexterity | damage | portal difficulty (always + 1 to stop * by 0) | critical

This is what I have so far

//player variable 
//(int) player.itemDamage = 20
//(int) player.itemStr = 2
//(int) player.itemDex = 4

            int defense = (int)Math.Round((((portal + 1) * ((rand.NextDouble() + 0.5))) + 5) / 2, 0);
            int damage = (int)Math.Round((((portal + 1) * ((rand.NextDouble() + 0.5))) + player.itemDamage), 0);
            int str = (int)Math.Round((((portal + 1) * ((rand.NextDouble() + 0.5))) + player.itemStr), 0);
            int dex = (int)Math.Round((((portal + 1) * ((rand.NextDouble() + 0.5))) + player.itemDex), 0);
while(true)
{
for (int i = 0; i < 10; i++)
                {
                    critical += rand.NextDouble();
                }
                eHP -= (int)Math.Round((((player.itemDamage + player.itemStr) - defense) * critical) / 2, 0);
                critical = 1;

                for (int i = 0; i < 10; i++)
                {
                    critical += rand.NextDouble();
                }
                HP -= (int)Math.Round((((damage + str) - 5) * critical) / 2, 0);

                if (eHP <= 0)
                {
                    return;
                }
else if (HP <= 0)
                {
                    return;
                }
                }

What could I change in the following code to let the user (HP) win 70% of the time can I get some suggestions please? I am terrible with algorithms Edit: Although I want the user to win 70% of the time I still want it to be round based on damage not using a simple if(0 > 70) win else loose statement because that wouldn't be a very interesting fight.

like image 818
Jordan Trainor Avatar asked Jul 10 '26 11:07

Jordan Trainor


1 Answers

In all seriousness, it is understandable why you are having trouble with this based on how you are approaching it with code. You should instead treat it like a math function.

Create a pure function f from a set of inputs (x, y, ..., z) to a boolean output. There should not be any usage of rand in this function. If you need random input, pass then into the function. Try doing it first without overwriting existing variables (don't use +=/-=). For every (x, y, z), f(x, y, z) should always result in the same output. If you do this, it should be pretty easy to use maths to solve for the missing values.

like image 106
drstevens Avatar answered Jul 14 '26 01:07

drstevens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!