From 12f6c4aad5566a53e22976aff7aa107e26a943cb Mon Sep 17 00:00:00 2001 From: "Chris Bass (aa6164)" Date: Mon, 7 Oct 2024 13:05:55 +0100 Subject: [PATCH] Add files via upload --- diceGame.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 diceGame.cpp diff --git a/diceGame.cpp b/diceGame.cpp new file mode 100644 index 0000000..4f883de --- /dev/null +++ b/diceGame.cpp @@ -0,0 +1,90 @@ +// Random number generators +// Rolling a dice 100 times +// Chris Bass +// November 2014 + +#include +#include +#include +#include +#include +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 " < highscore) { + outputFileStream.open("output.txt"); + outputFileStream << currentScore << endl; + outputFileStream.close(); + + cout << "The new highscore is: " << currentScore << endl; + } + } + +}