-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // Random number generators | ||
| // Rolling a dice 100 times | ||
| // Chris Bass | ||
| // November 2014 | ||
|
|
||
| #include <iostream> | ||
| #include <cstdlib> | ||
| #include <ctime> | ||
| #include <fstream> | ||
| #include <string> | ||
| using namespace std; | ||
|
|
||
| void diceGame() | ||
| { | ||
| int count1 = 0; // count of 1s rolled | ||
| int count2 = 0; // count of 2s rolled | ||
| int count3 = 0; // count of 3s rolled | ||
| int count4 = 0; // count of 4s rolled | ||
| int count5 = 0; // count of 5s rolled | ||
| int count6 = 0; // count of 6s rolled | ||
|
|
||
| srand(time(0)); // seed the pseudo random number generator | ||
|
|
||
| for (int roll = 1; roll <= 100; roll++) // 100 rolls | ||
| { | ||
| int diceRoll; | ||
| diceRoll = 1 + rand() % 6; // store a random number between 1 and 6 | ||
|
|
||
| switch (diceRoll) | ||
| { | ||
| case 1: | ||
| ++count1; | ||
| break; | ||
| case 2: | ||
| ++count2; | ||
| break; | ||
| case 3: | ||
| ++count3; | ||
| break; | ||
| case 4: | ||
| ++count4; | ||
| break; | ||
| case 5: | ||
| ++count5; | ||
| break; | ||
| case 6: | ||
| ++count6; | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
| ifstream inputFileStream; | ||
| inputFileStream.open("output.txt"); | ||
| int highscore; | ||
| if (inputFileStream.good()) { | ||
| inputFileStream >> highscore; | ||
| } else { | ||
| cout << "cannot find previous highscore textfile, lets create a new one!" << endl; | ||
| highscore = 0; | ||
| } | ||
|
|
||
| cout << "You have rolled " << count1 << " ones" << endl; | ||
| cout << "You have rolled " << count2 << " twos" << endl; // count of 1s rolled | ||
| cout << "You have rolled " << count3 << " threes" << endl; // count of 1s rolled | ||
| cout << "You have rolled " << count4 << " fours" << endl; // count of 1s rolled | ||
| cout << "You have rolled " << count5 << " fives" << endl; // count of 1s rolled | ||
| cout << "You have rolled " << count6 << " sixes" << endl; // count of 1s rolled | ||
|
|
||
| cout << "Your score this game is: " << | ||
| count1 * 1 + count2 * 2 + count3 * 3 + | ||
| count4 * 4 + count5 * 5 + count6 * 6 << endl; | ||
| int currentScore = count1 * 1 + count2 * 2 + count3 * 3 + | ||
| count4 * 4 + count5 * 5 + count6 * 6; | ||
|
|
||
| cout << "Your Score is " <<currentScore<< endl; | ||
| { | ||
| cout << "The previous highscore is: " << highscore << endl; | ||
|
|
||
| ofstream outputFileStream; | ||
| if (currentScore > highscore) { | ||
| outputFileStream.open("output.txt"); | ||
| outputFileStream << currentScore << endl; | ||
| outputFileStream.close(); | ||
|
|
||
| cout << "The new highscore is: " << currentScore << endl; | ||
| } | ||
| } | ||
|
|
||
| } |