Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- int humanTurn(int hTotal);
- int compTurn(int cTotal);
- int main()
- {
- //seedint the random number generator
- srand(time(0));
- int cTotal = 0; // keeos track of computer score
- int hTotal = 0; // keeos track of user score
- while(cTotal < 100 && hTotal < 100)
- {
- //human turn first
- int humanTurnScore = humanTurn(hTotal);
- hTotal += humanTurnScore;
- int compTurnScore = compTurn(cTotal);
- cTotal += compTurnScore;
- }
- if(hTotal >= 100)
- cout << "You won the game!!!";
- else
- cout << "The computer won the game.";
- return 0;
- }
- int humanTurn(int hTotal)
- {
- int turnTotal = 0;
- char input = 'r';
- while(input == 'r')
- {
- int face = 1 + rand()%6;
- cout << "You rolled " << face << endl;
- if(face == 1)
- {
- cout << "you lost your turn, you lose all your points for this turn";
- input = 'h';
- turnTotal = 0; //you lose all your accumulated turn points, turnTotal reset to zero
- }
- // you have not gotten 1, increment total and ask user to roll again
- else
- {
- turnTotal += face;
- cout << "do you want to roll again?" << endl;
- cin >> input;
- }
- }
- return turnTotal;
- }
- int compTurn(int cTotal)
- {
- int turnTotal = 0;
- while(turnTotal < 20)
- {
- int face = 1 *rand()%6;
- if(face == 1)
- return 0;
- else
- turnTotal += face;
- }
- cout << "computer gets " << turnTotal << " points this turn.\n"<< endl;
- return turnTotal;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement