home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource4 / 205_01 / bg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1979-12-31  |  17.6 KB  |  774 lines

  1. /*
  2. HEADER:         CUG205.00;
  3. TITLE:          BG.C - Backgammon II (for MS-DOS);
  4. VERSION:        2.00;
  5. DATE:           10/14/86;
  6. DESCRIPTION:    "Leor Zolman's public domain backgammon
  7.                 for PC/MS-DOS.  Uses ANSI.SYS for screen control and colors.";
  8. KEYWORDS:       games, backgammon;
  9. SYSTEM:         MS-DOS;
  10. FILENAME:       BG.C.;
  11. WARNINGS:       "This program uses the ANSI color standard.  Include the
  12.                  following line in your CONFIG.SYS file at system start.  
  13.                       device = ansi.sys
  14.                 For further details on ANSI.SYS, refer to your DOS manual.
  15.                 The author claims the copyright to the MS-DOS version and
  16.                 authorizes non-commercial use only.";
  17. SEE-ALSO:       CUG102, BGTALK.C, BGDISP.C;
  18. AUTHORS:        Michael Yokoyama;
  19. COMPILERS:      Microsoft v.3.00.;
  20. */
  21.  
  22. #include <conio.h>
  23. #include <ctype.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <time.h>
  27.  
  28. #include "bgdisp.c"     /* Display subroutines              */
  29. #include "bgtalk.c"     /* Conversation subroutines         */
  30.  
  31. #define LINT_ARGS 1     /* Enable strong lint-type checking */
  32. #define NIL (-1)
  33. #define NO 0
  34. #define YES 1
  35. #define MAXGMOV 10
  36. #define MAXLINE 150
  37. #define MAXIMOVES 1000
  38.  
  39. /*  Declare all functions which do not return integers */
  40.  
  41. char strips();
  42.  
  43. char c, level;
  44. int i, j, l, m, count;
  45. int die1, die2;               /* Dice                                   */
  46. int mwin, ywin;               /* Rolling count of my wins and your wins */
  47. int red[31];                  /* Array of the board's red pieces        */
  48. int white[31];                /* Array of the board's white pieces      */
  49. int prob[13];                 /* Probability?                           */
  50. int goodmove[MAXGMOV];
  51. int probmove[MAXGMOV];
  52. int imoves;
  53.  
  54. struct {
  55.   int pos[4];
  56.   int mov[4];
  57. } moves[MAXIMOVES];
  58.  
  59. main()
  60. {
  61.   int a, i, k, n, t, go[6];
  62.   int firstmove;                /* Flag == true on first move only       */
  63.   int wrongmove;                /* Flag == true if move is incorrect     */
  64.   long iths;                    /* Hundredths of secs for random seed    */
  65.   char s[MAXLINE];
  66.  
  67.   mwin = ywin = 0;              /* Reset game score                      */
  68.   time(&iths);                  /* Seed the random generator             */
  69.   srand(iths);
  70.   title();
  71.   printf ("Do you want the instructions?\n");
  72.   if ((c = getche()) == 'y')
  73.     help();
  74.   header();
  75.   select();
  76. newgame:
  77.   init();
  78.   firstmove = 1;
  79.   go[5] = NIL;
  80.   message0();
  81. throw:  
  82.   roll();
  83.   printf ("My roll is: %c[37m%d%c[30m, your roll is: %c[31m%d%c[30m\n",27,die1,27,27,die2,27);
  84.   if (die1 == die2) {
  85.     printf ("Tied up, roll again:\n");
  86.     sleep();
  87.     goto throw;
  88.   }
  89.   printf ("\n%s will go first . . .\n\n", die1 > die2 ? "I" : "You");
  90.   printf ("Press any key:\n");
  91.   getch();
  92.   cls();
  93.   if (die2 > die1) 
  94.     goto red;
  95.  
  96. white: 
  97.   if (!firstmove) 
  98.     roll();                 /* Skip roll on first move */
  99.   firstmove = 0;            /* First roll not true anymore */
  100.   order();
  101.   show2();
  102.   if (!canmove(white,red)) {
  103.     message6();
  104.     goto red;
  105.   }
  106.   message5();
  107.   if (nextmove(white,red) == NIL) 
  108.     goto red;
  109.   if (piececount(white,0,24) == 0) {
  110.     sleep();
  111.     cls();
  112.     printf ("\7I won!\n\n");
  113.     ++mwin;
  114.     score();
  115.     if (mwin == ywin)
  116.       printf ("All tied up!  Who's gonna pull ahead?\n");
  117.     else if (mwin > 2 && !ywin)
  118.       printf ("It's about time you managed to win a game!\n");
  119.     else message1();
  120.     goto again;
  121.   }
  122.  
  123. red:
  124.   dboard();
  125.   if (!firstmove) 
  126.     roll();                  /* Skip roll on first move             */
  127.   firstmove = 0;             /* First roll not true anymore         */
  128.   order();
  129.   show1();
  130.   if (!canmove(red,white)) {
  131.     message7();
  132.     getch();
  133.     goto white;
  134.   }
  135. retry:
  136.   message3();
  137.   while (a = kbhit() != 0) {
  138.     time(&iths);
  139.     srand(iths);             /* Exercise the random generator while */
  140.   }                          /* waiting for a move to be made . . . */
  141.   gets(s);                   
  142.  
  143.   if (strips(s) == '?' || strips(s) == 'h') {
  144.     helpscrn();
  145.     printf ("\nDo you want to see all the instructions?\n");
  146.     if ((c = getche()) == 'y')
  147.       help();
  148.     cls();
  149.     dboard();
  150.     show1();
  151.     wrongmove = YES;
  152.   }
  153.   if (strips(s) == 'q') {     /* check for quit command */
  154.     printf ("Aborting game:  Enter Y to confirm\n");
  155.     if ((c = getche()) == 'y')
  156.       goto quit;
  157.     dboard();
  158.     show1();
  159.     wrongmove = YES;
  160.   }
  161.   if (strips(s) == 'b') {     /* check for print board command */
  162.     cls();
  163.     dboard();
  164.     show1();
  165.     wrongmove = YES;
  166.   }
  167.   if (strips(s) != '-' && !isdigit(strips(s))) {
  168.     cls();
  169.     dboard();
  170.     printf ("\7You must move.  ");
  171.     show1();
  172.     wrongmove = YES;
  173.   }
  174.   /* convert commas to spaces */
  175.   for (i = 0; s[i]; i++)
  176.     if (s[i] == ',') 
  177.       s[i] = ' ';
  178.  
  179.   n = sscanf(s,"%d%d%d%d%d",&go[0],&go[1],&go[2],&go[3],&go[4]);
  180.  
  181.   if ((die1  != die2 && n > 2) || n > 4) {
  182.     cls();
  183.     dboard();
  184.     printf ("\7Too many moves.  ");
  185.     show1();
  186.     wrongmove = YES;
  187.   }
  188.  
  189.   if (((die1  != die2) && n < 2) || ((die1 == die2) && n < 4)) {
  190.     if (n == redcount()) 
  191.       goto moveok;
  192.     printf ("\7Ok to skip move(s)?");
  193.     if ((c = getche()) != 'y') {
  194.       cls();
  195.       dboard();
  196.       printf ("\7Enter your moves again:  ");
  197.       show1();
  198.       wrongmove = YES;
  199.     }
  200.     printf ("\n");
  201.   }
  202.  
  203. moveok: 
  204.   go[n] = NIL;
  205.   if (*s == '-') {
  206.     go[0] = -go[0];
  207.     t = die1;
  208.     die1 = die2;
  209.     die2 = t;
  210.   }
  211.   for (k = 0; k<n; k++) {
  212.     if (0 <= go[k] && go[k] <= 24)
  213.       continue;
  214.     else {
  215.       cls();
  216.       dboard();
  217.       printf ("\7You can't move %d.  ",go[k]);
  218.       show1();
  219.       wrongmove = YES;
  220.     }
  221.   }
  222.   if (play(red,white,go)) 
  223.     wrongmove = YES;
  224.   if (wrongmove == YES) {
  225.     wrongmove = NO;
  226.     goto retry;
  227.   }
  228.   if (piececount(red,0,24) == 0) {
  229.     sleep();
  230.     cls();
  231.     printf ("\7You win . . .\n");
  232.     ++ywin;
  233.     score();
  234.     if (mwin == ywin)
  235.       printf ("\nLooks like now we're dead even . . . it's tiebreaker time!\n");
  236.     else if (ywin > 2 && !mwin)
  237.       printf ("\nYou're lucky I don't throw the dice. . . .\n");
  238.     else message2();
  239.  
  240. again:    
  241.     message4();
  242. poll:
  243.     c = getch();
  244.     if (c == 'y') {
  245.       cls();
  246.       goto newgame;
  247.     }
  248.     else if (c != 'n') {
  249.       printf("\7");
  250.       goto poll;
  251.     }
  252.     else goto quit;
  253.   }
  254.   goto white;
  255. quit:
  256.   cls();
  257.   exit(1);
  258. }
  259.  
  260. /* Initialize the board and the probabilities */
  261. init()
  262. {
  263.   int i;
  264.  
  265.   /* Clear off board and enter the starting positions of the pieces */
  266.   for (i = 0; i <= 30; i++)
  267.     red[i] = white[i] = 0;
  268.   red[1] = white[1] = 2;
  269.   red[12] = white[12] = 5;
  270.   red[17] = white[17] = 3;
  271.   red[19] = white[19] = 5;
  272.  
  273.   for (i = 0; i <= 30; i++)   /* Note:  Is this code necessary? */
  274.     prob[i] = 0;
  275.  
  276.   prob[0] = 0;
  277.   prob[1] = 11;
  278.   prob[2] = 12;
  279.   prob[3] = 13;
  280.   prob[4] = 14;
  281.   prob[5] = 15;
  282.   prob[6] = 16;
  283.   prob[7] =  6;
  284.   prob[8] =  5;
  285.   prob[9] =  4;
  286.   prob[10] = 3;
  287.   prob[11] = 2;
  288.   prob[12] = 1;
  289. }
  290.  
  291. /* STRATEGY SECTION.STRATEGY SECTION.STRATEGY SECTION.*/
  292. /* STRATEGY SECTION.STRATEGY SECTION.STRATEGY SECTION.*/
  293. /* STRATEGY SECTION.STRATEGY SECTION.STRATEGY SECTION.*/
  294. /* STRATEGY SECTION.STRATEGY SECTION.STRATEGY SECTION.*/
  295. /* STRATEGY SECTION.STRATEGY SECTION.STRATEGY SECTION.*/
  296.  
  297. int play(player,opponent,pos)
  298. int *player,*opponent,pos[];
  299. {
  300.   int k,n,die,ipos;
  301.  
  302.   /* Blots on player[0] must be moved first */
  303.   for (k = 0;k<player[0];k++) {
  304.     if (pos[k] == NIL)
  305.       break;
  306.     if (pos[k] != 0) {
  307.       printf ("\nPlay your BAR piece (on point 0) first\n");
  308.       return(-1);
  309.     }
  310.   }
  311.  
  312.   for (k = 0;(ipos = pos[k]) != NIL;k++) {
  313.     die = k?die2:die1;
  314.     n = 25-ipos-die;
  315.     if (player[ipos] == 0) 
  316.       goto illegal;
  317.     if (n>0 && opponent[n] >= 2) 
  318.       goto illegal;
  319.     if (n <= 0) {
  320.       if (piececount(player,0,18) != 0) 
  321.         goto illegal;
  322.       if ((ipos+die) != 25 && piececount(player,19,24-die) != 0) 
  323.         goto illegal;
  324.     }
  325.     player[ipos]--;
  326.     player[ipos+die]++;
  327.   }
  328.   for (k = 0;pos[k] != NIL;k++) {
  329.     die = k?die2:die1;
  330.     n = 25-pos[k]-die;
  331.     if (n>0 && opponent[n] == 1) {
  332.       opponent[n] = 0;
  333.       opponent[0]++;
  334.     }
  335.   }
  336.   return(0);
  337.  
  338. illegal:
  339.   while(k--) {
  340.     die = k?die2:die1;
  341.     player[pos[k]]++;
  342.     player[pos[k]+die]--;
  343.   }
  344.   cls();
  345.   dboard();
  346.   printf ("\7You can't move %d.  Try again:\n",ipos);
  347.   show1();
  348.   return(-1);
  349. }
  350.  
  351. /* Return true if player can move */
  352. int canmove(player,opponent)
  353. int *player, *opponent;
  354. {
  355.   int k;
  356.   imoves = 0;
  357.   movegen(player,opponent);
  358.   if (die1 != die2) {
  359.     k = die1;   
  360.     die1 = die2;        
  361.     die2 = k;
  362.     movegen(player,opponent);
  363.     k = die1;   
  364.     die1 = die2;        
  365.     die2 = k;
  366.   }
  367.   return imoves;
  368. }
  369.  
  370. int nextmove(player,opponent)
  371. int *player,*opponent;
  372. {
  373.   int k;
  374.   if (die1 != die2) {
  375.     k = die1;  
  376.     die1 = die2;  
  377.     die2 = k;
  378.   }
  379.  
  380.   /* Select the kth possible move */
  381.   k = strategy(player,opponent);
  382.   dmove(k);
  383.   update(player,opponent,k);
  384.   return(0);
  385. }
  386.  
  387. dmove(k)
  388. int k;
  389. {
  390.   int cflag;                           /* Comma flag       */
  391.   int i, n;
  392.  
  393.   cflag = 0;
  394.   if (k == NIL)
  395.     printf ("No move possible\n");
  396.   else for (n = 0; n < 4; n++) {
  397.     if (moves[k].pos[n] == NIL)
  398.       break;
  399.     if (cflag)                         /* Is comma required? */ 
  400.       printf (",");
  401.     i = 25 - moves[k].pos[n];
  402.     if (i == 0 || i == 25)
  403.       printf (" BAR-%d",moves[k].mov[n]);
  404.     else
  405.       printf (" %d-%d",i,moves[k].mov[n]);
  406.     cflag = 1;
  407.   }
  408.   printf (".");
  409. }
  410.  
  411. update(player,opponent,k)
  412. int *player,*opponent,k;
  413. {
  414.   int n,t;
  415.   for (n = 0;n<4;n++) {
  416.     if (moves[k].pos[n] == NIL)
  417.       break;
  418.     player[moves[k].pos[n]]--;
  419.     player[moves[k].pos[n]+moves[k].mov[n]]++;
  420.     t = 25-moves[k].pos[n]-moves[k].mov[n];
  421.     if (t>0 && opponent[t] == 1) {
  422.       opponent[0]++;
  423.       opponent[t]--;
  424.     }
  425.   }
  426. }
  427.  
  428. movegen(mover,movee)
  429. int *mover,*movee;
  430. {
  431.   int k;
  432.   for (i = 0;i <= 24;i++) {
  433.     count = 0;
  434.     if (mover[i] == 0)
  435.       continue;
  436.     if ((k = 25-i-die1)>0 && movee[k] >= 2)
  437.       if (mover[0]>0)
  438.         break;
  439.       else 
  440.         continue;
  441.     if (k <= 0) {
  442.       if (piececount(mover,0,18) != 0)
  443.         break;
  444.       if ((i+die1) != 25 && piececount(mover,19,24-die1) != 0)
  445.         break;
  446.     }
  447.     mover[i]--;
  448.     mover[i+die1]++;
  449.     count = 1;
  450.     for (j = 0;j <= 24;j++) {
  451.       if (mover[j] == 0)
  452.         continue;
  453.       if ((k = 25-j-die2)>0 && movee[k] >= 2)
  454.         if (mover[0]>0)
  455.           break;
  456.         else 
  457.           continue;
  458.       if (k <= 0) {
  459.         if (piececount(mover,0,18) != 0)
  460.           break;
  461.         if ((j+die2) != 25 && piececount(mover,19,24-die2) != 0)
  462.           break;
  463.       }
  464.       mover[j]--;
  465.       mover[j+die2]++;
  466.       count = 2;
  467.       if (die1 != die2) {
  468.         moverec(mover);
  469.         if (mover[0]>0)
  470.           break;
  471.         else continue;
  472.       }
  473.       for (l = 0;l <= 24;l++) {
  474.         if (mover[l] == 0)
  475.           continue;
  476.         if ((k = 25-l-die1)>0 && movee[k] >= 2)
  477.           if (mover[0]>0)
  478.             break;
  479.         else continue;
  480.         if (k <= 0) {
  481.           if (piececount(mover,0,18) != 0)
  482.             break;
  483.           if ((l+die2) != 25 && piececount(mover,19,24-die1) != 0)
  484.             break;
  485.         }
  486.         mover[l]--;
  487.         mover[l+die1]++;
  488.         count = 3;
  489.         for (m = 0;m <= 24;m++) {
  490.           if (mover[m] == 0)
  491.             continue;
  492.           if ((k = 25-m-die1) >= 0 && movee[k] >= 2)
  493.             if (mover[0]>0)
  494.               break;
  495.           else
  496.             continue;
  497.           if (k <= 0) {
  498.             if (piececount(mover,0,18) != 0)
  499.               break;
  500.             if ((m+die2) != 25 && piececount(mover,19,24-die1) != 0)
  501.               break;
  502.           }
  503.           count = 4;
  504.           moverec(mover);
  505.           if (mover[0]>0)
  506.             break;
  507.         }
  508.         if (count == 3)
  509.           moverec(mover);
  510.         else {
  511.           mover[l]++;
  512.           mover[l+die1]--;
  513.         }
  514.         if (mover[0]>0)
  515.           break;
  516.       }
  517.       if (count == 2)
  518.         moverec(mover);
  519.       else {
  520.         mover[j]++;
  521.         mover[j+die1]--;
  522.       }
  523.       if (mover[0]>0)
  524.         break;
  525.     }
  526.     if (count == 1)
  527.       moverec(mover);
  528.     else {
  529.       mover[i]++;
  530.       mover[i+die1]--;
  531.     }
  532.     if (mover[0]>0)
  533.       break;
  534.   }
  535. }
  536.  
  537. moverec(mover)
  538. int *mover;
  539. {
  540.   int t;
  541.   if (imoves >= MAXIMOVES) 
  542.     goto undo;
  543.   ;
  544.   for (t = 0;t <= 3;t++)
  545.     moves[imoves].pos[t] = NIL;
  546.   switch(count) {
  547.   case 4:
  548.     moves[imoves].pos[3] = m;
  549.     moves[imoves].mov[3] = die1;
  550.   case 3:
  551.     moves[imoves].pos[2] = l;
  552.     moves[imoves].mov[2] = die1;
  553.   case 2:
  554.     moves[imoves].pos[1] = j;
  555.     moves[imoves].mov[1] = die2;
  556.   case 1:
  557.     moves[imoves].pos[0] = i;
  558.     moves[imoves].mov[0] = die1;
  559.     imoves++;
  560.   }
  561. undo:
  562.   switch(count) {
  563.   case 4:
  564.     break;
  565.   case 3:
  566.     mover[l]++;
  567.     mover[l+die1]--;
  568.     break;
  569.   case 2:
  570.     mover[j]++;
  571.     mover[j+die2]--;
  572.     break;
  573.   case 1:
  574.     mover[i]++;
  575.     mover[i+die1]--;
  576.   }
  577. }
  578.  
  579. strategy(player,opponent)
  580. int *player,*opponent;
  581. {
  582.   int k,n,nn,bestval,moveval,prob;
  583.   n = 0;
  584.   if (imoves == 0)
  585.     return(NIL);
  586.   goodmove[0] = NIL;
  587.   bestval = -32000;
  588.   for (k = 0;k<imoves;k++) {
  589.     if ((moveval = eval(player,opponent,k,&prob))<bestval)
  590.       continue;
  591.     if (moveval>bestval) {
  592.       bestval = moveval;
  593.       n = 0;
  594.     }
  595.     if (n<MAXGMOV) {
  596.       goodmove[n] = k;
  597.       probmove[n++] = prob;
  598.     }
  599.   }
  600.   if (level == 'e' && n>1) {
  601.     nn = n;
  602.     n = 0;
  603.     prob = 32000;
  604.     for (k = 0;k<nn;k++) {
  605.       if ((moveval = probmove[k])>prob)
  606.         continue;
  607.       if (moveval<prob) {
  608.         prob = moveval;
  609.         n = 0;
  610.       }
  611.       goodmove[n] = goodmove[k];
  612.       probmove[n++] = probmove[k];
  613.     }
  614.   }
  615.   return(goodmove[(rand()%n)]);
  616. }
  617.  
  618. int eval(player,opponent,k,prob)
  619. int *player,*opponent,k,*prob;
  620. {
  621.   int newtry[31],newother[31],*r,*q,*p,n,sum,rear;
  622.   int ii,lastwhite,lastred;
  623.   *prob = sum = 0;
  624.   r = player+25;
  625.   p = newtry;
  626.   q = newother;
  627.   while(player<r) {
  628.     *p++= *player++;
  629.     *q++= *opponent++;
  630.   }
  631.   q = newtry+31;
  632.  
  633.   for (p = newtry+25;p<q;) *p++= 0;     /* Zero out spaces for hit pieces */
  634.   for (n = 0;n<4;n++) {
  635.     if (moves[k].pos[n] == NIL)
  636.       break;
  637.     newtry[moves[k].pos[n]]--;
  638.     newtry[ii = moves[k].pos[n]+moves[k].mov[n]]++;
  639.     if (ii<25 && newother[25-ii] == 1) {
  640.       newother[25-ii] = 0;
  641.       newother[0]++;
  642.       if (ii <= 15 && level == 'e')
  643.         sum++;  /*hit if near other's home*/
  644.     }
  645.   }
  646.   for (lastred = 0;newother[lastred] == 0;lastred++);
  647.   for (lastwhite = 0;newtry[lastwhite] == 0;lastwhite++);
  648.   lastwhite = 25-lastwhite;
  649.   if (lastwhite <= 6 && lastwhite<lastred)
  650.     sum = 1000;
  651.  
  652.   /* Running game for expert level */
  653.   if (lastwhite<lastred && level == 'e' && lastwhite > 6) {
  654.     for (sum = 1000;lastwhite>6;lastwhite--)
  655.       sum = sum-lastwhite*newtry[25-lastwhite];
  656.   }
  657.  
  658.   /* Find opponent's rear piece */
  659.   for (rear = 0;rear<25;rear++)
  660.     if (newother[rear] != 0)
  661.       break;
  662.   q = newtry+25;
  663.  
  664.   /* Closed points are ok */
  665.   for (p = newtry+1;p<q;)
  666.     if (*p++ > 1)
  667.       sum++;
  668.  
  669.   /* As much as possible, bear off pieces only if the inner board is safe */
  670.   if (rear>5) { 
  671.     q = newtry+31;
  672.     p = newtry+25;
  673.  
  674.     /* Use one dice number for each piece so as to bear off economically  */
  675.     for (n = 6;p<q;n--)
  676.       sum += *p++ * n;
  677.   }
  678.  
  679.   /* Blots past this point can't be hit */
  680.   if (level != 'b') {
  681.     r = newtry+25-rear;
  682.  
  683.     /* Blots are bad after the first six points   */
  684.     for (p = newtry+7;p<r;)
  685.       if (*p++== 1)
  686.         sum--;
  687.  
  688.     /* Try not to be on first three points */
  689.     q = newtry+3;
  690.     for (p = newtry;p<q;)
  691.       sum-= *p++;
  692.   }
  693.  
  694.   for (n = 1;n <= 4;n++)
  695.     *prob += n*getprob(newtry,newother,6*n-5,6*n);
  696.   return(sum);
  697. }
  698.  
  699. /* Return the probability (times 102) that pieces belonging to 'player' 
  700.    between the player's 'start' and 'finish' points will be hit by a 
  701.    piece belonging to 'opponent' */
  702. int getprob(player,opponent,start,finish)
  703. int *player,*opponent,start,finish;
  704. {
  705.   int k, n, sum;
  706.   sum = 0;
  707.   for ( ; start <= finish; start++) {
  708.     if (player[start] == 1) {
  709.       for (k = 1; k <= 12; k++) {
  710.         if ((n = 25 - start - k) < 0)
  711.           break;
  712.         if (opponent[n] != 0)
  713.           sum += prob[k];
  714.       }
  715.     }
  716.   }
  717.   return(sum);
  718. }
  719.  
  720. /* Count pieces belonging to 'player' */
  721. int piececount(player,startrow,endrow)
  722. int *player,startrow,endrow;
  723. {
  724.   int sum;
  725.   sum = 0;
  726.   while(startrow <= endrow)
  727.     sum+= player[startrow++];
  728.   return(sum);
  729. }
  730.  
  731. /* Count red pieces */
  732. int redcount()
  733. {
  734.   int i, count;
  735.   for (count = 0, i = 0; i < 25; i++) count += red[i];
  736.   return count;
  737. }
  738.  
  739. /* BOOKKEEPING.BOOKKEEPING.BOOKKEEPING.BOOKKEEPING.   */
  740. /* BOOKKEEPING.BOOKKEEPING.BOOKKEEPING.BOOKKEEPING.   */
  741. /* BOOKKEEPING.BOOKKEEPING.BOOKKEEPING.BOOKKEEPING.   */
  742. /* BOOKKEEPING.BOOKKEEPING.BOOKKEEPING.BOOKKEEPING.   */
  743.  
  744. /* Generate two numbers, die1 and die2, for the dice */
  745. roll()
  746. {
  747.   long iths;
  748.   time(&iths);
  749.   srand(iths);
  750.  
  751.   die1 = ((((rand()%6)+12)%6)+1);
  752.   die2 = ((((rand()%6)+12)%6)+1);
  753. }
  754.  
  755. /* Order the dice, larger die first */
  756. order()
  757. {
  758.   int temp;
  759.   if (die1 < die2) {
  760.     temp = die1;
  761.     die1 = die2;
  762.     die2 = temp;
  763.   }
  764. }
  765.  
  766. /* Return first non-whitespace character in string s: */
  767. char strips(s)
  768. char *s;
  769. {
  770.   char u;
  771.   while (isspace(u = *s++));
  772.   return tolower(u);
  773. }
  774.