Advertisement
fimas

RPS with PRNG

May 18th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. // Rock Paper Scissors.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. /*
  5.     This is a test of the mersenne twister PRNG and how a PRNG would do in the game of Rock, paper, scissors.
  6.     Please try this yourself and make modifications as you see fit.
  7.  
  8.     This program comes with no guarantees and no claim of ownership
  9.  
  10.     Mersenne twister: http://en.wikipedia.org/wiki/Mersenne_twister
  11. */
  12.  
  13. #include "stdafx.h" // Not used
  14.  
  15. #include <random>
  16. #include <cmath>
  17. #include <string>
  18. #include <iostream>
  19. #include <iomanip>
  20.  
  21. using namespace std;
  22.  
  23. int getRandomNumber()
  24. {
  25.     // Seed with a real random value, if available
  26.     random_device rd;
  27.  
  28.     // Choose a random number between 1 and 3
  29.     mt19937 e1(rd()); // The mersenne twister implementation
  30.     uniform_int_distribution<int> uniform_dist(1, 3);
  31.     return uniform_dist(e1);
  32. }
  33.  
  34. class RPS
  35. {
  36. public:
  37.     const enum outcome { ROCK = 1, PAPER, SCISSORS };
  38.  
  39.     RPS() : rps(getRandomNumber()) {};
  40.  
  41.     const bool operator== (const RPS &r1) { return this->rps == r1.rps; }
  42.     const bool operator== (int r) { return this->rps == r; }
  43.  
  44.     const bool operator< (const RPS &r1) { return this->rps < r1.rps; }
  45.  
  46.     const bool operator> (const RPS &r1) { return this->rps > r1.rps; }
  47.  
  48.     // Makes it possible to print the result with text if we want that
  49.     // Maybe we want to save the results in a txt file in the future
  50.     string result()
  51.     {
  52.         switch (rps)
  53.         {
  54.         case RPS::outcome::ROCK:
  55.             return "ROCK";
  56.             break;
  57.         case RPS::outcome::PAPER:
  58.             return "PAPER";
  59.             break;
  60.         case RPS::outcome::SCISSORS:
  61.             return "SCISSOR";
  62.             break;
  63.         default:
  64.             return "Unknown value!";
  65.             break;
  66.         }
  67.     }
  68.  
  69.     int value() { return rps; }
  70. private:
  71.     int rps;
  72. };
  73.  
  74. int _tmain(int argc, _TCHAR* argv[]) // main()
  75. {
  76.     static int total = 1000000;
  77.     int rock = 0, paper = 0, scissor = 0;
  78.     for (int i = 0; i < total; i++)
  79.     {
  80.         RPS * r1 = new RPS();
  81.         RPS * r2 = new RPS();
  82.  
  83.         if (*r1 < *r2)
  84.         {
  85.             // r1 < r2 means r1 could be rock or paper
  86.             if (*r1 == RPS::outcome::ROCK)
  87.             {
  88.                 if (*r2 == RPS::outcome::SCISSORS)
  89.                     ++rock;
  90.                 else if (*r2 == RPS::outcome::PAPER)
  91.                     ++paper;
  92.             }
  93.             else
  94.                 ++scissor;
  95.         }
  96.         else if (*r1 > *r2)
  97.         {
  98.             // r1 > r2 means r1 could be scissor or paper
  99.             if (*r1 == RPS::outcome::SCISSORS)
  100.             {
  101.                 if (*r2 == RPS::outcome::ROCK)
  102.                     ++rock;
  103.                 else if (*r2 == RPS::outcome::PAPER)
  104.                     ++scissor;
  105.             }
  106.             else
  107.                 ++paper;
  108.         }
  109.         else if (*r1 == *r2)
  110.         {
  111.             // Ties are not interesting to this study
  112.             delete r1, r2;
  113.             continue;
  114.         }
  115.  
  116.         delete r1, r2;
  117.     }
  118.  
  119.     double rockPerc = ((double)rock / (double)total) * 100;
  120.     double paperPerc = ((double)paper / (double)total) * 100;
  121.     double scissorPerc = ((double)scissor / (double)total) * 100;
  122.  
  123.     cout.precision(5);
  124.     cout << fixed;
  125.  
  126.     cout << setw(9) << "Rock: "    << rockPerc    << "%" << endl;
  127.     cout << setw(9) << "Paper: "   << paperPerc   << "%" << endl;
  128.     cout << setw(9) << "Scissor: " << scissorPerc << "%" << endl;
  129.  
  130.     // Press enter to exit
  131.     cin.get();
  132.  
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement