Thread: Turn Based Battle

  1. #1
    Registered User madelinelise's Avatar
    Join Date
    Jul 2014
    Posts
    18

    Turn Based Battle

    Hello! First, I apologize for the long post.

    I am new to C++. I'm working on an assignment for school and I have a few errors I would like to work out.

    The program executes, but there are a few things I would like to add but not sure how to implement it into the program..

    1.) I would like to create a system pause in between the player output and the computer output. I have tried (you can see I have commented out at the end) a few different pauses, however none of them actually paused the game. I tried placing the pause in multiple places...

    2.) This game is supposed to be turn based, so it is suppose to function where the player takes a turn, then the computer, then the player.. so on. It does that MOST of the time, however in the process of randomizing the weapon damage I think I randomized whether the computer takes a turn at all.

    3.) I want to add a function where the player from the very beginning has an option of <1> easy <2> hard. Easy would mean that the computer randomly picks weapons (already have that as default). Hard would be where the computer depletes all cannons first. You can see where I have started it and commented it out.

    Any help would be greatly appreciated!

    Code:
    // Issues...........
    
    
    // Cannot get system pause to work.
    // Cannot get easy or hard mode to work
    // The program seems to randomize if the computer gets a turn at all.
    
    
    
    
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    
    using namespace std;
    
    
    
    
    int main()
    {
        int Player_Health = 100,
        Computer_Health = 100,
        Turn = 0,
        Weapon_Choice,
        Computer_Weapon,
        easy = 1,
        hard = 2,
        difficultyChoice,
        Cannon_Damage = 0,
        Grenade_Damage = 0,
        Rifle_Damage = 0;
        int Player_CannonQty = 3;
        int Player_GrenadeQty = 4;
        int Computer_CannonQty = 3;
        int Computer_GrenadeQty = 4;
        
        
        // cout << "Please select <1> easy or <2> hard:" << endl;
        //cin >> difficultyChoice;
        
        
        //if (difficultyChoice == 1)
        
        
        srand(static_cast<int>(time(0)));
        
        do
        {
            
            if(Turn == 0)// Player Turn
            {
                cout << "\nPick a weapon. <1> <2> or <3>:\n";
                cout << "1. Cannon\n";
                cout << "2. Grenade\n";
                cout << "3. Rifle\n";
                cin >> Weapon_Choice;
                
                //Validate weapon choice
                
                while ((Weapon_Choice < 1 || Weapon_Choice > 3) || (Weapon_Choice == 1
                && Player_CannonQty == 0) || (Weapon_Choice == 2 && Player_GrenadeQty ==0) )
        
                {
                    cout << "\nPlease enter a valid option\n";
                    cin >> Weapon_Choice;
                }
                
    
    
                
                
                
                switch(Weapon_Choice)
                {
                        
                    case 1: // player chooses to attack with cannon
                        Cannon_Damage = (10 + rand() % 6);
                        cout << "\nYou chose a cannon"<< endl;
                        Computer_Health = Computer_Health - Cannon_Damage;
                        cout << "You inflicted " << Cannon_Damage << " points on your enemy." << endl;
                        cout <<  "Your health is " << Player_Health << endl;
                        cout << "The computer's health is " << Computer_Health << endl;
                        Player_CannonQty = Player_CannonQty - 1;
                        break;
                        
                        
                    case 2:
                        Grenade_Damage = (7 + rand() % 13);
                        cout << "\nYou chose a grenade"<< endl;
                        Computer_Health = Computer_Health - Grenade_Damage;
                        cout << "You inflicted " << Grenade_Damage << " points on your enemy." << endl;
                        cout <<  "Your health is " << Player_Health << endl;
                        cout << "The computer's health is " << Computer_Health << endl;
                        Player_GrenadeQty = Player_GrenadeQty - 1;
                        
                        break;
                        
                    case 3:
                        Rifle_Damage = (3 + rand() % 9);
                        cout << "\nYou chose a Rifle"<< endl;
                        Computer_Health = Computer_Health - Rifle_Damage;
                        cout << "You inflicted " << Rifle_Damage << " points on your enemy." << endl;
                        cout <<  "Your health is " << Player_Health << endl;
                        cout << "The computer's health is " << Computer_Health << endl;
                        
                        break;
                }
                
                
                
            }
            
            
            
            Turn == 1; // Computer Turn
            
            Computer_Weapon = rand() % 3;
            
            
            switch(Computer_Weapon)
            {
                    
                case 1:
                    
                    Cannon_Damage = (10 + rand() % 6);
                    cout<<"\nYour opponent used a cannon and inflicted " << Cannon_Damage << " points on you." << endl;
                    Player_Health = Player_Health - Cannon_Damage;
                    cout << "Your health is " << Player_Health << endl;
                    cout << "The computer's health is " << Computer_Health << endl;
                    Computer_CannonQty = Player_CannonQty - 1;
                    break;
                    
                case 2:
                    Grenade_Damage = (7 + rand() % 6);
                    cout<<"\nYour opponent used a grenade and inflicted " << Grenade_Damage << " points on you." << endl;
                    Player_Health = Player_Health - Grenade_Damage;
                    cout << "Your health is " << Player_Health << endl;
                    cout << "The computer's health is " << Computer_Health << endl;
                    Computer_GrenadeQty = Computer_GrenadeQty - 1;
                    break;
                    
                case 3:
                    Rifle_Damage = (3 + rand() % 10);
                    cout<<"\nYour opponent used a rifle and inflicted " << Rifle_Damage << " points on you." << endl;
                    Player_Health = Player_Health - Rifle_Damage;
                    cout << "Your health is " << Player_Health << endl;
                    cout << "The computer's health is " << Computer_Health << endl;
                    break;
                    
                    
            }
            
            
            
            
        } while(Player_Health >= 0 && Computer_Health >= 0); //loops while both players are alive
        
        
        
        if (Computer_Health < 0)
            cout << "Congratulations! You won!" << endl;
        
        if (Player_Health < 0)
            cout << "YOU HAVE DIED! GAME OVER!" << endl;;
        
        
        
        // std::cout<< "Press ENTER to continue..." << flush;
        
        // cin.ignore(cin.rdbuf()->in_avail()+1);
        
        return 0;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The reason ignore() is not working in this case is because if there are characters to ignore already from cin's internal buffer, ignore() discards those first and simply will not give the user a chance to type.

    You can try peeking to see if there is something to ignore first:
    Code:
    if (cin.peek() != char_traits<char>::eof())
       cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
    cin.get();
    But honestly this is a bit backwards. You should be ignoring after every extraction OR using strings to keep cin's internal buffers empty. Then you can pause whenever.
    Code:
    cin >> Weapon_Choice;
    cin.ignore();
    
    // or...
    #include <typeinfo> // for bad_cast
    #include <string>
    #include <sstream>
    
    // and there are alternatives to rolling your own converter function, but
    // purely for example...
    int strtoi(const string& stuff, bool exceptions_on = false)
    {
      int result = 0;
      istringstream convert(stuff);
      convert >> result;
      if ( !convert.good() && exceptions_on)
        throw bad_cast();
      
      return result;
    }
    
    string inputBuffer;
    getline(cin, inputBuffer);
    Weapon_Choice = strtoi(inputBuffer);
    Choosing a line by line input approach can be simple and easy to manage.
    Last edited by whiteflags; 07-12-2014 at 03:49 PM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This was the hardest to find simple mistake for the month of July.

    Code:
    Turn == 1; // Computer Turn
    Hint: How does "==" and "=" differ in what they do?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User madelinelise's Avatar
    Join Date
    Jul 2014
    Posts
    18
    Quote Originally Posted by stahta01 View Post
    This was the hardest to find simple mistake for the month of July.

    Code:
    Turn == 1; // Computer Turn
    Hint: How does "==" and "=" differ in what they do?

    Tim S.
    I know that == is an equality operator and = is an assignment. However when I use an assignment it gives me an error?!

    This is the output I get if I use just
    Code:
     turn =1;


    Your opponent used a grenade and inflicted 10 points on you.
    Your health: 90
    The computer's health: 100


    ^^^ This loops over and over without input until someone dies.

    If I use the == it will execute and ask for a weapon choice.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    when do you set it to zero after setting it to one?

    edit: the line with == does nothing; fix it or remove that line.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User madelinelise's Avatar
    Join Date
    Jul 2014
    Posts
    18
    Thank you whiteflag, I got it to pause in between turns!

    stahta01, This is where I changed the counter to 0


    Code:
    }    //end if
            
            
            counter = 0;
            
        }while(Player_Health >= 0 && Computer_Health >= 0); //loops while both players are alive
    Also, I changed the == to if((counter = 0)) and it no longer gives me and error message, it does however still loop repeatedly until the game ends without asking the user for input.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by madelinelise View Post
    Also, I changed the == to if((counter = 0)) and it no longer gives me and error message, it does however still loop repeatedly until the game ends without asking the user for input.
    You can figure out what's going on by stepping through your code with a debugger, such as gdb or whatever's built in to your IDE.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User madelinelise's Avatar
    Join Date
    Jul 2014
    Posts
    18
    I actually was just using the debugger for the first time this morning (as I mentioned, I'm a beginner). The counter was fine. I found the problem though, it was a simple miscalculation.
    I had
    Code:
    Computer_Weapon = rand() % 3;
    Changed it to

    Code:
    Computer_Weapon = 1 + rand() % 3;
    So, I'm assuming it was randomly selecting a weapon choice I didn't have for the computer.

  9. #9
    Registered User madelinelise's Avatar
    Join Date
    Jul 2014
    Posts
    18
    Ok, so I have almost everything worked out! Thank you everyone for the help and fast responses.


    My remaining problem is this:

    I put in an option to use easy and hard mode.

    Easy: Randomizes computer weapon choice
    Hard: Depletes cannons, grenades, then uses rifles.

    Before I added the easy and hard mode, it was defaulted to the functions of the easy mode. It randomly picked the weapons for the computer.

    After I added the hard mode, that mode works great. However, easy mode is now doing the same thing as hard.

    I TRIED to make sure that the rand function for the weapon switch was within the scope of the difficultyChoice = 1, and separated them by different else if statement.. no luck. Obviously, I'm missing something.

    So here is an update of the program..

    Code:
    
    #include "stdafx.h"
    
    
    
    
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    
    
    
    
    
    
    
    using namespace std;
    
    
    
    
    int main()
    {
    	int Player_Health = 100,
    		Computer_Health = 100,
    		counter = 1,
    		Weapon_Choice,
    		Computer_Weapon,
    		easy = 1,
    		hard = 2,
    		difficultyChoice,
    		Cannon_Damage = 0,
    		Grenade_Damage = 0,
    		Rifle_Damage = 0;
    	int Player_CannonQty = 3;
    	int Player_GrenadeQty = 4;
    	int Computer_CannonQty = 3;
    	int Computer_GrenadeQty = 4;
    	const int Cannon = 1;
    	const int Grenade = 2;
    	const int Rifle = 3;
    
    
    
    
    	cout << "Please select <1> easy or <2> hard:" << endl;
    	cin >> difficultyChoice;
    
    
    
    
    
    
    
    
    
    
    	srand(static_cast<int>(time(0)));
    
    
    	do
    	{
    
    
    		if ((counter = 1))// Player Turn
    		{
    			cout << "\nPick a weapon. <1> <2> or <3>:\n";
    			cout << "1. Cannon Balls: " << Player_CannonQty << "\n";
    			cout << "2. Grenade: " << Player_GrenadeQty << "\n";
    			cout << "3. Rifle: unlimited! \n";
    			cin >> Weapon_Choice;
    			cin.ignore();
    
    
    
    
    
    
    			//Validate weapon choice
    
    
    			while ((Weapon_Choice < 1 || Weapon_Choice > 3) || (Weapon_Choice == 1
    				&& Player_CannonQty == 0) || (Weapon_Choice == 2 && Player_GrenadeQty == 0))
    
    
    			{
    				cout << "\nPlease enter a valid option\n";
    				cin >> Weapon_Choice;
    				cin.ignore();
    			}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    			switch (Weapon_Choice)
    			{
    
    
    			case Cannon: // player chooses to attack with cannon
    				Cannon_Damage = (10 + rand() % 6);
    				cout << "\nYou chose a cannon." << endl;
    				Computer_Health = Computer_Health - Cannon_Damage;
    				cout << "You inflicted " << Cannon_Damage << " points on your enemy." << endl;
    				cout << "Your health: " << Player_Health << endl;
    				cout << "The computer's health: " << Computer_Health << endl;
    				std::cout << "\nPress ENTER to continue..." << flush;
    
    
    				cin.ignore(cin.rdbuf()->in_avail() + 1);
    				Player_CannonQty--;
    
    
    
    
    				break;
    
    
    
    
    			case Grenade:
    				Grenade_Damage = (7 + rand() % 13);
    				cout << "\nYou chose a grenade." << endl;
    				Computer_Health = Computer_Health - Grenade_Damage;
    				cout << "You inflicted " << Grenade_Damage << " points on your enemy." << endl;
    				cout << "Your health: " << Player_Health << endl;
    				cout << "The computer's health: " << Computer_Health << endl;
    
    
    				std::cout << "\nPress ENTER to continue..." << flush;
    
    
    				cin.ignore(cin.rdbuf()->in_avail() + 1);
    				Player_GrenadeQty--;
    
    
    
    
    				break;
    
    
    			case Rifle:
    				Rifle_Damage = (3 + rand() % 9);
    				cout << "\nYou chose a rifle" << endl;
    				Computer_Health = Computer_Health - Rifle_Damage;
    				cout << "You inflicted " << Rifle_Damage << " points on your enemy." << endl;
    				cout << "Your health: " << Player_Health << endl;
    				cout << "The computer's health: " << Computer_Health << endl;
    
    
    				std::cout << "\nPress ENTER to continue..." << flush;
    
    
    				cin.ignore(cin.rdbuf()->in_avail() + 1);
    
    
    
    
    				break;
    			}
    
    
    
    
    
    
    		} // end player turn.
    
    
    
    
    
    
    		else if ((counter = 2) && (difficultyChoice == 1)) // Computer Turn
    
    
    		{
    
    
    			Computer_Weapon = 1 + rand() % 3;
    
    
    			switch (Computer_Weapon)
    			{
    
    
    
    
    
    
    			case 1:
    
    
    				Cannon_Damage = (10 + rand() % 6);
    				cout << "\nYour opponent used a cannon and inflicted " << Cannon_Damage << " points on you." << endl;
    				Player_Health = Player_Health - Cannon_Damage;
    				cout << "Your health: " << Player_Health << endl;
    				cout << "The computer's health: " << Computer_Health << endl;
    				Computer_CannonQty--;
    				counter = 1;
    				break;
    
    
    			case 2:
    				Grenade_Damage = (7 + rand() % 6);
    				cout << "\nYour opponent used a grenade and inflicted " << Grenade_Damage << " points on you." << endl;
    				Player_Health = Player_Health - Grenade_Damage;
    				cout << "Your health: " << Player_Health << endl;
    				cout << "The computer's health: " << Computer_Health << endl;
    				Computer_GrenadeQty--;
    				counter = 1;
    				break;
    
    
    
    
    
    
    			case 3:
    				Rifle_Damage = (3 + rand() % 10);
    				cout << "\nYour opponent used a rifle and inflicted " << Rifle_Damage << " points on you." << endl;
    				Player_Health = Player_Health - Rifle_Damage;
    				cout << "Your health: " << Player_Health << endl;
    				cout << "The computer's health: " << Computer_Health << endl;
    				counter = 1;
    				break;
    
    
    
    
    
    
    			}
    
    
    		}
    
    
    		else if ((counter = 2) && (difficultyChoice == 2));
    			{
    				 if (Computer_CannonQty > 0)
    				 {
    					 Cannon_Damage = (10 + rand() % 6);
    					 cout << "\nYour opponent used a cannon and inflicted " << Cannon_Damage << " points on you." << endl;
    					 Player_Health = Player_Health - Cannon_Damage;
    					 cout << "Your health: " << Player_Health << endl;
    					 cout << "The computer's health: " << Computer_Health << endl;
    					 Computer_CannonQty--;
    					 counter = 1;
    					 continue;
    					
    				 }
    
    
    				if (Computer_CannonQty == 0 && Computer_GrenadeQty > 0)
    				{
    					Grenade_Damage = (7 + rand() % 6);
    					cout << "\nYour opponent used a grenade and inflicted " << Grenade_Damage << " points on you." << endl;
    					Player_Health = Player_Health - Grenade_Damage;
    					cout << "Your health: " << Player_Health << endl;
    					cout << "The computer's health: " << Computer_Health << endl;
    					Computer_GrenadeQty--;
    					cout << "The computer has: " << Computer_GrenadeQty << " grenades." << endl;
    					
    					continue;
    					
    				}
    
    
    				if (Computer_GrenadeQty == 0)
    				{
    					Rifle_Damage = (3 + rand() % 10);
    					cout << "\nYour opponent used a rifle and inflicted " << Rifle_Damage << " points on you." << endl;
    					Player_Health = Player_Health - Rifle_Damage;
    					cout << "Your health: " << Player_Health << endl;
    					cout << "The computer's health: " << Computer_Health << endl;
    					counter = 1;
    				}
    
    
    			}
    
    
    
    
    		 // end if
    
    
    
    
    
    
    
    
    	} while (Player_Health >= 0 && Computer_Health >= 0); //loops while both players are alive
    
    
    
    
    
    
    	if (Computer_Health < 0)
    		cout << "\nCongratulations! You won!" << endl;
    
    
    	if (Player_Health < 0)
    		cout << "\nYOU HAVE DIED! GAME OVER!" << endl;;
    
    
    
    
    	std::cout << "\nPress ENTER to continue..." << flush;
    
    
    	cin.ignore(cin.rdbuf()->in_avail() + 1);
    
    
    
    
    	return 0;
    }

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Do you know the difference between the comparison operator== and the assignment operator=? If so which of the two should you be using in this snippet:

    Code:
            if ((counter = 1))

    Jim

  11. #11
    Registered User madelinelise's Avatar
    Join Date
    Jul 2014
    Posts
    18
    I do know the difference, at least I thought. I had counter == 1 before, and then was told that was wrong.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    So do you always want to execute the code in that if statement? If so then why are you using the if statement? When you assign some non-zero value to the variable the code will always execute.

    Or do you want to execute that code only if counter is equal to 1?


    Jim

  13. #13
    Registered User madelinelise's Avatar
    Join Date
    Jul 2014
    Posts
    18
    No, I only want to execute each respective code when the counter is 1 or 2, so it creates a turned base.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Then you need to use the comparison operator== not the assignment operator=.

    And note, you're doing this in several places.

    Jim

  15. #15
    Registered User madelinelise's Avatar
    Join Date
    Jul 2014
    Posts
    18
    I now have this program running 100% the way I want it! Thank you everyone for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Turn-based gaming in C
    By hyetoch in forum C Programming
    Replies: 7
    Last Post: 11-10-2011, 08:11 AM
  2. Turn Based Stradegy System Methods
    By TylerMoyer in forum Game Programming
    Replies: 2
    Last Post: 07-30-2007, 10:45 PM
  3. Looping Text based battle
    By pujuman in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2004, 05:24 AM
  4. Turn based battle demo
    By Kirdra in forum Game Programming
    Replies: 0
    Last Post: 09-13-2002, 01:01 PM