/*Dice Roller V1.0Robson<http://iceyboard.no-ip.org>Released under the GNU General Public License<http://www.gnu.org/copyleft/gpl.html>*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <sys/timeb.h>usingnamespacestd;// this code determines if the user requested an addition or subtractionintic_Modifier(char*arg){// additionif(strchr(arg,'+')){// remove everything before the plus sign strtok(arg,"+");// return everything at the endreturnatoi(strtok(NULL,"+"));}// subtractionelseif(strchr(arg,'-')){// remove everything before the plus sign strtok(arg,"-");// return everything at the endreturn0-atoi(strtok(NULL,"-"));}// nothingelse// the user didn't specify a modifier, so return zeroreturn0;}// ensure randomness of numbers// this uses milliseconds so quick restarts don't produce the same rolls// if this doesn't work for you, remove all code except rand() and add srand(time(NULL)); above thatvoidic_SeedRand(){// these lines get all the parts of the current system time struct_timebtimebuffer;_ftime(&timebuffer);ctime(&timebuffer.time);// seconds since midnight plus millisecondssrand(timebuffer.time+timebuffer.millitm);// generate on random numberrand();}intmain(intargc,char*argv[]){// default to one, so people can specify something like d6intnRolls=1;// declare these here because it's started in multiple placesintnDice,nTotal,n,nMod;// check if they sent somethingif(argc>1){// parse the first argument to see if they sent a modifiernMod=ic_Modifier(argv[1]);// check if the user specified a roll like d8 or d9+2if(argv[1][0]=='d')// no need to set rolls, they just want one// so just grab the size of the dienDice=atoi(strtok(argv[1],"d"));// the want multiple dice to be rolledelse{// get the number before the dnRolls=atoi(strtok(argv[1],"d"));// get the number after the dnDice=atoi(strtok(NULL,"d"));}}// if there's no rolls or dice, something went wrongif(argc==1||!nRolls||!nDice)// explain to the user how to use the applicationprintf(" Use [rolls]d[sides][-+][modifier] [loop]\n Only the sides of the dice is required.");// everything looks alright, so start generating the loopselse{// ensure randomness of numbersic_SeedRand();// if they didn't specify the number of runs to makeif(argc<3)// set loop to 1argv[2]="1";// loop the specified or default amount of times for(intnLoop=1;nLoop<=atoi(argv[2]);nLoop++){// show the loop numberprintf("%d. ",nLoop);// generate the dicefor(n=1,nTotal=nMod;n<=nRolls;n++){// random number between 1 and sides of diceintnResult=(nDice*rand()/RAND_MAX)+1;// add to running totalnTotal+=nResult;// if theres a modifier or multiple diceif(nMod||nRolls>1)// display each dice rollprintf("%d ",nResult);}// if there's a modifier, display itif(nMod)printf("(%d) ",nMod);// if data was displayed before the final resultif(nMod||nRolls>1)// show this before the result so everything lines up neatlyprintf("= ");// show the total of all dices and the modifierprintf("%d",nTotal);// if this isn't the last loopif(nLoop!=atoi(argv[2]))// add a new lineprintf("\n");}}// end of codereturn0;}