Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting awhile loop to work in C# for a game of HP

Tags:

c#

I do not know if the way I am trying to do this will work, but I am making a game where the player and the computer keep attacking each other until someone hits an HP of 0. I am trying to use a while loop to keep asking the player to attack tell someone dies. I can not get it to ask the player for another attack. It keeps saying please enter a number from 1-6 so it is just taking the blank text box as an input.

public int userPickForWeapon(int userPick)
{ //this takes what number the user pick for a weapon and returns it
    return userPick;
}
 private void btnPlayerOneAttack_Click(object sender, EventArgs e)
    {

            do 
            {
                int userPickTwo = 0;
                if (int.TryParse(txtPlayerWeapons.Text, out userPick))
                {
                    userPickTwo = userPickForWeapon(userPick);
                   
                        if (userPickTwo == 1)
                        {
                            bowAndArrow = rand.Next(8, 16); //picks a random number of damage
                            computerHP -= bowAndArrow; // takes away the players hp
                            lblComputerHP.Text = "HP: " + computerHP; // Shows the player their hp
                            lblComputerResultsShow.Text = "You used Bow and Arrow \n and did " + bowAndArrow + " damage";
                            txtPlayerWeapons.Text = ""; //clears the text box
                        }
                        else if (userPickTwo == 2)
                        {
                            sword = rand.Next(1, 10); //picks a random number of damage
                            computerHP -= sword; // takes away the players hp
                            lblComputerHP.Text = "HP: " + computerHP; // Shows the player their hp
                            lblComputerResultsShow.Text = "You used Sword \n and did " + sword + " damage";
                            txtPlayerWeapons.Text = ""; //clears the text box
                        }
                        else if (userPickTwo == 3)
                        {
                            fire = rand.Next(7, 15); //picks a random number of damage
                            computerHP -= fire; // takes away the players hp
                            lblComputerHP.Text = "HP: " + computerHP; // Shows the player their hp
                            lblComputerResultsShow.Text = "You used Fire \n and did " + fire + " damage";
                            txtPlayerWeapons.Text = ""; //clears the text box
                        }
                        else if (userPickTwo == 4)
                        {
                            ice = rand.Next(8, 15); //picks a random number of damage
                            computerHP -= ice; // takes away the players hp
                            lblComputerHP.Text = "HP: " + computerHP; // Shows the player their hp
                            lblComputerResultsShow.Text = "You used Ice \n and did " + ice + " damage";
                            txtPlayerWeapons.Text = ""; //clears the text box
                        }
                        else if (userPickTwo == 5)
                        {
                            wind = rand.Next(9, 15); //picks a random number of damage
                            computerHP -= wind; // takes away the players hp
                            lblComputerHP.Text = "HP: " + computerHP; // Shows the player their hp
                            lblComputerResultsShow.Text = "You used Wind \n and did " + wind + " damage";
                            txtPlayerWeapons.Text = ""; //clears the text box
                        }
                        else if (userPickTwo == 6)
                        {
                            water = rand.Next(1, 6); //picks a random number of damage
                            computerHP -= water; // takes away the players hp
                            lblComputerHP.Text = "HP: " + computerHP; // Shows the player their hp
                            lblComputerResultsShow.Text = "You used water \n and did " + water + " damage";
                            txtPlayerWeapons.Text = ""; //clears the text box
                        }
                        else
                        {
                            // Display an error message for if the user puts in something other than a number between 1 -6
                            MessageBox.Show("Please enter a number between 1 - 6");
                            txtPlayerWeapons.Focus();
                        }
                    }
                
                else
                {
                    // Display an error message for if the user puts in something other than a number
                    //inside the while loop
                    MessageBox.Show("Please enter a number between 1 - 6");
                }

            }while(playerOneHP >= 0 || computerHP >= 0);
        }
like image 417
Wheeze86 Avatar asked Jan 19 '26 14:01

Wheeze86


1 Answers

This is an order-of-operations problem. Generally, in these scenarios, what you want to do is:

  1. Enter a while loop
  2. Choose the active combatant
  3. If the active combatant is the player prompt the user to select a weapon
  4. Calculate damage for the selected weapon
  5. Inflict damage on the opposing player
  6. Exit the while loop if either player has reached HP 0

Instead, your algorithm appears to work as follows:

  1. Determines the active player
  2. Prompts the user for an active weapon
  3. Enters a while loop
  4. Calculates damage for the selected weapon
  5. Inflicts damage on the opposing player
  6. Exits the while loop if either player has reached HP 0

If this is how your algorithm is structured, it can't work because the loop will spin infinitely unless there's a huge spike in damage (possibly caused by a stray cosmic ray) that kills the opposing player.

Consider restructuring your algorithm so that it follows the first pattern.

Good luck!

Addendum

The loop that you are trying to create in your button click event is already provided for you by a Windows application, and is called the Event Loop. When your application starts, it enters a polling state, where it waits for the user to click an item in the user interface, press keys, or click mouse buttons. These actions raise events, which you handle in your application. Once you've done so, the application reenters the polling state. That polling state is the equivalent (but not exactly) of a while loop.

In the steps I've outlined above for you, the first and last steps refer to the Windows Event Loop (and it was my mistake for not being clearer).

Remove the while loop from your button click event handler, and let the operating system notify you when the user has clicked the button. Keep the logic inside the loop, just remove the do { and closing }.

like image 66
Mike Hofer Avatar answered Jan 22 '26 02:01

Mike Hofer



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!