home *** CD-ROM | disk | FTP | other *** search
- // File: ROLLDICE.CPP
- // Path: ...\REHACK\GENERAL\ROLLDICE.CPP
- // Version: 1.00
- // Author: V. James Krammes
- // CIS Id: 75300,1663
- // Created on: 23 June, 1993
- // Modified on: 23 June, 1993
- // Description: Code for the rollDice() function.
- // rollDice() returns an integer value from an AD&D-
- // type dice specifier.
- // Tabs: 4
-
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include "..\general\rolldice.hpp"
-
- int rollDice( const char far *specifier )
- {
-
- int value=0,numDice,dieSize,adjust,i;
- char *whatsLeft;
- char *s = new char[strlen(specifier)+1];
-
- strcpy( s,specifier );
- strupr( s );
- for (i = 0; i < strlen( s ); i++)
- if (!isdigit( s[i] ) && !strchr( "+-D", s[i] ))
- return badRollValue;
- if (!strchr( s, 'D' ))
- return badRollValue;
- numDice = atoi( strtok( s, "D" ) );
- if (!numDice)
- return badRollValue;
- dieSize = atoi( strtok( NULL, "+-" ) );
- if (!dieSize)
- return badRollValue;
- whatsLeft = strtok( NULL, "\0" );
- if (!strlen( whatsLeft ))
- adjust = 0;
- else
- if (!strchr( whatsLeft, 'D' ))
- adjust = atoi( whatsLeft );
- else
- adjust = rollDice( whatsLeft );
- for (i = 0; i < strlen(specifier); i++)
- if (specifier[i] == '+')
- break;
- else if (specifier[i] == '-')
- {
- adjust = -adjust;
- break;
- }
- while (numDice--)
- value += random( dieSize ) + 1;
- value += adjust;
- delete s;
- return value;
-
- }