Writing Programs with Multiple functions (Edit)
Oct 24, 2011 at 3:17am UTC
hi, i recently wrote some code for a school project and before i start this question, i am not asking you to do this for me, i have just encountered an error that i would like your advice on
anyways,
we were instructed to make a program that would simulate the dice game "pig"
what i would like to know is why my code insists on giving the player one turn,
then the computer turn, and then give the player two turns, and give the computer one turn, and then again give the player two turns, and give the computer one turn
i know my question isn't phrased the best i would really appreciate some help!
thank very much =)
here is my code:
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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int humanTurn(int &Humantotalscore);
int computerTurn(int &Computertotalscore);
int roll ();
int turn = 0;
int d = 0;
int main ()
{
srand(time(NULL));
int Humantotalscore = 0;
int Computertotalscore = 0;
while ((humanTurn(Humantotalscore) < 100) && (computerTurn(Computertotalscore) <100))
{
if ((humanTurn(Humantotalscore) >= 100) && (computerTurn(Computertotalscore) <100))
{
cout << "congradulations you win!" << endl;
cout << " =D\n" ;
}
else if ((Computertotalscore >= 100) && (Humantotalscore < 100))
{
cout << "sorry, you have lost" <<endl;
cout << " =(\n" ;
}
}
return 0;
}
int humanTurn(int & Humantotalscore)
{
int Humanscorethisturn = 0;
int Scoreperroll = 0;
char choice = 'r' ;
char roll = 'r' ;
Humanscorethisturn = 0;
while ((Scoreperroll != 1) && (choice == 'r' ) && (Humantotalscore <100))
{
Scoreperroll = (rand() %6 +1);
if (Humanscorethisturn == 0)
{
cout << "please press r to roll: " ;
cin >> roll;
}
if (Scoreperroll >= 2)
{
if (Humanscorethisturn ==0)
{
Humanscorethisturn = 0;
}
else if (Humanscorethisturn != 0)
{
Humanscorethisturn = Humanscorethisturn;
}
Humanscorethisturn = Scoreperroll + Humanscorethisturn;
cout << "You have rolled a " << Scoreperroll <<"\n" << "Your score this turn is: " << Humanscorethisturn << "\n" << " Please press r to roll again or h to hold: " ;
cin >> choice;
}
else
{
Humanscorethisturn = 0;
cout << "You have rolled a 1, End of turn\n" ;
}
}
Humantotalscore = Humantotalscore + Humanscorethisturn;
cout << "Your totall score is: " << Humantotalscore << "\n" ;
return (Humantotalscore);
}
int computerTurn(int & Computertotalscore)
{
int Computersscorethisturn = 0;
int Scoreperroll = 0;
char choice = 'r' ;
Computersscorethisturn = 0;
while ((Computertotalscore < 100) && (choice == 'r' ) && (Scoreperroll != 1) && (Computersscorethisturn < 20))
{
Scoreperroll = (rand() %6 +1);
if (Scoreperroll >= 2)
{
if (Computersscorethisturn == 0)
{
Computersscorethisturn = 0;
}
else if (Computersscorethisturn != 0)
{
Computersscorethisturn = Computersscorethisturn;
}
Computersscorethisturn = Computersscorethisturn + Scoreperroll;
cout << "the computer has rolled a: " << Scoreperroll << "\n" << "the computer's score this turn is: " << Computersscorethisturn << "\n" ;
}
else if (Scoreperroll == 1)
{
Computersscorethisturn = 0;
cout << "The computer has rolled a 1, End of turn\n" ;
}
}
Computertotalscore = Computertotalscore + Computersscorethisturn;
cout << "The computer's total score is: " << Computertotalscore <<"\n" ;
return (Computertotalscore);
}
Oct 24, 2011 at 3:56am UTC
Oct 24, 2011 at 3:58am UTC
this question has already been solved on the original post by Disch
thanks again btw =D
Topic archived. No new replies allowed.