misc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* $NetBSD: misc.c,v 1.13 2004/11/05 21:30:32 dsl 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. #if 0
  33. static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: misc.c,v 1.13 2004/11/05 21:30:32 dsl Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include "monop.ext"
  39. #include <ctype.h>
  40. #include <signal.h>
  41. /*
  42. * This routine executes a truncated set of commands until a
  43. * "yes or "no" answer is gotten.
  44. */
  45. int
  46. getyn(prompt)
  47. const char *prompt;
  48. {
  49. int com;
  50. for (;;)
  51. if ((com=getinp(prompt, yncoms)) < 2)
  52. return com;
  53. else
  54. (*func[com-2])();
  55. }
  56. /*
  57. * This routine tells the player if he's out of money.
  58. */
  59. void
  60. notify()
  61. {
  62. if (cur_p->money < 0)
  63. printf("That leaves you $%d in debt\n", -cur_p->money);
  64. else if (cur_p->money == 0)
  65. printf("that leaves you broke\n");
  66. else if (fixing && !told_em && cur_p->money > 0) {
  67. printf("-- You are now Solvent ---\n");
  68. told_em = TRUE;
  69. }
  70. }
  71. /*
  72. * This routine switches to the next player
  73. */
  74. void
  75. next_play()
  76. {
  77. player = (player + 1) % num_play;
  78. cur_p = &play[player];
  79. num_doub = 0;
  80. }
  81. /*
  82. * This routine gets an integer from the keyboard after the
  83. * given prompt.
  84. */
  85. int
  86. get_int(prompt)
  87. const char *prompt;
  88. {
  89. int num;
  90. char *sp;
  91. int c;
  92. char buf[257];
  93. for (;;) {
  94. inter:
  95. printf(prompt);
  96. num = 0;
  97. for (sp = buf; (c=getchar()) != '\n'; *sp++ = c)
  98. if (c == -1) /* check for interrupted system call */
  99. goto inter;
  100. *sp = c;
  101. if (sp == buf)
  102. continue;
  103. for (sp = buf; isspace((unsigned char)*sp); sp++)
  104. continue;
  105. for (; isdigit((unsigned char)*sp); sp++)
  106. num = num * 10 + *sp - '0';
  107. if (*sp == '\n')
  108. return num;
  109. else
  110. printf("I can't understand that\n");
  111. }
  112. }
  113. /*
  114. * This routine sets the monopoly flag from the list given.
  115. */
  116. void
  117. set_ownlist(pl)
  118. int pl;
  119. {
  120. int num; /* general counter */
  121. MON *orig; /* remember starting monop ptr */
  122. OWN *op; /* current owned prop */
  123. OWN *orig_op; /* origianl prop before loop */
  124. op = play[pl].own_list;
  125. #ifdef DEBUG
  126. printf("op [%d] = play[pl [%d] ].own_list;\n", op, pl);
  127. #endif
  128. while (op) {
  129. #ifdef DEBUG
  130. printf("op->sqr->type = %d\n", op->sqr->type);
  131. #endif
  132. switch (op->sqr->type) {
  133. case UTIL:
  134. #ifdef DEBUG
  135. printf(" case UTIL:\n");
  136. #endif
  137. for (num = 0; op && op->sqr->type == UTIL;
  138. op = op->next)
  139. num++;
  140. play[pl].num_util = num;
  141. #ifdef DEBUG
  142. printf("play[pl].num_util = num [%d];\n", num);
  143. #endif
  144. break;
  145. case RR:
  146. #ifdef DEBUG
  147. printf(" case RR:\n");
  148. #endif
  149. for (num = 0; op && op->sqr->type == RR;
  150. op = op->next) {
  151. #ifdef DEBUG
  152. printf("iter: %d\n", num);
  153. printf("op = %d, op->sqr = %d, "
  154. "op->sqr->type = %d\n", op, op->sqr,
  155. op->sqr->type);
  156. #endif
  157. num++;
  158. }
  159. play[pl].num_rr = num;
  160. #ifdef DEBUG
  161. printf("play[pl].num_rr = num [%d];\n", num);
  162. #endif
  163. break;
  164. case PRPTY:
  165. #ifdef DEBUG
  166. printf(" case PRPTY:\n");
  167. #endif
  168. orig = op->sqr->desc->mon_desc;
  169. orig_op = op;
  170. num = 0;
  171. while (op && op->sqr->desc->mon_desc == orig) {
  172. #ifdef DEBUG
  173. printf("iter: %d\n", num);
  174. #endif
  175. num++;
  176. #ifdef DEBUG
  177. printf("op = op->next ");
  178. #endif
  179. op = op->next;
  180. #ifdef DEBUG
  181. printf("[%d];\n", op);
  182. #endif
  183. }
  184. #ifdef DEBUG
  185. printf("num = %d\n");
  186. #endif
  187. if (orig == 0) {
  188. printf("panic: bad monopoly descriptor: "
  189. "orig = %p\n", orig);
  190. printf("player # %d\n", pl+1);
  191. printhold(pl);
  192. printf("orig_op = %p\n", orig_op);
  193. printf("orig_op->sqr->type = %d (PRPTY)\n",
  194. op->sqr->type);
  195. printf("orig_op->next = %p\n", op->next);
  196. printf("orig_op->sqr->desc = %p\n",
  197. op->sqr->desc);
  198. printf("op = %p\n", op);
  199. printf("op->sqr->type = %d (PRPTY)\n",
  200. op->sqr->type);
  201. printf("op->next = %p\n", op->next);
  202. printf("op->sqr->desc = %p\n", op->sqr->desc);
  203. printf("num = %d\n", num);
  204. }
  205. #ifdef DEBUG
  206. printf("orig->num_in = %d\n", orig->num_in);
  207. #endif
  208. if (num == orig->num_in)
  209. is_monop(orig, pl);
  210. else
  211. is_not_monop(orig);
  212. break;
  213. }
  214. }
  215. }
  216. /*
  217. * This routine sets things up as if it is a new monopoly
  218. */
  219. void
  220. is_monop(mp, pl)
  221. MON *mp;
  222. int pl;
  223. {
  224. int i;
  225. mp->owner = pl;
  226. mp->num_own = mp->num_in;
  227. for (i = 0; i < mp->num_in; i++)
  228. mp->sq[i]->desc->monop = TRUE;
  229. mp->name = mp->mon_n;
  230. }
  231. /*
  232. * This routine sets things up as if it is no longer a monopoly
  233. */
  234. void
  235. is_not_monop(mp)
  236. MON *mp;
  237. {
  238. int i;
  239. mp->owner = -1;
  240. for (i = 0; i < mp->num_in; i++)
  241. mp->sq[i]->desc->monop = FALSE;
  242. mp->name = mp->not_m;
  243. }
  244. /*
  245. * This routine gives a list of the current player's routine
  246. */
  247. void
  248. list()
  249. {
  250. printhold(player);
  251. }
  252. /*
  253. * This routine gives a list of a given players holdings
  254. */
  255. void
  256. list_all()
  257. {
  258. int pl;
  259. while ((pl = getinp("Whose holdings do you want to see? ", name_list))
  260. < num_play)
  261. printhold(pl);
  262. }
  263. /*
  264. * This routine gives the players a chance before it exits.
  265. */
  266. void
  267. quit()
  268. {
  269. putchar('\n');
  270. if (getyn("Do you all really want to quit? ") == 0)
  271. exit(0);
  272. }