Robson » Code Viewer
- Path: files/roll.code.txt
- Lines: 136
- Size: 4.18kB
/* 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> using namespace std; // this code determines if the user requested an addition or subtractionint ic_Modifier(char *arg){ // addition if (strchr(arg, '+')) { // remove everything before the plus sign strtok(arg, "+"); // return everything at the end return atoi(strtok(NULL, "+")); } // subtraction else if (strchr(arg, '-')) { // remove everything before the plus sign strtok(arg, "-"); // return everything at the end return 0 - atoi(strtok(NULL, "-")); } // nothing else // the user didn't specify a modifier, so return zero return 0; } // 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 thatvoid ic_SeedRand(){ // these lines get all the parts of the current system time struct _timeb timebuffer; _ftime(&timebuffer); ctime(&timebuffer.time); // seconds since midnight plus milliseconds srand(timebuffer.time + timebuffer.millitm); // generate on random number rand();} int main(int argc, char *argv[]){ // default to one, so people can specify something like d6 int nRolls = 1; // declare these here because it's started in multiple places int nDice, nTotal, n, nMod; // check if they sent something if (argc > 1) { // parse the first argument to see if they sent a modifier nMod = ic_Modifier(argv[1]); // check if the user specified a roll like d8 or d9+2 if (argv[1][0] == 'd') // no need to set rolls, they just want one // so just grab the size of the die nDice = atoi(strtok(argv[1], "d")); // the want multiple dice to be rolled else { // get the number before the d nRolls = atoi(strtok(argv[1], "d")); // get the number after the d nDice = atoi(strtok(NULL, "d")); } } // if there's no rolls or dice, something went wrong if (argc == 1 || !nRolls || !nDice) // explain to the user how to use the application printf(" Use [rolls]d[sides][-+][modifier] [loop]\n Only the sides of the dice is required."); // everything looks alright, so start generating the loops else { // ensure randomness of numbers ic_SeedRand(); // if they didn't specify the number of runs to make if (argc < 3) // set loop to 1 argv[2] = "1"; // loop the specified or default amount of times for (int nLoop = 1; nLoop <= atoi(argv[2]); nLoop++) { // show the loop number printf("%d. ", nLoop); // generate the dice for (n = 1, nTotal = nMod; n <= nRolls; n++) { // random number between 1 and sides of dice int nResult = (nDice * rand()/RAND_MAX)+1; // add to running total nTotal += nResult; // if theres a modifier or multiple dice if (nMod || nRolls > 1) // display each dice roll printf("%d ", nResult); } // if there's a modifier, display it if (nMod) printf("(%d) ", nMod); // if data was displayed before the final result if (nMod || nRolls > 1) // show this before the result so everything lines up neatly printf("= "); // show the total of all dices and the modifier printf("%d", nTotal); // if this isn't the last loop if (nLoop != atoi(argv[2])) // add a new line printf("\n"); } } // end of code return 0; }