home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume1 / yahtzee / main.c < prev    next >
C/C++ Source or Header  |  1987-05-08  |  2KB  |  184 lines

  1. #include "types.h"
  2. #include <sys/time.h>
  3. #include <signal.h>
  4.  
  5. Game player[4];
  6. char DELETE1, DELETE2;
  7.  
  8. main()
  9. {
  10.     int np, clean();
  11.  
  12.     seedrand();
  13.     initscr();
  14.     initkey(&DELETE1, &DELETE2);
  15.     signal(SIGINT, clean);
  16.     crmode();
  17.     noecho();
  18.     nonl();
  19.     np = getnames();
  20.     while(1) {
  21.         init(np);
  22.         if(playgame(np) == 0) break;
  23.         move(23,0);
  24.         clrtoeol();
  25.         addstr("Same Players? (Y/N) ");
  26.         refresh();
  27.         if(yes(23,21)) continue;
  28.         else np = getnames();
  29.     }
  30.     clean();
  31. }
  32.  
  33. clean()
  34. {
  35.     clear();
  36.     refresh();
  37.     endwin();
  38.     endkey();
  39.     exit(0);
  40. }
  41.  
  42. yes(y,x)
  43. int y,x;
  44. {
  45.     char s[2];
  46.  
  47.     while(1) {
  48.         getstring(s,1,0,  y,x);
  49.         s[0] |= 040;
  50.         if(s[0] == 'y') return(1);
  51.         else if(s[0] == 'n') return(0);
  52.         else {
  53.             DisplayBeep(0);
  54.             mvaddstr(y,x+1," ");
  55.             move(y,x+1);
  56.             refresh();
  57.         }
  58.     }
  59. }
  60.  
  61. init(n)
  62. int n;
  63. {
  64.     int i,j;
  65.  
  66.     for(i=0; i<n; i++)
  67.         for(j=0; j<NS; j++) player[i].score[j] = -1;
  68.     player[i].ycount = 0;
  69. }
  70.  
  71. playgame(n)
  72. int n;
  73. {
  74.     int which = 0, old = 0, i, j;
  75.  
  76.     drawscreen(n);
  77.     while(gameon(n)) {
  78.         setname(old,which);
  79.         roll();
  80.         for(i=0; i<2; i++) {
  81.             if(choose()) break;
  82.             roll();
  83.         }
  84.         clrroll();
  85.         makechoice(which);
  86.         old = which;
  87.         if(++which == n) which=0;
  88.     }
  89.     return(winner(n));
  90. }
  91.  
  92. gameon(n)
  93. {
  94.     int i,j;
  95.  
  96.     for(i=0; i<n; i++)
  97.         for(j=0; j<NS; j++) {
  98.             if(j == 6) continue;
  99.             if(player[i].score[j] == -1) return(1);
  100.         }
  101.     return(0);
  102. }
  103.  
  104. getnames()
  105. {
  106.     int n,i;
  107.     char s[7];
  108.  
  109.     clear();
  110.     mvaddstr(0,0,"How many players: ");
  111.     refresh();
  112.     while(1) {
  113.         move(0,18);
  114.         clrtoeol();
  115.         refresh();
  116.         getstring(s,1,1,0,18);
  117.         if((n=atoi(s)) >= 1 && n <= 4) break;
  118.         DisplayBeep(0);
  119.     }
  120.     for(i=0; i<n; i++) {
  121.         mvaddstr(i+1,0,"Enter name: ");
  122.         refresh();
  123.         move(i+1,12);
  124.         clrtoeol();
  125.         getstring(s, 6, 0,  i+1, 12);
  126.         strcpy(player[i].name, s);
  127.     }
  128.     return(n);
  129. }
  130.  
  131. getstring(s,n,flag,y,x)
  132. char *s;
  133. int n,flag,y,x;
  134. {
  135.     int c,i=0;
  136.  
  137.     while(1) {
  138.         c = key();
  139.         if(c < 0 && c!=LF_ARROW) DisplayBeep(0);
  140.         else if(c == 8 || c == LF_ARROW) {
  141.             if(i == 0) DisplayBeep(0);
  142.             else {
  143.                 s[--i] = 0;
  144.                 addch(8);
  145.                 addch(' ');
  146.                 addch(8);
  147.                 refresh();
  148.                 x--;
  149.             }
  150.         } else if(c == '\r') {
  151.             s[i]=0;
  152.             return;
  153.         } else {
  154.             if(i < n) {
  155.                 if(flag) {
  156.                     if(c<'0' || c>'9') {
  157.                         DisplayBeep(0);
  158.                         continue;
  159.                     }
  160.                 }
  161.                 s[i++] = c;
  162.                 x++;
  163.                 addch(c);
  164.                 refresh();
  165.             } else DisplayBeep(0);
  166.         }
  167.     }
  168. }
  169.  
  170. seedrand()
  171. {
  172.     struct timeval tp;
  173.     struct timezone tpz;
  174.  
  175.     gettimeofday(&tp,&tpz);
  176.     srandom((int)tp.tv_sec);
  177. }
  178.  
  179. get_rand()
  180. {
  181.     long random();
  182.     return( (int) random() % 6 + 1);
  183. }
  184.