Thread: Need help with a easy C++ 2 player game code

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    2

    Need help with a easy C++ 2 player game code

    This is my first c++ game and I'm trying to make a balloon popping game where you have a total of 10 balloons and 2 players take turns to pick either 1 or 2 balloons to pop, until there are no balloons left.
    I'm stuck trying to get it to keep looping for the next player to have their turn and so on till game over and the last player to pop a balloon wins.


    Here's the code I have:

    Code:
    // myballoongame.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    
    
    int main()
    {
    
    
        int rem_balloons = 10;
        int pick=1, win=0, pturn;
        srand(time(NULL));
        int random = rand();
        char player1[20], player2[20];
        
        while (rem_balloons>0)  {
        
    
    
            rem_balloons = 10;
            win = 0;
            cout << "Player 1 enter your name: " << endl;
            cin >> player1;
            cout << "Player 2 enter your name" << endl;
            cin >> player2;
    
    
    
    
            if (random % 2 == 0)
            {
                cout << player1 << " goes first" << endl << endl;
                pturn = 0;
            }
    
    
            else
            {
                cout << player2 << " goes first" << endl << endl;
                pturn = 1;
            }
        
    
    
            if (rem_balloons >= 0)
            {
                cout << "Pick one or two balloons to pop: " << endl;
            }
    
    
    
    
            while (pick != 0)
            {
                cin >> pick;
    
    
        
                if (pick == 1 || pick == 2)
    
    
                {
                    rem_balloons = rem_balloons - pick;
                    pick = 0;
                    cout << "There are " << rem_balloons << " balloons left" << endl;
                }
    
    
                else
                {
                    cout << "You must only pick upto 2 balloons to pop" << endl;
                    cout << "Pick again" << endl;
                    cin >> pick;
    
    
                    if (!cin)
    
    
                    {
                        cin.clear();
                        cin.ignore(numeric_limits<streamsize>::max(), '\n');
                        pick = 3;
                    }
    
    
                    if (pick == 1 || pick == 2)
    
    
                    {
                        rem_balloons = rem_balloons - pick;
                        pick = 0;
                        cout << "There are " << rem_balloons << " balloons left" << endl;
                    }
    
    
                }
                
            }
            
            return 0;
        }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    *moved to C++ programming forum*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    You might want to think why line 107 isn't the last statement in main.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2016
    Posts
    2
    Thanks forgot to move that

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Why do you have line 27?

    Code:
    rem_balloons = 10;
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alternating turns in C code for 2 player game
    By richardpd in forum C Programming
    Replies: 7
    Last Post: 01-20-2013, 05:41 PM
  2. Trying to implement a sound class for a player in a game
    By SimplyEvert in forum C++ Programming
    Replies: 3
    Last Post: 02-17-2012, 06:38 PM
  3. C++ Video Game Player Program
    By zonen in forum C++ Programming
    Replies: 33
    Last Post: 10-01-2011, 06:12 PM
  4. Basic structure for 2 player game
    By pants in forum C Programming
    Replies: 2
    Last Post: 05-10-2009, 05:16 PM
  5. Seg fault in easy, easy code
    By lisa1901 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2007, 05:28 AM

Tags for this Thread