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
craps.c
/* craps.c - the casino dice game */ /* Michael J. Chappell */ #define VERSION "0.3 6 Oct 2024" #define VERSION_YEAR 2024 #if defined(__linux__) #define sranddev() srand((unsigned int)time(NULL)) #endif #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <time.h> #include "screen.h" #include "craps.h" #include "getbank.h" #include "getbet.h" int matrix[7][3][3] = { { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }, { { 0, 0, 0 }, { 0, 1, 0 }, { 0, 0, 0 } }, { { 0, 0, 1 }, { 0, 0, 0 }, { 1, 0, 0 } }, { { 0, 0, 1 }, { 0, 1, 0 }, { 1, 0, 0 } }, { { 1, 0, 1 }, { 0, 0, 0 }, { 1, 0, 1 } }, { { 1, 0, 1 }, { 0, 1, 0 }, { 1, 0, 1 } }, { { 1, 0, 1 }, { 1, 0, 1 }, { 1, 0, 1 } } }; int die[2] = { 0, 0 }; int printDieLine(int roll, 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[roll][line-1][i] ? FILL : EMPTY, stdout); fputs(REDGE, stdout); break; case 4: fputs(BOTTOMLINE, stdout); break; case 5: fprintf(stdout, " %d ", roll); break; } return EXIT_SUCCESS; } int version(char *command, char *versionstring, int year) { printf("%s %s", command, versionstring); printf("\nCopyright %d Michael J. Chappell\n", year); return EXIT_SUCCESS; } /* printDice - prints out dice number is the number of dice. die is an array integers reprsenting rolls. verifies each roll is 1<=roll<=6. returns the number of dice printed. */ int printDice(int number, int *die) { int line; int i; /* Are rolls valid? */ for (i=0; i<number; i++) { if (die[i]<1) return 0; if (die[i]>6) return 0; } for (line=0; line<6; line++) { putc('\n', stdout); for (i=0; i<number; i++) printDieLine(die[i], line); } return number; } int main(int argc, char **argv) { int line=0; int roll=0; int bank=0; int i=0; int toss=0; int point=0; int total=0; int kitty=0; int bet=0; die[0]=4; die[1]=3; bank = getBank(argc, argv); /* handle errors */ switch (bank) { case CRAPS_ERR_HELP: printf("%s\n", argv[0]); printf("\n" "Usage:\n"); printf("\t%s [-][n]\n", argv[0]); printf("\t%s [-h][-v]\n", argv[0]); printf("\n" "n is your initial bank roll and should be in the range of 5\n" "to %d. The default bank roll is %d.\n", MAX_BANK_ROLL, DEFAULT_BANK_ROLL); printf("\n" "A negative value will not seed the random number generator.\n" "\n" "-h\tHelp\n" "-v\tVersion\n" "\n" "Copyright %d Michael J. Chappell\n", VERSION_YEAR); return EXIT_SUCCESS; break; case CRAPS_ERR_PARAM: printf("Bad argument!\n" "\n" "Valid arguments are integers 5 to %d, -h or -v.\n", MAX_BANK_ROLL); printf("Negating the integer will skip seeding the random number\n" "generator.\n"); return EXIT_FAILURE; break; case CRAPS_ERR_RANGE: printf("n is out of range!\n" "\n" "n is your initial bank roll and should be in the range of 5\n"); printf("to %d. The default bank roll is %d.\n", MAX_BANK_ROLL, DEFAULT_BANK_ROLL); printf("A negative value will not seed the random number generator.\n" "\n"); return EXIT_FAILURE; break; case CRAPS_ERR_VERSION: version(argv[0],VERSION, VERSION_YEAR); return EXIT_SUCCESS; break; } /* If bank is positive seed rand, otherwise skip, make n positive */ if (bank>0) { sranddev(); } else { bank=bank*-1; } for (roll=1; ;) { if ((bank<1)&&(roll==1)) { printf("You ran out of money.\n" "\n" "Come again soon!\n"); break; } if (roll==1) { kitty = 0; point = 0; } screen_clear(); printf(" ____ ____ _ ____ ____\n" " / ___| _ \\ / \\ | _ \\/ ___|\n" "| | | |_) | / _ \\ | |_) \\___ \\\n" "| |___| _ < / ___ \\| __/ ___) |\n" " \\____|_| \\_\\/_/ \\_\\_| |____/\n" "\n"); printf("Bank: $%6d Pot: $%6d\n", bank, kitty); printf("Roll: %d\n\n", roll); bet = getBet(roll, point, kitty, bank, CRAPS_MAX_BET); if (bet==-2) break; bank = bank-bet; kitty = kitty+bet; die[0] = rand()%6+1; die[1] = rand()%6+1; printDice(2, die); printf("\n\n"); total = die[0]+die[1]; if (roll==1) { switch(total) { case 2: case 3: printf("Craps! You lose.\n"); kitty = 0; break; case 7: printf("Natural! Winner!\n"); bank = bank+2*kitty; kitty = 0; break; case 11: printf("Natural! Winner!\n"); bank = bank+2*kitty; kitty = 0; break; case 12: printf("Craps! You lose.\n"); kitty = 0; break; case 4: case 5: case 6: case 8: case 9: case 10: printf("Your point is %d.\n", total); point = total; roll++; break; } } else { if (total==point) { printf("Point made.\n"); if (die[0]==die[1]) printf("The hard way!\n"); printf("Winner!\n"); bank = bank+2*kitty; roll = 1; } else if (total==7) { printf("Craps! You loose!\n"); roll = 1; } else { roll++; } } /* Eat the carriage return left by scanf */ while (fgetc(stdin)!='\n') ; printf("\nPress <Enter> or <Return> to Continue.."); while (fgetc(stdin)!='\n') ; printf("\n"); } return EXIT_SUCCESS; }
This source has been viewed 15 times.
Tuesday, 8 July 2025
Michael J. Chappell
Contact me at:
mcsuper5@freeshell.org