1 #include  <stdio.h>
  2 /*# include         <signal.h>*/
  3 #include <time.h>
  4 
  5 static char ID[] = "@(#)craps.c         2.1 ";
  6 long      lent;
  7 long      minus;
  8 long      h_money;
  9 long      drandno;
 10 char      *get_bet();
 11 
 12 static    char      lose[] =  "\nYou lose your bet of $%ld\n";
 13 static    char      win[] =   "^G\nYou win with %ld\n";
 14 static    char      point[] = "^G\nYou made your point of %ld\n";
 15 
 16 extern long clock_ ();
 17 
 18 main()
 19 {
 20           long r;
 21           long d;
 22           long l;
 23           long tod;
 24           long      j;
 25           long       money;
 26           long      patol();
 27           extern    int finish();
 28           char      line[BUFSIZ*2];
 29           char      *p;
 30           FILE      *iop;
 31 
 32           tod = clock_();
 33           iop = stdin;
 34           h_money = 2000;
 35           /*signal(SIGINT,finish);*/
 36           srand((unsigned int)&tod);
 37           printf("Welcome to the PWB floating crap game.\n");
 38           printf("Commentary? ");
 39           if ((fgets(line,BUFSIZ,iop) == NULL))
 40                     finish();
 41           if (line[0] == 'y') {
 42                     printf("\nYou start out with $2,000.  If you  lose  it, the\n");
 43                     printf("House will lend you an additional $2,000. A total\n");
 44                     printf("of $20,000 will be lent.   Should  you  lose  all\n");
 45                     printf("$22,000, you will be escorted from the casino.\n");
 46                     printf("\nYou break the bank with $100,000 or more.\n\n");
 47           }
 48 
 49           gameloop:
 50           minus = lent = 0;
 51           h_money = 2000;
 52           while (1) {
 53                     l = (long)getdie();
 54                     if (h_money >= 50000 && lent) {
 55                               printf("You now have $%ld\n",h_money);
 56                               printf("You owe the House money, it is ");
 57                               printf("automatically reclaimed\n");
 58                               printf("(The House needs working capital).\n");
 59                               h_money = h_money - (lent * 2000);
 60                               lent = 0;
 61                     }
 62                     if (h_money >= 100000) {
 63                               broke();
 64                               goto gameloop;
 65                     }
 66                     if (h_money <= 0) {
 67                               if (lent == 10) {
 68                                         printf("You borrowed $20,000. ");
 69                                         printf("Don't you think that's ");
 70                                         printf("enough??\nGoodbye\n");
 71                                         exit (1);
 72                               }
 73                               printf("marker? ");
 74                               fgets(line,BUFSIZ,iop);
 75                               if (line[0] == 'y') {
 76                                         h_money = 2000;
 77                                         lent++;
 78                               }
 79                               else finish();
 80                     }
 81                     if (lent) {
 82                               printf("You have %ld marker(s) outstanding\n",lent);
 83                               if (h_money > 2000) {
 84                                         printf("You now have $%ld\n",h_money);
 85                                         printf("Repay marker? ");
 86                                         if ((fgets(line,BUFSIZ,iop) == NULL))
 87                                                   finish();
 88                                         if (line[0] == '\n' || line[0] == 'n')
 89                                                   ;
 90                                         else if (line[0] == 'y') {
 91                                                   if (lent == 1) {
 92                                                             h_money = h_money - 2000;
 93                                                             lent--;
 94                                                   }
 95                                                   else how_many();
 96                                         }
 97                               }
 98                     }
 99                     printf("Your bankroll is $%ld\n",h_money);
100                     if (p = get_bet())
101                               money = patol(p);
102                     else money = 2000000;
103                     while (money > h_money) {
104                               if (minus)
105                                         --minus;
106                               printf("Illegal bet\n");
107                               if (p = get_bet())
108                                         money = patol(p);
109                               else money = 2000000;
110                     }
111                     d = (long)getdie();
112                     printf ("%ld\t%ld\n",l,d);
113                     r = d + l;
114                     if (r == 7 || r == 11) {
115                               if (minus) {
116                                         printf(lose,money);
117                                         h_money = h_money - money;
118                                         --minus;
119                               }
120                               else {
121                                         printf(win,r);
122                                         h_money = h_money + money;
123                               }
124                     }
125                     else if (r == 2 || r == 3 || r == 12) {
126                               if (minus) {
127                                         printf(win,r);
128                                         h_money = h_money + money;
129                                         --minus;
130                               }
131                               else {
132                                         printf(lose,money);
133                                         h_money = h_money - money;
134                               }
135                     }
136                     else {
137                               printf("The point is %ld\n",r);
138                               j = 0;
139                               while (j != r) {
140                                         l = (long)getdie();
141                                         d = (long)getdie();
142                                         printf ("\t%ld\t%ld\n",l,d);
143                                         j = d + l;
144                                         if (j == 7) {
145                                                   if (minus) {
146                                                             printf(win,j);
147                                                             h_money = h_money + money;
148                                                             --minus;
149                                                   }
150                                                   else {
151                                                             printf(lose,money);
152                                                             h_money = h_money - money;
153                                                   }
154                                                   break;
155                                         }
156                               }
157                               if (j == r) {
158                                         if (minus) {
159                                                   printf(lose,money);
160                                                   h_money = h_money - money;
161                                                   --minus;
162                                         }
163                                         else {
164                                                   printf(point,j);
165                                                   h_money = h_money + money;
166                                         }
167                               }
168                     }
169           }
170 }
171 
172 
173 getdie()
174 {
175           long retval;
176 rrdo:
177           retval=(rand()%6)+1;
178           if (retval < 1) goto rrdo;
179           return((int)retval);
180 }
181 
182 
183 finish()
184 {
185           long       money;
186 
187           money = ((lent * 2000) + 2000);
188           h_money = h_money - money;
189           if (h_money > 0)
190                     printf("\nYou win $%ld\n",h_money);
191           else if (h_money == 0)
192                     printf("\nYou break even\n");
193           else {
194                     h_money = (-h_money);
195                     printf("\nYou lose $%ld\n",h_money);
196           }
197           exit (1);
198 }
199 
200 
201 broke()
202 {
203           char      line[BUFSIZ];
204           FILE      *iop;
205 
206           iop = stdin;
207           printf("\nYour luck is pretty good.  You broke the bank\n");
208           printf("New game? ");
209           fgets(line,BUFSIZ,iop);
210           if (line[0] == 'y')
211                     return;
212           exit (1);
213 }
214 
215 
216 a_number(l)
217 char      *l;
218 {
219           register long j;
220 
221           for (j = 0; l[j] != '\0'; j++)
222                     if ('0' <= l[j] && l[j] <= '9')
223                               continue;
224                     else return(0);
225           if (l[0] == '\0')
226                     return(0);
227           return(1);
228 }
229 
230 
231 how_many()
232 {
233           FILE *iop;
234           register long repay;
235           char      line[BUFSIZ];
236 
237           iop = stdin;
238           repay = 0;
239           while (repay <= lent) {
240                     printf("How many? ");
241                     if ((fgets(line,BUFSIZ,iop) == NULL)) {
242                               printf("\nYou refused to pay\n");
243                               finish();
244                     }
245                     line[strlen(line) - 1] = '\0';
246                     if (a_number(line)) {
247                               repay = atoi(line);
248                               if (repay > lent) {
249                                         printf("You don't have that many markers\n");
250                                         repay = 0;
251                                         continue;
252                               }
253                               if (repay * 2000 > h_money) {
254                                         printf("You don't have enough money\n");
255                                         repay = 0;
256                                         continue;
257                               }
258                               if (repay * 2000 == h_money) {
259                                         printf("That wouldn't leave you much!!\n");
260                                         repay = 0;
261                                         continue;
262                               }
263                               h_money = h_money - (repay * 2000);
264                               lent = lent - repay;
265                               printf("You repayed %ld marker(s)\n",repay);
266                               break;
267                     }
268                     else {
269                               printf("Illegal entry\n");
270                               repay = 0;
271                     }
272           }
273           return;
274 }
275 
276 
277 char      *get_bet()
278 {
279           static char line[BUFSIZ];
280           register char *p;
281           FILE *iop;
282 
283           iop = stdin;
284           printf("bet? ");
285           if ((fgets(line,BUFSIZ,iop)) == NULL)
286                     finish();
287           if (line[0] != '\n') {
288                     line[strlen(line) - 1] = '\0';
289                     p = line;
290                     if (line[0] == '-') {
291                               p++;
292                               minus++;
293                     }
294                     if (!a_number(p))
295                               return(0);
296                     else return(p);
297           }
298           else return(0);
299 }
300 
301 
302 long patol(s)
303 register char *s;
304 {
305           long i;
306 
307           i = 0;
308           while (*s >= '0' && *s <= '9')
309                     i = 10*i + *s++ - '0';
310 
311           if (*s)
312                     return(-1);
313           return(i);
314 }