So I copied your code and messed with it a bit, here it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int pennies, comp_remove, user_win, comp_win, pennies_removed_user;
int checkPenniesTaken( int toBeChecked )
{
if( toBeChecked <= pennies )
return toBeChecked;
cout<<"Sorry, you're trying to take more pannies than are in the pile. Please try" <<endl;
cout<<"again. Remember, there are " <<pennies << " pennies in the pile." <<endl;
int temp;
cin>>temp;
return checkPenniesTaken( temp );
}
int playGame( int count )
{
system( "CLS" );
if( count > 4 )
return count;
srand( time(NULL) );
cout << "Please enter how many pennies you would like for there to be in the pile?"<<endl;
cin >> pennies;
system("CLS");
cout << "There are "<< pennies << " pennies in the pile"<<endl;
for( int i = 0; i != pennies; i++ )
cout << "O";
while( pennies > 0 )
{
comp_remove = rand()%pennies + 1;
cout << "\n\n\nThe computer removes " << comp_remove << " pennies from the pile"<<endl;
pennies = pennies - comp_remove;
for( int i = 0; i < comp_remove; i++ )
cout << "O";
if( pennies == 0 && comp_remove >= 0 )
{
user_win++;
cout<<"You win!" <<endl;
system( "PAUSE" );
return playGame( count+1 );
}
cout<<endl;
system("CLS");
cout << "There are currently " << pennies << " pennies left in the pile"<<endl;
for( int i = 0; i != pennies; i++ )
cout << "O";
cout << "\nPlease enter how many pennies you wish to remove from the pile: "<<endl;
cin>>pennies_removed_user;
pennies_removed_user = checkPenniesTaken( pennies_removed_user );
pennies = pennies - pennies_removed_user;
system("CLS");
cout << "There are now " <<pennies <<" pennies left in the pile." <<endl;
for( int i = 0; i < pennies; i++ )
cout << "O";
if( pennies <= 0 && pennies_removed_user >= 0 )
{
comp_win++;
cout<<"The computer wins!" <<endl;
system( "PAUSE" );
return playGame( count+1 );
}
}
}
int main(int argc, char *argv[])
{
int i = playGame( 0 );
cout<< playGame( 0 ) <<" games played." <<endl;
cout<< "Player wins: " <<user_win <<endl;
cout<< "Computer wins: " <<comp_win <<endl;
if( user_win > comp_win )
cout<< "The computer won more. You'd better practice up on this game!" <<endl;
else
cout<< "Congradulations, you won more than the computer!" <<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
|
Basically what this does is call playGame() in the first line. IDK how much you know about C++ but cout<< needs a number to display, so playGame() is executed before anything is printed.
Now, let's take a look at the playGame() function. it takes one number, an int, as a parameter and returns an int. First we clear the screen, just to make things pretty, and check the number we passed into the function. If it's greater than 4 then that means that the game has already run five times and we should stop running the game and see how well we did (more on this later). I chose to seed rand() with the time for more variation in the number of pennies the computer takes, but you don't have to. We then specify how many pennies we want in the pile, the computer takes his share, and we cout the results from this. The next few lines are the real beauty in the program. If the computer has removed the last penny then we tell the player so and call playGame() again, this time with a number one greater than the original number. When we first called playGame() count was at 0. Now, though, it's at 1. When it reaches 5 then the function will return 5 and in effect exit the game. The program then goes through the rest of the loop, doing the same thing for the player as it did for the computer in terms of checking to see who won. The technique used here is called recursion, loosely defined as a function calling itself.
The biggest change I made other than that is to change all your if statements to say
if( pennies < 0 )
instead of
if( pennies == 0)
. The way you had it, no one would win if the player took out more pennies than are in the pile. I threw in some recursive error checking with the checkPenniesTaken() function, but it's still a good idea to make sure that all eventualities produce a meaningful result.