Having serious problems with looping my program

deleted
Last edited on
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.
Last edited on
Thanks a lot mate, will have a big read through this today and try implement the ideas you've given me : )
deleted
Last edited on
int temp; cin>>temp is so that the player can chose a valid number of pennies to take from the pile. I could've used to_check or any int, really. cin>>temp, of course, assigns temp a value which the user enters.

int i = game(0); starts the game for the first time. the value you pass to it (in this case 0) represents the number of times the game has been played. I'm actually not sure why I have that line in there... Feel free to remove it, or replace cout<<game(0) <<endl; with cout<<i <<endl;

return game( count+1 ) could be count++, it doesn't really matter. The ++ operator increments the value of count while count+1 creates a new value, not changing count. We don't really need to change the value of count, but either way works fine.

There is another problem, though: on line 78 you have return (count+1) but you should have return game( count+1 );. The way you have it, it'll simply exit the game if the computer wins, which I assume is not what you want.

The count parameter represents the number of times that the game has been played, not the number of times you want the game to be played. The number of times you want the game to be played is represented by the constant in line 45 in your code (if( count > 4 )), in this case 4, plus one. The way the game is now, it'll play 4 times. Were you to change that number to, say, 10, the game would play 11 times.

Anything else?
Thanks, you've helped a ridiculous amount, I really appreciate it. The only problem I have is with the looping system now, it's looping quite a few times more than the 4 I specified! Thanks in advance...again : )
Topic archived. No new replies allowed.