home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / pascal / rehack / general / rolldice.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-27  |  1.4 KB  |  62 lines

  1. //    File:                ROLLDICE.CPP
  2. //    Path:                ...\REHACK\GENERAL\ROLLDICE.CPP
  3. //    Version:            1.00
  4. //    Author:                V. James Krammes
  5. //    CIS Id:                75300,1663
  6. //    Created on:            23 June, 1993
  7. //    Modified on:        23 June, 1993
  8. //    Description:        Code for the rollDice() function.
  9. //                        rollDice() returns an integer value from an AD&D-
  10. //                        type dice specifier.
  11. //    Tabs:                4
  12.  
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include "..\general\rolldice.hpp"
  18.  
  19. int rollDice( const char far *specifier )
  20. {
  21.  
  22.     int value=0,numDice,dieSize,adjust,i;
  23.     char *whatsLeft;
  24.     char *s = new char[strlen(specifier)+1];
  25.  
  26.     strcpy( s,specifier );
  27.     strupr( s );
  28.     for (i = 0; i < strlen( s ); i++)
  29.         if (!isdigit( s[i] ) && !strchr( "+-D", s[i] ))
  30.             return badRollValue;
  31.     if (!strchr( s, 'D' ))
  32.         return badRollValue;
  33.     numDice = atoi( strtok( s, "D" ) );
  34.     if (!numDice)
  35.         return badRollValue;
  36.     dieSize = atoi( strtok( NULL, "+-" ) );
  37.     if (!dieSize)
  38.         return badRollValue;
  39.     whatsLeft = strtok( NULL, "\0" );
  40.     if (!strlen( whatsLeft ))
  41.         adjust = 0;
  42.     else
  43.         if (!strchr( whatsLeft, 'D' ))
  44.             adjust = atoi( whatsLeft );
  45.         else
  46.             adjust = rollDice( whatsLeft );
  47.     for (i = 0; i < strlen(specifier); i++)
  48.         if (specifier[i] == '+')
  49.             break;
  50.         else if (specifier[i] == '-')
  51.         {
  52.             adjust = -adjust;
  53.             break;
  54.         }
  55.     while (numDice--)
  56.         value += random( dieSize ) + 1;
  57.     value += adjust;
  58.     delete s;
  59.     return value;
  60.  
  61. }
  62.