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
|
#include <cstdlib>
#include <iostream>
#include <time.h>
int sidesOnDice, enemyRoll, userRoll;
int numberOfRolls, numberOfHits, numberOfMisses;
char continueRolling, continueRolling1, rollResult;
using namespace std;
int rollDice(int sidesOnDice)
{
int a = rand() % sidesOnDice + 1;
return a;
}
char displayResult(int enemyRoll, int userRoll)
{
cout << "Enemy rolled: " << enemyRoll << endl;
cout << "User rolled: " << userRoll << endl;
if(userRoll>= enemyRoll)
{
char rollResult = 'h';
cout << "Hit!" << endl;
return rollResult;
}
if(userRoll< enemyRoll)
{
char rollResult = 'm';
cout << "Miss!" << endl;
return rollResult;
}
return rollResult;
}
int rollDice(int sidesOnDice, int numberOfHits, int numberOfMisses)
{
numberOfRolls ++;
enemyRoll = rollDice(sidesOnDice);
userRoll = rollDice(sidesOnDice);
rollResult = displayResult(enemyRoll, userRoll);
if(rollResult== 'h')
{
numberOfHits++;
}
if(rollResult== 'm')
{
numberOfMisses++;
}
return numberOfHits;
return numberOfMisses;
}
void displayEndResults(int numberOfHits, int numberOfMisses)
{
cout << "Total Hits: " << numberOfHits << endl;
cout << "Total Misses: " << numberOfMisses << endl;
}
int main(int argc, char *argv[])
{
do
{
cout << "Welcome to the dice roller." << endl;
srand(time(NULL));
numberOfHits = 0;
numberOfMisses = 0;
cout << "How many sides are on the dice?" << endl;
cin >> sidesOnDice;
do
{
rollDice(sidesOnDice, numberOfHits, numberOfMisses);
cout << "Would you like to continue this set?" << endl;
cout << "'y' or 'n'" << endl;
cin >> continueRolling;
}while(continueRolling!= 'n');
displayEndResults(numberOfHits, numberOfMisses);
cout << "Would you like to continue rolling?" << endl;
cout << "'y' or 'n'" << endl;
cin >> continueRolling1;
}while(continueRolling1!= 'n');
return EXIT_SUCCESS;
}
|