mercredi 20 juillet 2016

Xcode C++ How to add a "Play Again?" function to my program

my program is all finished but I want to know how to add a "Play again" feature that uses "Yes" or "No" input to decide whether to run the program again or to end it. I am a little unsure of where to place it in my program since I use a handful of while loops and if/else statements. Any insight would be appreciated, thanks! Here is my code:

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>

using namespace std;

int main() // Setting up the code to follow.
{
    srand(static_cast<unsigned int>(time(0)));

    int Weapon_Selection;  // Creating variables for the player and computer's weapon selection.
    int Comp_Weapon;
    bool Battle = true;    // The variable to begin the battle.

    while(Battle)          // The main while loop to run the program code.
    {
        int Player_Health = 100; // More variables for both the player and the computer.
        int Comp_Health = 100;
        int Cannon_Ammo = 3;
        int Grenade_Ammo = 4;
        int Comp_Cannon_Ammo = 3;
        int Comp_Grenade_Ammo = 4;

        while(Player_Health > 0 && Comp_Health > 0) // The while loop to continue running the program while both combatants are alive.
        {
            bool Invalid = true;
            bool Comp_Invalid = true;


            while(Invalid)
            {
                cout << "Select a weapon using number keys 1, 2, and 3.\n"; // Weapon selection input from the player.
                cout << "1. Cannon (Ammo left: " << Cannon_Ammo << ")\n";
                cout << "2. Grenade (Ammo left: " << Grenade_Ammo << ")\n";
                cout << "3. Rifle\n";
                cout << "\n";
                cout << "You select: ";
                cin >> Weapon_Selection;

                srand(static_cast<unsigned int>(time(0)));

                if (Weapon_Selection == 1 && Cannon_Ammo > 0)
                {
                    Cannon_Ammo -= 1;
                    Invalid = false;
                    int Player_Dmg = rand() % 5 + 10; // Randomly generated damage range.
                    cout << "You caused " <<  Player_Dmg << " cannon damage to your enemy!\n";
                    Comp_Health -= Player_Dmg;
                    cout << "Your health is " << Player_Health << "\n";
                    cout << "The computer's health is " << Comp_Health << "\n";
                    cout << "\n";
                    break;
                }

                else if (Weapon_Selection == 2 && Grenade_Ammo > 0)
                {
                    Grenade_Ammo -= 1;
                    Invalid = false;
                    int Player_Dmg = rand() % 5 + 7;
                    cout << "You caused " << Player_Dmg << " grenade damage to your enemy!\n";
                    Comp_Health -= Player_Dmg;
                    cout << "Your health is " << Player_Health << "\n";
                    cout << "The computer's health is " << Comp_Health << "\n";
                    cout << "\n";
                    break;
                }

                else if (Weapon_Selection == 3)
                {
                    Invalid = false;
                    int Player_Dmg = rand() % 5 + 3;
                    cout << "You caused " << Player_Dmg << " rifle damage to your enemy!\n";
                    Comp_Health -= Player_Dmg;
                    cout << "Your health is " << Player_Health << "\n";
                    cout << "The computer's health is " << Comp_Health << "\n";
                    cout << "\n";
                    break;
                }

                else
                    cout << "You are out of ammo or you have entered an invalid number." << "\n"; // Output for wrong number input or no ammo.
                    cout << "\n";
            }


            if (Comp_Health <= 0) // Break point if the computer dies in battle.
                break;

            while(Comp_Invalid) // The computer's random weapon selection.
            {
                Comp_Weapon = rand() % 3 + 1;

                if (Comp_Weapon == 1 && Comp_Cannon_Ammo > 0)
                {
                    Comp_Cannon_Ammo -= 1;
                    Comp_Invalid = false;
                    int Comp_Dmg = rand() % 5 + 10;  // Randomly generated damage range for the computer.
                    cout << "Your enemy used a cannon and caused " << Comp_Dmg << " damage to you!\n";
                    Player_Health -= Comp_Dmg;
                    cout << "Your health is " << Player_Health << "\n";
                    cout << "The computer's health is " << Comp_Health << "\n";
                    cout << "\n";
                }

                else if (Comp_Weapon == 2 && Comp_Grenade_Ammo > 0)
                {
                    Comp_Grenade_Ammo -= 1;
                    Comp_Invalid = false;
                    int Comp_Dmg = rand() % 5 + 7;
                    cout << "Your enemy used a grenade and caused " << Comp_Dmg << " damage to you!\n";
                    Player_Health -= Comp_Dmg;
                    cout << "Your health is " << Player_Health << "\n";
                    cout << "The computer's health is " << Comp_Health << "\n";
                    cout << "\n";
                }

                else
                {
                    Comp_Invalid = false;
                    int Comp_Dmg = rand() % 5 + 3;
                    cout << "Your enemy used a rifle and caused " << Comp_Dmg << " damage to you!\n";
                    Player_Health -= Comp_Dmg;
                    cout << "Your health is " << Player_Health << "\n";
                    cout << "The computer's health is " << Comp_Health << "\n";
                    cout << "\n";
                }

            }
        }
        if(Player_Health < 0) // End of battle output, regardless of who wins or loses.
            cout << "You have been defeated by your enemy!" << "\n" << endl;

        else
            cout << "Congratulations, you beat your enemy!" << "\n" << endl;


    }
    cin.ignore(2);
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire