home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / yahtzee.t.Z / yahtzee.t / yz.c < prev   
C/C++ Source or Header  |  1988-07-29  |  6KB  |  288 lines

  1. /* yz.c
  2.  *    main function and declaration of the important global
  3.  *    data structures.
  4.  */
  5. #include <stdio.h>
  6. #include <curses.h>
  7. #include <signal.h>
  8. #include "defs.h"
  9.  
  10. /* sleep period at end of game if no delay flag is given to yahtzee */
  11. #define Non_Stop_Wait 10
  12. #define TicToc fprintf(stderr, "."); fflush(stderr);
  13.  
  14. /* y and x coordinates for the display of the 5 dice */
  15. int diey[Five_Dice] = {0, 0, 0, 0, 0}, diex[Five_Dice] = {0, 12, 24, 36, 48};
  16.  
  17. /* scoreboard stores scores for all players for each category, machine
  18.  * is a boolean to determine if the given player is a human or
  19.  * computer simulation */
  20. int scoreboard[max_players][max_marks], machine[max_players];
  21. int subtotals[max_players];
  22.  
  23. /* used to form category availability list for all players */
  24. int available[13], avail_count;
  25. WINDOW *screen;
  26.  
  27. /* users that don't like the default curses standout mode and don't
  28.  * want to change it themselves can avoid it my setting this boolean
  29.  * at startup */
  30. int BadStandout = FALSE;
  31.  
  32. /* boolean for non-stop play, set at startup */
  33. int non_stop = FALSE;
  34.  
  35. /* me */
  36. #ifndef BSD
  37. static char id[] = "@(#)yz.c 1.2    Yahtzee -  Stacey Campbell";
  38. #endif
  39.  
  40. #ifdef BSD
  41. extern long random();
  42. extern void srandom();
  43. #else
  44. extern long random();
  45. extern void srandom();
  46. #endif
  47. extern byebye();
  48.  
  49. main(argc, argv)
  50.  
  51. int argc;
  52. char *argv[];
  53.  
  54.     {
  55.     int mark, player, player_count, round, i, dice[Five_Dice],
  56.         hold[Five_Dice], totals[max_players], best_player;
  57.     extern long time();
  58.     extern int old_dice[Five_Dice];
  59.     char ch = ' ', arg[50];
  60.  
  61. /* boot the seed for the random number generator */
  62. #ifdef BSD
  63.     srandom((int) time(0));
  64. #else
  65.     srandom(time((long *) 0));
  66. #endif
  67.     TicToc
  68.  
  69. /* initialise curses */
  70.     initscr();
  71.     TicToc
  72.  
  73. /* create the game window */
  74.     screen = newwin(0, 0, 0, 0);
  75.     TicToc
  76.  
  77. /* process arguments */
  78.     for (i = 1; i < argc; ++i)
  79.         {
  80.         strcpy(arg, argv[i]);
  81.  
  82. /* flag for non-stop play */
  83.         if (strcmp("-N", arg) == 0)
  84.             non_stop = TRUE;
  85.  
  86. /* flag to avoid using standout mode of terminal */
  87.         else if (strcmp("-d", arg) == 0)
  88.             BadStandout = TRUE;
  89.  
  90. /* display score and exit */
  91.         else if (strcmp("-s", arg) == 0)
  92.             yahtzee_exit(dis_score((WINDOW *) 0));
  93.  
  94. /* put options on stdout then exit */
  95.         else if (strcmp("-h", arg) == 0)
  96.             {
  97.             option_message(argv[0]);
  98.             yahtzee_exit(0);
  99.             }
  100.         else
  101.             {
  102.             fprintf(stderr, "Unknown argument: %s\n", arg);
  103.             fprintf(stderr, "Try: %s -h\n", argv[0]);
  104.             sleep(4);
  105.             }
  106.         }
  107.  
  108. /* trap signals for clean exit */
  109. _siginit();
  110.     signal(SIGINT, byebye);
  111.     signal(SIGQUIT, byebye);
  112. /*    signal(SIGTERM, byebye); */
  113.     TicToc
  114.  
  115. /* set crmode and noecho for the terminal */
  116.     crmode();
  117.     noecho();
  118.  
  119. /* determine player count and which players will be simulated */
  120.     player_count = player_info(machine, screen);
  121.  
  122. /* loop until user quits */
  123.     do
  124.         {
  125.  
  126. /* setup the play screen */
  127.         werase(screen);
  128.         dis_side();
  129.         for (i = 0; i < Five_Dice; ++i)
  130.             {
  131.             old_dice[i] = 0;
  132.             hold[i] = FALSE;
  133.             }
  134.  
  135. /* initialise dice to anything */
  136.         roll_dice(dice, hold);
  137.         dis_dice(dice, hold);
  138.         if (! BadStandout)
  139.             wstandout(screen);
  140.  
  141. /* initialise the scoreboard */
  142.         for (player = 0; player < player_count; ++player)
  143.             {
  144.             subtotals[player] = 0;
  145.             for (mark = 0; mark < max_marks; ++mark)
  146.                 scoreboard[player][mark] = category_available;
  147.             mvwprintw(screen, 6, player * 10 + 27, "%d", player);
  148.             }
  149.         if (! BadStandout)
  150.             wstandend(screen);
  151.         mvwaddstr(screen, 2, 59, "-- y a h t z e e --");
  152.         wmove(screen, 3, 68);
  153.         wrefresh(screen);
  154.  
  155. /* start the game */
  156.         for (round = 0; round < 13; ++round)
  157.             for (player = 0; player < player_count; ++player)
  158.                 {
  159.  
  160. /* player is human then form availability list */
  161.                 if (! machine[player])
  162.                     human_availability(player);
  163.  
  164. /* stage 1: roll dice then get move */
  165.                 roll_dice(dice, hold);
  166.                 get_move(player, dice, hold);
  167.                 if (machine[player])
  168.                     sleep(4);
  169.  
  170. /* stage 2: if all dice have not been held then do it again */
  171.                 if (! all_held(hold))
  172.                     {
  173.                     roll_dice(dice, hold);
  174.                     get_move(player, dice, hold);
  175.                     if (machine[player])
  176.                         sleep(4);
  177.                     roll_dice(dice, hold);
  178.                     }
  179.  
  180. /* display the dice */
  181.                 if (BadStandout)
  182.                     dis_dice(dice, hold);
  183.                 for (i = 0; i < Five_Dice; ++i)
  184.                     hold[i] = FALSE;
  185.                 if (! BadStandout)
  186.                     dis_dice(dice, hold);
  187.  
  188. /* get selection of best category */
  189.                 if (machine[player])
  190.                     mark = computer_select(player, dice,
  191.                         available, avail_count);
  192.                 else
  193.                     mark = human_select(player, dice);
  194.  
  195. /* update the score board (not the HS board!) */
  196.                 update_score(player, mark, dice);
  197.  
  198. /* have a little nap if a computer simulation */
  199.                 if (machine[player])
  200.                     sleep(4);
  201.                 }
  202.  
  203. /* determine who won */
  204.         best_player = finish(totals, player_count);
  205.         wrefresh(screen);
  206.  
  207. /* attempt to put the best players score in the HS file */
  208.         update_high(best_player, totals[best_player]);
  209.         if (! non_stop)
  210.             {
  211.  
  212. /* determine if user wishes to continue */
  213.             mvwaddstr(screen, 5, 0,
  214.                 "--(q)uit, (c)ontinue, (r)econfigure--");
  215. #ifndef SYS5_3
  216.             wrefresh(screen);
  217. #endif
  218.             do
  219.                 {
  220.                 ch = wgetch(screen);
  221.                 switch (ch)
  222.                     {
  223.  
  224. /* get help */
  225.                     case '?' : help_out(1, screen);
  226.                            break;
  227.  
  228. /* display the current HS file */
  229.                     case 's' : dis_score(screen);
  230.                            break;
  231.  
  232. /* redraw the current window */
  233.                     case Form_Feed : redraw(screen);
  234.                            break;
  235.  
  236. /* display the rule book */
  237.                     case 'b' : rools(screen);
  238.                            break;
  239.  
  240. /* reconfigure the players */
  241.                     case 'r' : player_count =
  242.                             player_info(machine,
  243.                             screen);
  244.                           break;
  245.  
  246. /* get a shell */
  247.                     case '!' : shell(screen);
  248.                            break;
  249.                     case 'v' : version(screen);
  250.                            break;
  251. #if defined(SYS5) || defined(SYS5_3)
  252.  
  253. /* get a shell window */
  254.                     case '$' : shwin(screen);
  255.                            break;
  256.                     case ' ' :
  257.                     case 'q' :
  258.                     case 'c' : break;
  259.                     default : flash();
  260.                           break;
  261. #endif
  262.                     }
  263.                 } while (ch != ' ' && ch != 'q' && ch != 'c');
  264.             }
  265.         else
  266.             {
  267.             dis_score(screen);
  268.             sleep(Non_Stop_Wait);
  269.             }
  270.         } while (ch != 'q');
  271.     dis_score((WINDOW *) 0);
  272.     yahtzee_exit(0);
  273.     }
  274.  
  275. /* return true if all dice are held */
  276. int all_held(hold)
  277.  
  278. int hold[Five_Dice];
  279.  
  280.     {
  281.     register int i;
  282.  
  283.     for (i = 0; i < Five_Dice; ++i)
  284.         if (! hold[i])
  285.             return(FALSE);
  286.     return(TRUE);
  287.     }
  288.