CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2008
    Posts
    4

    dice game of pig

    Ok I wrote out the whole program and it works except for one problem. Whenever the user or the computer rolls a 1, instead of going to the next player (whether it be from the user to the computer or vice versa) it just lets that same player roll again. I was wondering where my error is?
    Code:
    #include <string> 
    #include <iostream>
    
    
    using namespace std;
    
    int humanTurn(int humanTotalScore);
    int computerTurn(int computerTotalScore);
    
    int main()
    {
        
        int humanTotalScore;
        int computerTotalScore;
        humanTotalScore=0;
        computerTotalScore=0;
        string playername;
        
        cout << "What is your name?\n";
             cin >> playername;
        cout << "Dice Game of Pig\n";
        getchar ();
        cout << "The goal of Pig is to reach 100 points or more before the other player does.\n";
        cout << "Each time you roll the die points are added to your turn total.";
        cout << "If you roll a 2-6, then you can choose to roll again or stop ";
        cout << "If you stop, then the points are added to your score.\n";
        cout << "But if you roll a 1, then all your points for that turn are lost!\n\n";
        
        do
        {
          humanTotalScore=humanTurn(humanTotalScore);
          computerTotalScore=computerTurn(computerTotalScore);
        
        if (humanTotalScore>=100 && computerTotalScore<=99)
        {
           cout << playername << ", you win!";
        }
        
        if (humanTotalScore<=99 && computerTotalScore>=100)
        {
           cout << "Sorry " << playername << ", you lost.";
        }
        
        else
                continue;
        }
        while (humanTotalScore<=99 && computerTotalScore<=99);{
    }
    
        
        getchar ();
        return 0;
    
    }
    
    int humanTurn(int humanTotalScore)
    {
        int RollTotal;
        RollTotal=0;
        char human_answer;
        
        
        do 
          {
          int randomNumber = rand(); 
          int die = (randomNumber % 6) + 1; 
          cout << "You rolled a " << die << endl;
          
          if (die == 1)  
          {
                cout << "Your score for this round is lost.\n";  
                RollTotal = 0;
          }
          else
          {
                RollTotal+= die;
                
                cout << "Your current total is " << RollTotal;
                
                cout << "\nDo you want to roll again or hold?\n";
                     cin >> human_answer;
                if (human_answer !='r' && human_answer != 'R'){
                humanTotalScore+=RollTotal;
                            cout << "\nThen you hold. Your total score is " << humanTotalScore;
                            getchar ();
                            }
                
                
                else
                   continue;
          }
          getchar ();
          }
          while (human_answer == 'r' || human_answer == 'R');{
          
    
          return humanTotalScore;
    }
    }
    
    int computerTurn(int computerTotalScore)
    {
        int RollTotal;
        RollTotal=0;
        
        do
        {
          int randomNumber = rand(); 
          int die = (randomNumber % 6) + 1; 
          cout << "\nComputer rolled a " << die << endl;
          
          if (die == 1)  
          {
                cout << "Computer's score for this round is lost.\n";  
                RollTotal = 0;
          }
          else
          {
                RollTotal+= die;
                
                cout << "Computer's current total is " << RollTotal;
                
                
                if (RollTotal>=20){
                computerTotalScore+=RollTotal;
                            cout << "\nThen computer hold. Computer's total score is " << computerTotalScore;
                            getchar ();
                            }
                
                
                else
                   continue;
          }
          getchar ();
          }
          while (RollTotal <=19);{
          
    
          return computerTotalScore;
    }
    }

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: dice game of pig

    Run it through the debugger, step through the code line by line. You will figure out what is wrong.

  3. #3
    Join Date
    Feb 2006
    Location
    Croatia - Zagreb
    Posts
    459

    Re: dice game of pig

    It's because you didn't specify what will happen after "1" is rolled.
    You just carry on with the loop.
    Try putting break or something in the if keyword when "1" is rolled.

    Oh and BTW if you want different values every time you run a program, use:
    srand(time(0)); this will randomize the rand() seed, based on the timer.
    rand(); is made to get the same values everytime because it's easier to debug.
    Last edited by Odiee; October 29th, 2008 at 03:33 AM.
    You just divided by zero, didn't you?

  4. #4
    Join Date
    Oct 2008
    Posts
    4

    Re: dice game of pig

    ya haha i figured out why it didnt stop at 1 its because i had to add the line to calculate the total score then return it and thanks
    and i will try the srand that makes a lot of sense
    your help is much appreciated

    and of the debugger I'm not quite sure how to use that so maybe a link to a tutorial?
    THanks guys.

  5. #5
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: dice game of pig

    darkavenger, I was about to point you to a tutorial. But you have turned off private messaging.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured