Dice Game.

Hello,
I'm having some issues with my program, its suppose to roll a die then once the score reaches a set limit it declares a winner, if the roll is 1 then it is suppose to give the user the option to roll again or hold and pass turn onto the second player.
The issue I'm having is when the limit is reached it continues to roll the die, and when the user chose to hold it does not pass the turn onto the second player.
Any suggestions or pointers as to what I might be doing wrong would be helpful.

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
#include <iostream>
#include <cctype>
#include <cstring>
#include <iomanip>
using namespace std; 

int diceRoll (); 
int player1turn (int &);
int player2turn (int &); 
void winner (int&, int &, char);
int conplay (int&, int &);

const int SCORELIMIT = 10;

int main ()
{
  int p1score = 0;
  int p2score = 0; 
  char choice;
  srand (time(NULL));
  do
  {
  conplay (p1score, p2score);
  }
  while (choice == 'y' ||choice == 'Y');

return 0; 
}
/*
conplay allows the users to continue to play till one reaches 100 
*/
int conplay (int & p1score, int& p2score)
{
    
    char choice; 
   cout <<"Rules to pig:\n"<< " First player to make it to 100 wins!"<<endl;
   cout <<"Each player has the choice to roll or hold on their turns!\n";
    while (p1score <= SCORELIMIT )	
    {
             player1turn ( p1score);

           
    } 
    while(p2score <= SCORELIMIT)
    {
            player2turn ( p2score);  
    }      
    winner (p1score, p2score, choice);
    


}
/*
determins the winner of the dice game 
*/
void winner (int& p1score, int& p2score, char choice)
{  
    int player1wins = 0;
    int player2wins = 0; 
    
    if (p1score >= SCORELIMIT)// compares 
    {         
     cout<<" Player one has won! Your Score is "<<p1score<<endl;
    }
    
    if (p1score >= SCORELIMIT)
    {         
     cout<<" Player two has won! Your score is "<<p2score<<endl;
    }
     cout << " Would you like to play again? [ y or n]"<< endl;
     cin >> choice; //gives user the choice to play the game again
     
    while (choice == 'n'||choice == 'N')
    {
    cout << " GoodBye!" << endl; 
    }
}
/*
allows the user to roll the dice and keeps track of how many points the 
player has.
*/
int player1turn (int& p1score)
{   
    int cscore, roll;
    char rORh;
    
    cout << " The toral score is:" << cscore<<endl;
    cout << " Please enter r to roll again or h to hold:"<< endl; 
    cin >> rORh; 
    while (rORh == 'h'|| rORh == 'H') // ends the loop if user presses h and adds cscore to
                        // p1score
    { 
       cscore += p1score;
       break;
    }
    
    while (rORh == 'r'||rORh == 'R')
    {
      roll =diceRoll(); // calls in the dice roll function 
      if (roll == 1)
        {
          cout << " Player 1 rolled a" << setw(2) << roll<< " your turn is over!"<<endl;
          cscore += roll;
          break;
          
        }
      else 
        {
          
   	      cscore += roll; // adds the roll to the cscore 
          cout << "Player 1 rolled a"<< setw(2) << roll << endl;
          cout << "Player 1 Your score this turn is:"<< cscore<< endl;
          cout << "Player 1 Please enter r to roll again or h to hold:"<< endl; 
             cin >> rORh;     
        } 
    }
  return p1score; 
}
/*
allows the user to roll the dice and keeps track of how many points the 
player has.
*/
int player2turn (int& p2score)
{ 
    int cscore =0, roll;
    char rORh;
    cout << "Player 2 The toral score is:" << p2score<<endl;
    cout << "Player 2 Please enter r to roll again or h to hold:"<< endl; 
    cin >> rORh; 
    
    while (rORh == 'h'||rORh == 'H')// if user presses h p2score is added to cscore
    { 
    p2score += cscore;
       break;
    }
    
    while (rORh == 'r'||rORh =='R' )
    {
      roll =diceRoll();// calls in the dice roll function
      if (roll == 1)
        {
          cout << " Player 2 rolled a" << setw(2) << roll<< " your turn is over!"<<endl;
          cscore += roll;
          break;
        }
      else 
        {  
           cscore += roll;// adds the roll to the cscore 
          cout << "Player 2 rolled a"<< setw(2) << roll << endl;
          cout << "Player 2 score this turn is:"<< cscore<< endl;
          cout << "Player 2 enter r to roll again or h to hold:"<< endl; 
             cin >> rORh; 
        }
    }
  return p2score; 
}
/*
diceRoll, is a random number generater that will not allow the number go past 6
*/
int diceRoll()
{
	int roll;

	roll = (rand()%6)+1;
   
	return roll;
}
Well your external loop is looping only on whether the player has points <= the goal, so when you leave the function (because they chose to hold) it keeps looping. You need to change that loop condition or rework your code.
edit: in addition to the main problem

Alright I'll post as I find problems.
Problem # 1, cscore is not initiated.
When you create it set it = 0;

Problem # 2: you never edit p1score or p2score, you only add to cscore

Problem # 3: why the heck did you make two functions for p1 and p2?

Problem # 4: cscore is not the total score, pXscore is.

Problem # 5: the user can enter anything but r to escape from that while loop

Problem # 6: (not really a problem, just a suggestion) when you make your roll function you don't have to make a temporary int variable, all you have to do is
return (rand() % (highNumber - lowNumber) + lowNumber);//works as long as the range is > 0

Problem # 7: You don't need to pass a character reference into your winning function, unless you're actually going to use it. I would make the function return bool (playAgain) rather than having a useless extra parameter.


Alright, there are a couple things to work on, have fun and let us know how it works out (or if you have any more problems, of course)
Last edited on
Topic archived. No new replies allowed.