|
-
October 29th, 2008, 12:08 AM
#1
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;
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|