Providers
Debian Linux
SDF
MinGW-W64
Verizon
Research
Search Engines
Bing
DuckDuckGo
Google
Metacrawler
SearX
Miscellaneous
Faqs and RFCs
Open Directory Project
W3Schools Online Web Tutorials
Wikipedia
Multimedia
CTIS
For the Pigs Among Us
Family Photos
My House
My Photos (1999-2002?)
My Photos (2005)
Retro Computers
Minions of Mirth
Main
Criticals
Criticals (Single Player)
Ghaqua
Maps and Points of Interest
Miscellaneous
Monster Templates
Miscellaneous
Area Codes
Bard’s Tale
Dungeons & Dragons Online
My Blog
Quote of the Day
RogueBasin
Slashdot
The Adventuer’s Guild
Weather
GitHub:eprive/Z80-MBC3
Programming
Main
Across
Biorhythm
BMP Chips
Complex Roots
Magic Square
Magic Square(js)
Matrix(js)
More DOS Programs
Roll
Strip Dups (bash)
Wacky Wood-Worker
How To
Enlightenment 0.16.7.2 Startup Patch
How to Create an OS X Iconset
My RedHat Howto
SSH to VMS (Deathrow OpenVMS Cluster)
Tools
Allowable Mortgage Interest
Dates and Mileage
Day of the Year
Fun With Time
Downloads
Educational
Utilities
roll.c
/* Roll.c - A simplified recreation of telehack.com's roll */ /* Michael J. Chappell */ #define VERSION "0.6 5 October 2024" #define VERSION_YEAR 2024 /* For reference rolls 2 die, or n die for "roll n", Usage is: roll [n]|[/?]|[-?][/V][/v] will accept only one argument, checks for invalid characters in options, options start with '-' or '/', if legal option is not '?' it is ignored, 0<=n<=10, no output on 0, just blank lines, error is "%bad number of dice", lines are prefaced with CR */ #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <time.h> #include "screen.h" #include "roll.h" #if defined(__FreeBSD__) #define sranddev() srandomdev() #endif #if defined(__NetBSD__)||defined(__linux__) #define sranddev() srand((int)time(NULL)) #endif int die[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int printDieLine(int r, int line) { int i, j; switch (line) { case 0: fputs(TOPLINE, stdout); break; case 1: case 2: case 3: fputs(LEDGE, stdout); for (i=0; i<3; i++) fputs(matrix[r][line-1][i] ? FILL : EMPTY, stdout); fputs(REDGE, stdout); break; case 4: fputs(BOTTOMLINE, stdout); break; case 5: fprintf(stdout, " %d ", r); break; } return EXIT_SUCCESS; } /* getNum returns 0 to MAXDIES ROLL_ERR_RANGE, ROLL_ERR_PARAM or ROLL_ERR_HELP based on the contents of the commandline. */ int getNum(int count, char **value) { int n=0; int i=0; int option=0; /* Boolean indicates option passed */ int flagged=0; /* Boolean indicator for invalid characters */ /* allow for only 0 or 1 parameters */ if (count==1) return DEFAULT_NUM_DICE; if (count>2) return ROLL_ERR_PARAM; /* Checks option for '?' and 'H' for help, also checks for 'V' for version. Throws away other valid option strings, Valid options consist of characters in {0-9,A-Z,a-z}. */ if ((value[1][0]=='-')||(value[1][0]=='/')) option=1; if (option) { flagged=0; i=1; if (value[1][1]=='\0') flagged=1; /* no option after flag */ while (value[1][i]!='\0') { if (!isalnum(value[1][i++])) /* invalid char after flag */ flagged=1; } } else { flagged=0; i=0; while (value[1][i]!='\0') { if (!isdigit(value[1][i++])) flagged=1; } } if (strcmp(value[1],"-H")==0) return ROLL_ERR_HELP; if (strcmp(value[1],"-h")==0) return ROLL_ERR_HELP; if (strcmp(value[1],"/H")==0) return ROLL_ERR_HELP; if (strcmp(value[1],"/h")==0) return ROLL_ERR_HELP; if (strcmp(value[1],"-?")==0) return ROLL_ERR_HELP; if (strcmp(value[1],"/?")==0) return ROLL_ERR_HELP; if (strcmp(value[1],"-V")==0) return ROLL_ERR_VERSION; if (strcmp(value[1],"-v")==0) return ROLL_ERR_VERSION; if (strcmp(value[1],"/V")==0) return ROLL_ERR_VERSION; if (strcmp(value[1],"/v")==0) return ROLL_ERR_VERSION; n = atoi(value[1]); if (n>MAXDIES) return ROLL_ERR_RANGE; /* since we allow for negative, we need test for the negative out of range too */ if ((n<0) && (n<MAXDIES*-1)) return ROLL_ERR_RANGE; if (flagged) return ROLL_ERR_PARAM; if (option && !n) return ROLL_ERR_HELP; return n; } /* getNum */ int version(char *command, char *versionstring, int year) { printf("\n%s %s", command, versionstring); printf("\nCopyright %d Michael J. Chappell\n", year); return EXIT_SUCCESS; } int main(int argc, char **argv) { int line=0; int roll=0; int n=0; int i=0; int toss=0; for (i=0; i<10; i++ ) die[i] = i%6+1; n = getNum(argc, argv); /* handle errors */ switch (n) { case ROLL_ERR_HELP: printf("\n%s %s",argv[0], VERSION); printf("\nUsage:\n\t%s [-][N][/v][/?]\n",argv[0]); printf("\nOptions can be designated with \'/\' or \'-\' in upper or"); printf("\nlower-case. N should be 1-10 inclusive. Default is 2.\n"); printf("\nCopyright %d Michael J. Chappell\n", VERSION_YEAR); return EXIT_SUCCESS; break; case ROLL_ERR_PARAM: fputs(BAD_NUMBER_PARAM, stderr); return EXIT_FAILURE; break; case ROLL_ERR_RANGE: fputs(BAD_NUMBER_DICE, stderr); return EXIT_FAILURE; break; case ROLL_ERR_VERSION: version(argv[0],VERSION, VERSION_YEAR); return EXIT_SUCCESS; break; } /* If n is positive seed rand, otherwise skip seeding, make n positive */ if (n>0) { sranddev(); } else { n=n*-1; } for (toss=0; toss<NUM_THROWS; toss++) { for (i=0; i<n; i++) die[i]=rand()%6+1; screen_clear(); for (line=0; line<6; line++) { putc('\n', stdout); for (i=0; i<n; i++) printDieLine(die[i], line); } } fputc('\n', stdout); return EXIT_SUCCESS; }
This source has been viewed 18 times.
Wednesday, 9 July 2025
Michael J. Chappell
Contact me at:
mcsuper5@freeshell.org