PageRenderTime 22ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/games/monop/misc.c

https://bitbucket.org/kmv/aeriebsd-src
C | 298 lines | 227 code | 10 blank | 61 comment | 47 complexity | 1bf0f15b842a772832e1578aca2d8924 MD5 | raw file
  1. /*
  2. * Copyright (c) 1980, 1993
  3. * The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #ifndef lint
  30. #if 0
  31. static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 5/31/93";
  32. #else
  33. static const char rcsid[] = "$ABSD$";
  34. #endif
  35. #endif /* not lint */
  36. #include "monop.ext"
  37. #include <ctype.h>
  38. #include <signal.h>
  39. /*
  40. * This routine executes a truncated set of commands until a
  41. * "yes or "no" answer is gotten.
  42. */
  43. int
  44. getyn(prompt)
  45. char *prompt;
  46. {
  47. int com;
  48. for (;;)
  49. if ((com=getinp(prompt, yn)) < 2)
  50. return com;
  51. else
  52. (*func[com-2])();
  53. }
  54. /*
  55. * This routine tells the player if he's out of money.
  56. */
  57. void
  58. notify()
  59. {
  60. if (cur_p->money < 0)
  61. printf("That leaves you $%d in debt\n", -cur_p->money);
  62. else if (cur_p->money == 0)
  63. printf("that leaves you broke\n");
  64. else if (fixing && !told_em && cur_p->money > 0) {
  65. printf("-- You are now Solvent ---\n");
  66. told_em = TRUE;
  67. }
  68. }
  69. /*
  70. * This routine switches to the next player
  71. */
  72. void
  73. next_play()
  74. {
  75. player = (player + 1) % num_play;
  76. cur_p = &play[player];
  77. num_doub = 0;
  78. }
  79. /*
  80. * This routine gets an integer from the keyboard after the
  81. * given prompt.
  82. */
  83. int
  84. get_int(prompt)
  85. char *prompt;
  86. {
  87. int num, snum;
  88. char *sp;
  89. int c, i;
  90. char buf[257];
  91. for (;;) {
  92. printf("%s", prompt);
  93. num = 0;
  94. i = 1;
  95. for (sp = buf; (c = getchar()) != '\n';) {
  96. if (c == EOF) {
  97. printf("user closed input stream, quitting...\n");
  98. exit(0);
  99. }
  100. *sp = c;
  101. if (i < (int)sizeof(buf)) {
  102. i++;
  103. sp++;
  104. }
  105. }
  106. *sp = c;
  107. if (sp == buf)
  108. continue;
  109. for (sp = buf; isspace(*sp); sp++)
  110. ;
  111. for (; isdigit(*sp); sp++) {
  112. snum = num;
  113. num = num * 10 + *sp - '0';
  114. if (num < snum) {
  115. printf("Number too large - ");
  116. *(sp + 1) = 'X'; /* Force a break */
  117. }
  118. }
  119. /* Be kind to trailing spaces */
  120. for (; *sp == ' '; sp++)
  121. ;
  122. if (*sp == '\n')
  123. return num;
  124. else
  125. printf("I can't understand that\n");
  126. }
  127. }
  128. /*
  129. * This routine sets the monopoly flag from the list given.
  130. */
  131. void
  132. set_ownlist(pl)
  133. int pl;
  134. {
  135. int num; /* general counter */
  136. MON *orig; /* remember starting monop ptr */
  137. OWN *op; /* current owned prop */
  138. OWN *orig_op; /* origianl prop before loop */
  139. op = play[pl].own_list;
  140. #ifdef DEBUG
  141. printf("op [%p] = play[pl [%d] ].own_list;\n", op, pl);
  142. #endif
  143. while (op) {
  144. #ifdef DEBUG
  145. printf("op->sqr->type = %d\n", op->sqr->type);
  146. #endif
  147. switch (op->sqr->type) {
  148. case UTIL:
  149. #ifdef DEBUG
  150. printf(" case UTIL:\n");
  151. #endif
  152. for (num = 0; op && op->sqr->type == UTIL; op = op->next)
  153. num++;
  154. play[pl].num_util = num;
  155. #ifdef DEBUG
  156. printf("play[pl].num_util = num [%d];\n", num);
  157. #endif
  158. break;
  159. case RR:
  160. #ifdef DEBUG
  161. printf(" case RR:\n");
  162. #endif
  163. for (num = 0; op && op->sqr->type == RR; op = op->next) {
  164. #ifdef DEBUG
  165. printf("iter: %d\n", num);
  166. printf("op = %p, op->sqr = %p, op->sqr->type = %d\n", op, op->sqr, op->sqr->type);
  167. #endif
  168. num++;
  169. }
  170. play[pl].num_rr = num;
  171. #ifdef DEBUG
  172. printf("play[pl].num_rr = num [%d];\n", num);
  173. #endif
  174. break;
  175. case PRPTY:
  176. #ifdef DEBUG
  177. printf(" case PRPTY:\n");
  178. #endif
  179. orig = op->sqr->desc->mon_desc;
  180. orig_op = op;
  181. num = 0;
  182. while (op && op->sqr->desc->mon_desc == orig) {
  183. #ifdef DEBUG
  184. printf("iter: %d\n", num);
  185. #endif
  186. num++;
  187. #ifdef DEBUG
  188. printf("op = op->next ");
  189. #endif
  190. op = op->next;
  191. #ifdef DEBUG
  192. printf("[%p];\n", op);
  193. #endif
  194. }
  195. #ifdef DEBUG
  196. printf("num = %d\n", num);
  197. #endif
  198. if (orig == NULL) {
  199. printf("panic: bad monopoly descriptor: orig = %p\n", orig);
  200. printf("player # %d\n", pl+1);
  201. printhold(pl);
  202. printf("orig_op = %p\n", orig_op);
  203. if (orig_op) {
  204. printf("orig_op->sqr->type = %d (PRPTY)\n",
  205. orig_op->sqr->type);
  206. printf("orig_op->next = %p\n",
  207. orig_op->next);
  208. printf("orig_op->sqr->desc = %p\n",
  209. orig_op->sqr->desc);
  210. }
  211. printf("op = %p\n", op);
  212. if (op) {
  213. printf("op->sqr->type = %d (PRPTY)\n",
  214. op->sqr->type);
  215. printf("op->next = %p\n", op->next);
  216. printf("op->sqr->desc = %p\n",
  217. op->sqr->desc);
  218. }
  219. printf("num = %d\n", num);
  220. exit(1);
  221. }
  222. #ifdef DEBUG
  223. printf("orig->num_in = %d\n", orig->num_in);
  224. #endif
  225. if (num == orig->num_in)
  226. is_monop(orig, pl);
  227. else
  228. isnot_monop(orig);
  229. break;
  230. }
  231. }
  232. }
  233. /*
  234. * This routine sets things up as if it is a new monopoly
  235. */
  236. void
  237. is_monop(mp, pl)
  238. MON *mp;
  239. int pl;
  240. {
  241. int i;
  242. mp->owner = pl;
  243. mp->num_own = mp->num_in;
  244. for (i = 0; i < mp->num_in; i++)
  245. mp->sq[i]->desc->monop = TRUE;
  246. mp->name = mp->mon_n;
  247. }
  248. /*
  249. * This routine sets things up as if it is no longer a monopoly
  250. */
  251. void
  252. isnot_monop(mp)
  253. MON *mp;
  254. {
  255. int i;
  256. mp->owner = -1;
  257. for (i = 0; i < mp->num_in; i++)
  258. mp->sq[i]->desc->monop = FALSE;
  259. mp->name = mp->not_m;
  260. }
  261. /*
  262. * This routine gives a list of the current player's routine
  263. */
  264. void
  265. list()
  266. {
  267. printhold(player);
  268. }
  269. /*
  270. * This routine gives a list of a given players holdings
  271. */
  272. void
  273. list_all()
  274. {
  275. int pl;
  276. while ((pl=getinp("Whose holdings do you want to see? ", name_list)) < num_play)
  277. printhold(pl);
  278. }
  279. /*
  280. * This routine gives the players a chance before it exits.
  281. */
  282. void
  283. quit()
  284. {
  285. putchar('\n');
  286. if (getyn("Do you all really want to quit? ") == 0)
  287. exit(0);
  288. }