monop.c 4.9 KB

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