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.
/*
#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.
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)