C# Can someone help me with this code


#1

The problem is that i can't get out of the do while loop
It should go out if dragonHealth or heroHealth is below zero or zero
any ideas ? much appreciated

do
                        {
                            Console.WriteLine("The dragon wakes up !");
                            Console.WriteLine("and the fight begins...");
                            Console.WriteLine("Press any key to continue!");
                            Console.ReadLine();
                            Found:
                            Console.WriteLine("Your health: {0}", heroHealth);
                            Console.WriteLine("Dragon health: {0}", dragonHealth);
                            Console.WriteLine("What are you going to do?");
                            Console.WriteLine("1.Use Sword does 100dmg");
                            Console.WriteLine("2.Use Ice spell does 50 dmg has 50/50 chance to stun");
                           
                            val = Convert.ToInt32(Console.ReadLine());
                            Console.Clear();
                            switch (val)
                            {
                                case 1:
                                    dragonHealth -= 100;
                                    Console.WriteLine("You slash 100 dmg");
                                    Console.WriteLine("Dragon health: {0}" , dragonHealth);
                                    Console.WriteLine("Press any key to continue...");
                                    Console.ReadLine();
                                    oracle = rng.Next(10, 21);
                                    Console.WriteLine("Now its dragon turn");
                                    Console.WriteLine("Dragon does {0} dmg", oracle);
                                    heroHealth -= oracle;
                                    Console.WriteLine("Your health: {0}", heroHealth);
                                    Console.WriteLine("Press any key to continue...");
                                    Console.ReadLine();
                                    Console.Clear();
                                    goto Found;

                                case 2:
                                    oracle = rng.Next(1, 3);
                                    dragonHealth -= 50;
                                    Console.WriteLine("Icespell does 50 dmg");
                                    Console.WriteLine("Dragon health: {0}", dragonHealth);
                                    if(oracle == 2)
                                    {
                                        Console.WriteLine("The dragon is stunned you get another round!");
                                        Console.WriteLine("Press any key to continue...");
                                        Console.ReadLine();
                                        Console.Clear();
                                        goto Found;
                                    }
                                    else
                                    Console.WriteLine("Press any key to continue...");
                                    Console.ReadLine();
                                    oracle = rng.Next(10, 21);
                                    Console.WriteLine("Now it dragon turn");
                                    Console.WriteLine("Dragon does {0} dmg", oracle);
                                    heroHealth -= oracle;
                                    Console.WriteLine("Press any key to continue...");
                                    Console.ReadLine();
                                    Console.Clear();
                                    goto Found;

                            }
                        } while (dragonHealth != 0 || heroHealth != 0 || heroHealth < 0 || dragonHealth < 0);
                       if(heroHealth == 0 || heroHealth < 0)
                        {
                            Console.WriteLine("You died");
                            Console.ReadLine();
                        }
                        else Console.WriteLine("You've slain the mighty dragon congratz!");

#2

Those "goto Found" expressions creates an infinite loop. You end up looping inside the do statement, never even reaching condition check.


#3

Thanks,

so I took all the goto away,
it is still stuck in the do while loop maybe something is wrong with the:
while (dragonHealth != 0 || heroHealth != 0 || heroHealth < 0 || dragonHealth < 0)
maybe the "||" or operator? Am I right here if one of the || are true it will go out?


#4

Ok I found out the problem it was while (heroHealth > 0 && dragonHealth > 0);
Thanks for the help :slight_smile: