Advertisement
jacknpoe

Rolling dices (rolando dados) RPG (GURPS, D&D, Storyteller)

Jul 2nd, 2014
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.70 KB | None | 0 0
  1. /*########################### RPG DICE ROLLER (VERSION 0.1 2014-07-01)
  2. /
  3. */
  4.  
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <ctime>
  8.  
  9. namespace dices {
  10.  
  11. /* ########################### DUNGEONS AND DRAGONS
  12. */
  13.     class DungeonsAndDragons {
  14.     protected:
  15.         unsigned short DicesSize;       // d4, d6, d8, d10, d100, d12, d20, dX
  16.         unsigned short QuantDices;      // number of (DicesSize) to roll
  17.         short Modifier;     // modifier
  18.         unsigned short *Dices;      // dices after roll
  19.     public:
  20.         DungeonsAndDragons( unsigned short size = 20, unsigned short quant = 1, short mod = 0);
  21.         ~DungeonsAndDragons();
  22.  
  23.         bool setDicesSize( unsigned short size);
  24.         unsigned short getDicesSize( void);
  25.  
  26.         bool setQuantDices( unsigned short quant);
  27.         unsigned short getQuantDices( void);
  28.  
  29.         bool setModifier( short mod);
  30.         short getModifier( void);
  31.  
  32.         unsigned short getDice( unsigned short dice);
  33.  
  34.         bool rollDices( void);
  35.  
  36.         short getTotal( void);
  37.     };
  38.  
  39.     DungeonsAndDragons::DungeonsAndDragons( unsigned short size, unsigned short quant, short mod){
  40.         if( quant < 0 ) quant = 0;
  41.         Dices = ( unsigned short *) std::malloc( sizeof( unsigned short) * quant);
  42.         QuantDices = ( Dices == NULL ? 0 : quant);
  43.         DicesSize = size;
  44.         Modifier = mod;
  45.         for( unsigned short index = 0; index < quant; index++) Dices[ index] = 0;
  46.     }
  47.  
  48.     bool DungeonsAndDragons::setDicesSize( unsigned short size){
  49.         if( size < 2) return false;
  50.         DicesSize = size; return true;
  51.     }
  52.     unsigned short DungeonsAndDragons::getDicesSize( void){ return DicesSize; }
  53.  
  54.     bool DungeonsAndDragons::setQuantDices( unsigned short quant){
  55.         if( quant < 0) return false;
  56.         Dices = ( unsigned short *) std::realloc( Dices, sizeof( unsigned short) * quant);
  57.         for( short index = QuantDices; index < quant; index++) Dices[ index] = 0;
  58.         QuantDices = ( Dices == NULL ? 0 : quant);
  59.         return true;
  60.     }
  61.     unsigned short DungeonsAndDragons::getQuantDices( void){ return QuantDices; }
  62.  
  63.     bool DungeonsAndDragons::setModifier( short mod){
  64.         Modifier = mod; return true;
  65.     }
  66.     short DungeonsAndDragons::getModifier( void){ return Modifier; }
  67.  
  68.     unsigned short DungeonsAndDragons::getDice( unsigned short dice){
  69.         if( ( dice < 0) or ( dice >= QuantDices) ) return 0;
  70.         return Dices[ dice];
  71.     }
  72.  
  73.     bool DungeonsAndDragons::rollDices( void){
  74.         if( ( QuantDices < 1) or ( DicesSize < 2)) return false;
  75.         for( unsigned short index = 0; index < QuantDices; index++) Dices[ index] = std::rand() % DicesSize + 1;
  76.         return true;
  77.     }
  78.  
  79.     short DungeonsAndDragons::getTotal( void){
  80.         short temp = 0;
  81.         for( unsigned short index = 0; index < QuantDices; index++) temp += Dices[ index];
  82.         temp += Modifier;
  83.         return temp;
  84.     }
  85.  
  86.     DungeonsAndDragons::~DungeonsAndDragons(){ std::free( Dices); }
  87.  
  88.  
  89. /* ########################### GURPS
  90. */
  91.     class GURPS {
  92.     protected:
  93.         unsigned short QuantDices;      // number of D6 to roll
  94.         short Modifier;     // modifier
  95.         unsigned short *Dices;      // dices after roll
  96.     public:
  97.         GURPS( unsigned short quant = 1, short mod = 0);
  98.         ~GURPS();
  99.  
  100.         bool setQuantDices( unsigned short quant);
  101.         unsigned short getQuantDices( void);
  102.  
  103.         bool setModifier( short mod);
  104.         short getModifier( void);
  105.  
  106.         unsigned short getDice( unsigned short dice);
  107.  
  108.         bool rollDices( void);
  109.  
  110.         short getTotal( void);
  111.     };
  112.  
  113.     GURPS::GURPS( unsigned short quant, short mod){
  114.         if( quant < 0 ) quant = 0;
  115.         Dices = ( unsigned short *) std::malloc( sizeof( unsigned short) * quant);
  116.         QuantDices = ( Dices == NULL ? 0 : quant);
  117.         Modifier = mod;
  118.         for( unsigned short index = 0; index < quant; index++) Dices[ index] = 0;
  119.     }
  120.  
  121.     bool GURPS::setQuantDices( unsigned short quant){
  122.         if( quant < 0) return false;
  123.         Dices = ( unsigned short *) std::realloc( Dices, sizeof( unsigned short) * quant);
  124.         for( short index = QuantDices; index < quant; index++) Dices[ index] = 0;
  125.         QuantDices = ( Dices == NULL ? 0 : quant);
  126.         return true;
  127.     }
  128.     unsigned short GURPS::getQuantDices( void){ return QuantDices; }
  129.  
  130.     bool GURPS::setModifier( short mod){
  131.         Modifier = mod; return true;
  132.     }
  133.     short GURPS::getModifier( void){ return Modifier; }
  134.  
  135.     unsigned short GURPS::getDice( unsigned short dice){
  136.         if( ( dice < 0) or ( dice >= QuantDices) ) return 0;
  137.         return Dices[ dice];
  138.     }
  139.  
  140.     bool GURPS::rollDices( void){
  141.         if( QuantDices < 1) return false;
  142.         for( unsigned short index = 0; index < QuantDices; index++) Dices[ index] = std::rand() % 6 + 1;
  143.         return true;
  144.     }
  145.  
  146.     short GURPS::getTotal( void){
  147.         short temp = 0;
  148.         for( unsigned short index = 0; index < QuantDices; index++) temp += Dices[ index];
  149.         temp += Modifier;
  150.         return temp;
  151.     }
  152.  
  153.     GURPS::~GURPS(){ std::free( Dices); }
  154.  
  155.  
  156. /* ########################### STORYTELLER
  157. */
  158.     class Storyteller {
  159.     protected:
  160.         unsigned short QuantDices;      // number of D10 to roll
  161.         short Difficulty;       // difficulty
  162.         unsigned short *Dices;      // dices after roll
  163.     public:
  164.         Storyteller( unsigned short quant = 1, short dif = 0);
  165.         ~Storyteller();
  166.  
  167.         bool setQuantDices( unsigned short quant);
  168.         unsigned short getQuantDices( void);
  169.  
  170.         bool setDifficulty( short dif);
  171.         short getDifficulty( void);
  172.  
  173.         unsigned short getDice( unsigned short dice);
  174.  
  175.         bool rollDices( void);
  176.  
  177.         short getTotal( void);
  178.     };
  179.  
  180.     Storyteller::Storyteller( unsigned short quant, short dif){
  181.         if( quant < 0 ) quant = 0;
  182.         Dices = ( unsigned short *) std::malloc( sizeof( unsigned short) * quant);
  183.         QuantDices = ( Dices == NULL ? 0 : quant);
  184.         Difficulty = dif;
  185.         for( unsigned short index = 0; index < quant; index++) Dices[ index] = 0;
  186.     }
  187.  
  188.     bool Storyteller::setQuantDices( unsigned short quant){
  189.         if( quant < 0) return false;
  190.         Dices = ( unsigned short *) std::realloc( Dices, sizeof( unsigned short) * quant);
  191.         for( short index = QuantDices; index < quant; index++) Dices[ index] = 0;
  192.         QuantDices = ( Dices == NULL ? 0 : quant);
  193.         return true;
  194.     }
  195.     unsigned short Storyteller::getQuantDices( void){ return QuantDices; }
  196.  
  197.     bool Storyteller::setDifficulty( short dif){
  198.         if( ( dif < 2) or ( dif > 10)) return false;
  199.         Difficulty = dif; return true;
  200.     }
  201.     short Storyteller::getDifficulty( void){ return Difficulty; }
  202.  
  203.     unsigned short Storyteller::getDice( unsigned short dice){
  204.         if( ( dice < 0) or ( dice >= QuantDices) ) return 0;
  205.         return Dices[ dice];
  206.     }
  207.  
  208.     bool Storyteller::rollDices( void){
  209.         if( (QuantDices < 1) or ( Difficulty < 2) or ( Difficulty > 10)) return false;
  210.         for( unsigned short index = 0; index < QuantDices; index++) Dices[ index] = std::rand() % 10 + 1;
  211.         return true;
  212.     }
  213.  
  214.     short Storyteller::getTotal( void){
  215.         short temp = 0;
  216.         for( unsigned short index = 0; index < QuantDices; index++)
  217.         {
  218.             if( Dices[ index] == 1) temp -= 1;
  219.             if( Dices[ index] >= Difficulty) temp += 1;
  220.         }
  221.         return temp;
  222.     }
  223.  
  224.     Storyteller::~Storyteller(){ std::free( Dices); }
  225.  
  226.  
  227. /* ########################### INTERFACE
  228. */
  229.     class Interface {
  230.     protected:
  231.         void ExecuteDungeonsAndDragons( void);      // Execute D&D interface
  232.         void ExecuteGURPS( void);       // Execute GURPS interface
  233.         void ExecuteStoryteller( void);     // Execute Storyteller interface
  234.     public:
  235.         void Execute( void);
  236.     };
  237.  
  238.     void Interface::Execute( void){
  239.         unsigned short choice;
  240.  
  241.         while( true)
  242.         {
  243.             std::cout << "=====================" << std::endl;
  244.             std::cout << "PLEASE, SELECT A GAME" << std::endl;
  245.             std::cout << "=====================" << std::endl;
  246.             std::cout << "1. Dungeon & Dragons" << std::endl;
  247.             std::cout << "2. GURPS" << std::endl;
  248.             std::cout << "3. Storyteller" << std::endl;
  249.             std::cout << "OTHER. Exit" << std::endl;
  250.             std::cout << "=====================" << std::endl;
  251.             std::cout << "Choice: ";
  252.    
  253.             std::cin >> choice;
  254.  
  255.             switch( choice){
  256.                 case 1:
  257.                     ExecuteDungeonsAndDragons();
  258.                     break;
  259.                 case 2:
  260.                     ExecuteGURPS();
  261.                     break;
  262.                 case 3:
  263.                     ExecuteStoryteller();
  264.                     break;
  265.                 default:
  266.                     return;
  267.             }
  268.             std::cout << std::endl;
  269.         }
  270.     }
  271.  
  272.     void Interface::ExecuteDungeonsAndDragons( void){
  273.         unsigned short size, quant;
  274.         short modifier;
  275.         DungeonsAndDragons dice( 1, 1, 1);
  276.        
  277.         while( true)
  278.         {
  279.             std::cout << "---------------------" << std::endl;
  280.             std::cout << "Size: ";
  281.             std::cin >> size;
  282.    
  283.             if( size < 2) { std::cout << "Size lower than two." << std::endl; break; }
  284.    
  285.             std::cout << "Quantity: ";
  286.             std::cin >> quant;
  287.    
  288.             if( quant < 1) { std::cout << "Quantity lower than one." << std::endl; break; }
  289.    
  290.             std::cout << "Modifier: ";
  291.             std::cin >> modifier;
  292.        
  293.             dice.setDicesSize( size);
  294.             dice.setQuantDices( quant);
  295.             dice.setModifier( modifier);
  296.    
  297.             if( ! dice.rollDices()) { std::cout << "Fail rolling dices." << std::endl; break; }
  298.        
  299.             std::cout << std::endl << "Dices:";
  300.             for( unsigned short index = 0; index < dice.getQuantDices(); index++)
  301.                 std::cout << " " << dice.getDice( index);
  302.             std::cout << std::endl << "Result: " << dice.getTotal() << std::endl;
  303.         }
  304.     }
  305.  
  306.     void Interface::ExecuteGURPS( void){
  307.         unsigned short quant;
  308.         short modifier;
  309.         GURPS dice( 1, 1);
  310.        
  311.         while( true)
  312.         {
  313.             std::cout << "---------------------" << std::endl;
  314.             std::cout << "Quantity: ";
  315.             std::cin >> quant;
  316.    
  317.             if( quant < 1) { std::cout << "Quantity lower than one." << std::endl; break; }
  318.    
  319.             std::cout << "Modifier: ";
  320.             std::cin >> modifier;
  321.        
  322.             dice.setQuantDices( quant);
  323.             dice.setModifier( modifier);
  324.    
  325.             if( ! dice.rollDices()) { std::cout << "Fail rolling dices." << std::endl; break; }
  326.        
  327.             std::cout << std::endl << "Dices:";
  328.             for( unsigned short index = 0; index < dice.getQuantDices(); index++)
  329.                 std::cout << " " << dice.getDice( index);
  330.             std::cout << std::endl << "Result: " << dice.getTotal() << std::endl;
  331.         }
  332.     }
  333.  
  334.     void Interface::ExecuteStoryteller( void){
  335.         unsigned short quant, difficulty;
  336.         Storyteller dice( 1, 1);
  337.        
  338.         while( true)
  339.         {
  340.             std::cout << "---------------------" << std::endl;
  341.             std::cout << "Quantity: ";
  342.             std::cin >> quant;
  343.    
  344.             if( quant < 1) { std::cout << "Quantity lower than one." << std::endl; break; }
  345.    
  346.             std::cout << "Difficulty: ";
  347.             std::cin >> difficulty;
  348.  
  349.             if( difficulty < 2) { std::cout << "Difficulty lower than two." << std::endl; break; }
  350.  
  351.             if( difficulty > 10) { std::cout << "Difficulty bigger than ten." << std::endl; break; }
  352.        
  353.             dice.setQuantDices( quant);
  354.             dice.setDifficulty( difficulty);
  355.    
  356.             if( ! dice.rollDices()) { std::cout << "Fail rolling dices." << std::endl; break; }
  357.        
  358.             std::cout << std::endl << "Dices:";
  359.             for( unsigned short index = 0; index < dice.getQuantDices(); index++)
  360.                 std::cout << " " << dice.getDice( index);
  361.             std::cout << std::endl << "Result: " << dice.getTotal() << std::endl;
  362.         }
  363.     }
  364. }
  365.  
  366. //using namespace dices;
  367.    
  368. int main( void){
  369.     dices::Interface myInterface;
  370.  
  371.     std::srand( std::time(0));      // GLOBAL. TODO: seed in the classes
  372.  
  373.     myInterface.Execute();
  374.  
  375.     return 0;
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement