IamLupo

Warhammer | Dice Rolls

Feb 27th, 2016
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. /*
  2.     Compile: g++ main.cpp -o main --std=c++11 -O2
  3. */
  4.  
  5. #include <iostream>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <math.h>
  9. #include <numeric>
  10. #include <fstream>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <time.h>
  14.  
  15. using namespace std;
  16.  
  17. enum dices { skaven = 6, chaos = 4, cursed = 3, bone = 2 };
  18.  
  19. int roll(const vector<int> &s) {
  20.     int i, r;
  21.    
  22.     r = 0;
  23.  
  24.     for(i = 0; i < s.size(); i++) {
  25.         //Roll dices
  26.         if(rand() % 6 + 1 <= s[i])
  27.             r++;
  28.     }
  29.    
  30.     return r;
  31. }
  32.  
  33. vector<int> staticsRoll(const vector<int> &s) {
  34.     int i;
  35.     vector<int> r(s.size() + 1, 0);
  36.    
  37.     for(i = 0; i < 1000000; i++)
  38.         r[roll(s)]++;
  39.    
  40.     return r;
  41. }
  42.  
  43. void drawResult(const vector<int> &s, const vector<int> &r) {
  44.     int i;
  45.    
  46.     cout << "Settings: ";
  47.     for(i = 0; i < s.size(); i++) {
  48.         switch(s[i]) {
  49.             case dices::skaven: cout << "Skaven dice";  break;
  50.             case dices::chaos:  cout << "Chaos dice";   break;
  51.             case dices::cursed: cout << "Cursed dice";  break;
  52.             case dices::bone:   cout << "Bone dice";    break;
  53.         }
  54.        
  55.         if(i + 1 != s.size())
  56.             cout << ", ";
  57.     }
  58.     cout << endl;
  59.    
  60.     for(i = 0; i < r.size(); i++) {
  61.         cout << i << " = " << r[i] / 100 / 100.0 << "%" << endl;
  62.     }
  63.     cout << endl;
  64. }
  65.  
  66. int main() {
  67.     vector<int> settings, result;
  68.  
  69.     srand(time(NULL));
  70.    
  71.     settings = {
  72.         dices::skaven, dices::skaven, dices::chaos, dices::chaos,
  73.         dices::chaos, dices::cursed, dices::cursed
  74.     };
  75.     result = staticsRoll(settings);
  76.     drawResult(settings, result);
  77.    
  78.     /* ------------------------------------ */
  79.    
  80.     settings = {
  81.         dices::skaven, dices::skaven, dices::chaos, dices::chaos,
  82.         dices::chaos, dices::bone, dices::bone
  83.     };
  84.     result = staticsRoll(settings);
  85.     drawResult(settings, result);
  86.    
  87.     /* ------------------------------------ */
  88.    
  89.     settings = {
  90.         dices::skaven, dices::chaos, dices::chaos, dices::chaos,
  91.         dices::bone, dices::bone, dices::bone
  92.     };
  93.     result = staticsRoll(settings);
  94.     drawResult(settings, result);
  95.    
  96.     /* ------------------------------------ */
  97.    
  98.     settings = {
  99.         dices::bone, dices::bone, dices::bone, dices::bone,
  100.         dices::bone, dices::bone, dices::bone
  101.     };
  102.     result = staticsRoll(settings);
  103.     drawResult(settings, result);
  104.    
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment