Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Rock Paper Scissors.cpp : Defines the entry point for the console application.
- //
- /*
- This is a test of the mersenne twister PRNG and how a PRNG would do in the game of Rock, paper, scissors.
- Please try this yourself and make modifications as you see fit.
- This program comes with no guarantees and no claim of ownership
- Mersenne twister: http://en.wikipedia.org/wiki/Mersenne_twister
- */
- #include "stdafx.h" // Not used
- #include <random>
- #include <cmath>
- #include <string>
- #include <iostream>
- #include <iomanip>
- using namespace std;
- int getRandomNumber()
- {
- // Seed with a real random value, if available
- random_device rd;
- // Choose a random number between 1 and 3
- mt19937 e1(rd()); // The mersenne twister implementation
- uniform_int_distribution<int> uniform_dist(1, 3);
- return uniform_dist(e1);
- }
- class RPS
- {
- public:
- const enum outcome { ROCK = 1, PAPER, SCISSORS };
- RPS() : rps(getRandomNumber()) {};
- const bool operator== (const RPS &r1) { return this->rps == r1.rps; }
- const bool operator== (int r) { return this->rps == r; }
- const bool operator< (const RPS &r1) { return this->rps < r1.rps; }
- const bool operator> (const RPS &r1) { return this->rps > r1.rps; }
- // Makes it possible to print the result with text if we want that
- // Maybe we want to save the results in a txt file in the future
- string result()
- {
- switch (rps)
- {
- case RPS::outcome::ROCK:
- return "ROCK";
- break;
- case RPS::outcome::PAPER:
- return "PAPER";
- break;
- case RPS::outcome::SCISSORS:
- return "SCISSOR";
- break;
- default:
- return "Unknown value!";
- break;
- }
- }
- int value() { return rps; }
- private:
- int rps;
- };
- int _tmain(int argc, _TCHAR* argv[]) // main()
- {
- static int total = 1000000;
- int rock = 0, paper = 0, scissor = 0;
- for (int i = 0; i < total; i++)
- {
- RPS * r1 = new RPS();
- RPS * r2 = new RPS();
- if (*r1 < *r2)
- {
- // r1 < r2 means r1 could be rock or paper
- if (*r1 == RPS::outcome::ROCK)
- {
- if (*r2 == RPS::outcome::SCISSORS)
- ++rock;
- else if (*r2 == RPS::outcome::PAPER)
- ++paper;
- }
- else
- ++scissor;
- }
- else if (*r1 > *r2)
- {
- // r1 > r2 means r1 could be scissor or paper
- if (*r1 == RPS::outcome::SCISSORS)
- {
- if (*r2 == RPS::outcome::ROCK)
- ++rock;
- else if (*r2 == RPS::outcome::PAPER)
- ++scissor;
- }
- else
- ++paper;
- }
- else if (*r1 == *r2)
- {
- // Ties are not interesting to this study
- delete r1, r2;
- continue;
- }
- delete r1, r2;
- }
- double rockPerc = ((double)rock / (double)total) * 100;
- double paperPerc = ((double)paper / (double)total) * 100;
- double scissorPerc = ((double)scissor / (double)total) * 100;
- cout.precision(5);
- cout << fixed;
- cout << setw(9) << "Rock: " << rockPerc << "%" << endl;
- cout << setw(9) << "Paper: " << paperPerc << "%" << endl;
- cout << setw(9) << "Scissor: " << scissorPerc << "%" << endl;
- // Press enter to exit
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement