home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / games / monop / monop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-08  |  4.4 KB  |  163 lines

  1. /*
  2.  * Copyright (c) 1980 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)monop.c    5.7 (Berkeley) 6/1/90";
  42. #endif /* not lint */
  43.  
  44. # include    "monop.def"
  45.  
  46. /*
  47.  *    This program implements a monopoly game
  48.  */
  49. main(ac, av)
  50. reg int        ac;
  51. reg char    *av[]; {
  52.  
  53.  
  54.     srand(getpid());
  55.     if (ac > 1) {
  56.         if (!rest_f(av[1]))
  57.             restore();
  58.     }
  59.     else {
  60.         getplayers();
  61.         init_players();
  62.         init_monops();
  63.     }
  64.     num_luck = sizeof lucky_mes / sizeof (char *);
  65.     init_decks();
  66.     signal(2, quit);
  67.     for (;;) {
  68.         printf("\n%s (%d) (cash $%d) on %s\n", cur_p->name, player + 1,
  69.             cur_p->money, board[cur_p->loc].name);
  70.         printturn();
  71.         force_morg();
  72.         execute(getinp("-- Command: ", comlist));
  73.     }
  74. }
  75. /*
  76.  *    This routine gets the names of the players
  77.  */
  78. getplayers() {
  79.  
  80.     reg char    *sp;
  81.     reg int        i, j;
  82.     char        buf[257];
  83.  
  84. blew_it:
  85.     for (;;) {
  86.         if ((num_play=get_int("How many players? ")) <= 0 ||
  87.             num_play > MAX_PL)
  88.             printf("Sorry. Number must range from 1 to 9\n");
  89.         else
  90.             break;
  91.     }
  92.     cur_p = play = (PLAY *) calloc(num_play, sizeof (PLAY));
  93.     for (i = 0; i < num_play; i++) {
  94. over:
  95.         printf("Player %d's name: ", i + 1);
  96.         for (sp = buf; (*sp=getchar()) != '\n'; sp++)
  97.             continue;
  98.         if (sp == buf)
  99.             goto over;
  100.         *sp++ = '\0';
  101.         strcpy(name_list[i]=play[i].name=(char *)calloc(1,sp-buf),buf);
  102.         play[i].money = 1500;
  103.     }
  104.     name_list[i++] = "done";
  105.     name_list[i] = 0;
  106.     for (i = 0; i < num_play; i++)
  107.         for (j = i + 1; j < num_play; j++)
  108.             if (strcasecmp(name_list[i], name_list[j]) == 0) {
  109.                 if (i != num_play - 1)
  110.                     printf("Hey!!! Some of those are IDENTICAL!!  Let's try that again....\n");
  111.                 else
  112.                     printf("\"done\" is a reserved word.  Please try again\n");
  113.                 for (i = 0; i < num_play; i++)
  114.                     cfree(play[i].name);
  115.                 cfree(play);
  116.                 goto blew_it;
  117.             }
  118. }
  119. /*
  120.  *    This routine figures out who goes first
  121.  */
  122. init_players() {
  123.  
  124.     reg int    i, rl, cur_max;
  125.     bool    over;
  126.     int    max_pl;
  127.  
  128. again:
  129.     putchar('\n');
  130.     for (cur_max = i = 0; i < num_play; i++) {
  131.         printf("%s (%d) rolls %d\n", play[i].name, i+1, rl=roll(2, 6));
  132.         if (rl > cur_max) {
  133.             over = FALSE;
  134.             cur_max = rl;
  135.             max_pl = i;
  136.         }
  137.         else if (rl == cur_max)
  138.             over++;
  139.     }
  140.     if (over) {
  141.         printf("%d people rolled the same thing, so we'll try again\n",
  142.             over + 1);
  143.         goto again;
  144.     }
  145.     player = max_pl;
  146.     cur_p = &play[max_pl];
  147.     printf("%s (%d) goes first\n", cur_p->name, max_pl + 1);
  148. }
  149. /*
  150.  *    This routine initalizes the monopoly structures.
  151.  */
  152. init_monops() {
  153.  
  154.     reg MON    *mp;
  155.     reg int    i;
  156.  
  157.     for (mp = mon; mp < &mon[N_MON]; mp++) {
  158.         mp->name = mp->not_m;
  159.         for (i = 0; i < mp->num_in; i++)
  160.             mp->sq[i] = &board[mp->sqnums[i]];
  161.     }
  162. }
  163.