Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
aa6164 committed Oct 7, 2024
1 parent d39d2c9 commit 12f6c4a
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions diceGame.cpp
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;
}
}

}

0 comments on commit 12f6c4a

Please sign in to comment.