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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
#include <iostream>
#include <cstdlib>
#include <ctime>
// Function Declarations
void DisplayMenu();
void Roller(int);
void Percentile();
int main()
{
srand(time(0)); // Initialize PRNG Seed
DisplayMenu(); // Enter "Main" Program Body
return 0;
}
void DisplayMenu()
{
int iChoice=0; // Integer Initialized To 0, Used To Retrieve Menu Option
// Begin Menu
std::cout << "1. 4-Sided Dice\n";
std::cout << "2. 6-Sided Dice\n";
std::cout << "3. 8-Sided Dice\n";
std::cout << "4. 10-Sided Dice\n";
std::cout << "5. 12-Sided Dice\n";
std::cout << "6. 20-Sided Dice\n";
std::cout << "7. % Dice\n";
std::cout << "8. Exit Dice Roller\n\n";
// End Menu
std::cout << "Choice: ";
std::cin >> iChoice; // Input Menu Choice
std::cout << "\n\n";
if(iChoice<1) // Internal Check, Used To Assure Valid Input
{
std::cout << "\n\nError: Invalid Choice\n\n"; // Tell User Input Was Not A Valid Option
std::cout << "Please choose a number between 1 and 8, corresponding to the menu option you would like to select.\n\n"; // Give User Acceptable Values
std::cout << "\n\n"; // Add Empty Lines For Aesthetic Appeal
DisplayMenu(); // Return To Top Of Menu Function
return;
}
else if(iChoice>8) // Internal Check, Used To Assure Valid Input
{
std::cout << "\n\nError: Invalid Choice\n\n"; // Tell User Input Was Not A Valid Option
std::cout << "Please choose a number between 1 and 8, corresponding to the menu option you would like to select.\n\n"; // Give User Acceptable Values
std::cout << "\n\n"; // Add Empty Lines For Aesthetic Appeal
DisplayMenu(); // Return To Top Of Menu Function
return;
}
/* This Section Commented Out
Wondering If:
if(iChoice<1||iChoice>8)
Would Be An Acceptable Substitute For The Two Previous Checks, Or If Only One Condition Should Be Checked Per "if" Statement
*/
else if(iChoice==7) // Percentile Dice Use Different Function. The Results Of Two Dice Must Be Added Differently
{
Percentile(); // Call Percentile Dice Function
return;
}
else if(iChoice==8) // Directs Program To Return With No Further Action, Or "Exit"
{
return;
}
else // Continue To Rolling Function
{
Roller(iChoice); // Call Roller Function
return;
}
return;
}
void Roller(int iChoice)
{
int iDice=0; // Integer Initialized To 0, Used To Determine Number Of Dice To Roll
int iSides=0; // Integer Initialized To 0, Used To Determine Number Of Sides On Selected Die
// Begin Setting Number Of Sides On Die
if(iChoice==1)
{
iSides=4;
}
else if(iChoice==2)
{
iSides=6;
}
else if(iChoice==3)
{
iSides=8;
}
else if(iChoice==4)
{
iSides=10;
}
else if(iChoice==5)
{
iSides=12;
}
else if(iChoice==6)
{
iSides=20;
}
// End Of Setting Number Of Sides On Die
// Begin Number Of Dice Being Rolled
std::cout << "\n\nHow many dice would you like to roll?\n\n";
std::cout << "Roll: ";
std::cin >> iDice;
std::cout << "\n\n";
// End Number Of Dice Being Rolled
do
{
std::cout << rand()%iSides+1; // Display Die Roll With iSides Deciding Which Die Is Rolled
std::cout <<"\n"; // Add Empty Line For Aesthetic Appeal
iDice=iDice-1; // Decrease Number Of Dice To Be Rolled
}
while(iDice>0); // Stop Rolling When Number Of Die Rolls Reaches 0
std::cout << "\n\n"; // Add Empty Lines For Aesthetic Appeal
DisplayMenu(); // Return To Menu Function
return;
}
void Percentile()
{
int iDice=0; // Integer Initialized To 0, Used To Determine Number Of Dice To Roll
int iTens=0; // Integer Initialized To 0, Used To Hold First Die Roll - Tens Digit
int iOnes=0; // Integer Initialized To 0, Used To Hold Second Die Roll - Ones Digit
int iResult=0; // Integer Initialized To 0, Used To Hold Total Of Both Dice
// Begin Number Of Dice Being Rolled
std::cout << "\n\nHow many dice would you like to roll?\n\n";
std::cout << "Roll: ";
std::cin >> iDice;
std::cout << "\n\n";
// End Number Of Dice Being Rolled
do
{
iTens=rand()%11; // iTens Is Rolled To A Number Between 0 And 10
iOnes=rand()%10; // iOnes Is Rolled To A Number Between 0 And 9
if(iTens==10) // Internal Check, Used To Decide If Percent Is At 100, Or Max Value
{
iResult=100; // Set Result To Max Percent Value
}
else
{
iResult=iTens+iOnes; // Add Tens Digit And Ones Digit To Get Total Percent Value
}
std::cout << iResult; // Display Result
std::cout << "%\n"; // Add % Symbol To Displayed Value And Add Empty Line For Aesthetic Appeal
iDice=iDice-1; // Decrease Number Of Dice To Be Rolled
}
while(iDice>0); // Stop Rolling When Number Of Dice To Be Rolled Reaches 0
std::cout << "\n\n"; // Add Empty Lines For Aesthetic Appeal
DisplayMenu(); // Return To Menu Function
return;
}
| |