133209Sbostic /*
2*60815Sbostic * Copyright (c) 1980, 1993
3*60815Sbostic * The Regents of the University of California. All rights reserved.
433209Sbostic *
542583Sbostic * %sccs.include.redist.c%
633209Sbostic */
733209Sbostic
833209Sbostic #ifndef lint
9*60815Sbostic static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 05/31/93";
1033209Sbostic #endif /* not lint */
1133209Sbostic
1233209Sbostic # include "monop.ext"
1333209Sbostic # include <ctype.h>
1433209Sbostic # include <signal.h>
1533209Sbostic
1633209Sbostic /*
1733209Sbostic * This routine executes a truncated set of commands until a
1833209Sbostic * "yes or "no" answer is gotten.
1933209Sbostic */
getyn(prompt)2033209Sbostic getyn(prompt)
2133209Sbostic reg char *prompt; {
2233209Sbostic
2333209Sbostic reg int com;
2433209Sbostic
2533209Sbostic for (;;)
2633209Sbostic if ((com=getinp(prompt, yn)) < 2)
2733209Sbostic return com;
2833209Sbostic else
2933209Sbostic (*func[com-2])();
3033209Sbostic }
3133209Sbostic /*
3233209Sbostic * This routine tells the player if he's out of money.
3333209Sbostic */
notify()3433209Sbostic notify() {
3533209Sbostic
3633209Sbostic if (cur_p->money < 0)
3733209Sbostic printf("That leaves you $%d in debt\n", -cur_p->money);
3833209Sbostic else if (cur_p->money == 0)
3933209Sbostic printf("that leaves you broke\n");
4033209Sbostic else if (fixing && !told_em && cur_p->money > 0) {
4133209Sbostic printf("-- You are now Solvent ---\n");
4233209Sbostic told_em = TRUE;
4333209Sbostic }
4433209Sbostic }
4533209Sbostic /*
4633209Sbostic * This routine switches to the next player
4733209Sbostic */
next_play()4833209Sbostic next_play() {
4933209Sbostic
5033209Sbostic player = ++player % num_play;
5133209Sbostic cur_p = &play[player];
5233209Sbostic num_doub = 0;
5333209Sbostic }
5433209Sbostic /*
5533209Sbostic * This routine gets an integer from the keyboard after the
5633209Sbostic * given prompt.
5733209Sbostic */
get_int(prompt)5833209Sbostic get_int(prompt)
5933209Sbostic reg char *prompt; {
6033209Sbostic
6133209Sbostic reg int num;
6233209Sbostic reg char *sp;
6333209Sbostic char buf[257];
6433209Sbostic
6533209Sbostic for (;;) {
6633209Sbostic inter:
6733209Sbostic printf(prompt);
6833209Sbostic num = 0;
6933209Sbostic for (sp = buf; (*sp=getchar()) != '\n'; sp++)
7033209Sbostic if (*sp == -1) /* check for interrupted system call */
7133209Sbostic goto inter;
7233209Sbostic if (sp == buf)
7333209Sbostic continue;
7433209Sbostic for (sp = buf; isspace(*sp); sp++)
7533209Sbostic continue;
7633209Sbostic for (; isdigit(*sp); sp++)
7733209Sbostic num = num * 10 + *sp - '0';
7833209Sbostic if (*sp == '\n')
7933209Sbostic return num;
8033209Sbostic else
8133209Sbostic printf("I can't understand that\n");
8233209Sbostic }
8333209Sbostic }
8433209Sbostic /*
8533209Sbostic * This routine sets the monopoly flag from the list given.
8633209Sbostic */
set_ownlist(pl)8733209Sbostic set_ownlist(pl)
8833209Sbostic int pl; {
8933209Sbostic
9033209Sbostic reg int num; /* general counter */
9133209Sbostic reg MON *orig; /* remember starting monop ptr */
9233209Sbostic reg OWN *op; /* current owned prop */
9333209Sbostic OWN *orig_op; /* origianl prop before loop */
9433209Sbostic
9533209Sbostic op = play[pl].own_list;
9633209Sbostic #ifdef DEBUG
9733209Sbostic printf("op [%d] = play[pl [%d] ].own_list;\n", op, pl);
9833209Sbostic #endif
9933209Sbostic while (op) {
10033209Sbostic #ifdef DEBUG
10133209Sbostic printf("op->sqr->type = %d\n", op->sqr->type);
10233209Sbostic #endif
10333209Sbostic switch (op->sqr->type) {
10433209Sbostic case UTIL:
10533209Sbostic #ifdef DEBUG
10633209Sbostic printf(" case UTIL:\n");
10733209Sbostic #endif
10833209Sbostic for (num = 0; op && op->sqr->type == UTIL; op = op->next)
10933209Sbostic num++;
11033209Sbostic play[pl].num_util = num;
11133209Sbostic #ifdef DEBUG
11233209Sbostic printf("play[pl].num_util = num [%d];\n", num);
11333209Sbostic #endif
11433209Sbostic break;
11533209Sbostic case RR:
11633209Sbostic #ifdef DEBUG
11733209Sbostic printf(" case RR:\n");
11833209Sbostic #endif
11933209Sbostic for (num = 0; op && op->sqr->type == RR; op = op->next) {
12033209Sbostic #ifdef DEBUG
12133209Sbostic printf("iter: %d\n", num);
12233209Sbostic printf("op = %d, op->sqr = %d, op->sqr->type = %d\n", op, op->sqr, op->sqr->type);
12333209Sbostic #endif
12433209Sbostic num++;
12533209Sbostic }
12633209Sbostic play[pl].num_rr = num;
12733209Sbostic #ifdef DEBUG
12833209Sbostic printf("play[pl].num_rr = num [%d];\n", num);
12933209Sbostic #endif
13033209Sbostic break;
13133209Sbostic case PRPTY:
13233209Sbostic #ifdef DEBUG
13333209Sbostic printf(" case PRPTY:\n");
13433209Sbostic #endif
13533209Sbostic orig = op->sqr->desc->mon_desc;
13633209Sbostic orig_op = op;
13733209Sbostic num = 0;
13833209Sbostic while (op && op->sqr->desc->mon_desc == orig) {
13933209Sbostic #ifdef DEBUG
14033209Sbostic printf("iter: %d\n", num);
14133209Sbostic #endif
14233209Sbostic num++;
14333209Sbostic #ifdef DEBUG
14433209Sbostic printf("op = op->next ");
14533209Sbostic #endif
14633209Sbostic op = op->next;
14733209Sbostic #ifdef DEBUG
14833209Sbostic printf("[%d];\n", op);
14933209Sbostic #endif
15033209Sbostic }
15133209Sbostic #ifdef DEBUG
15233209Sbostic printf("num = %d\n");
15333209Sbostic #endif
15433209Sbostic if (orig == 0) {
15533209Sbostic printf("panic: bad monopoly descriptor: orig = %d\n", orig);
15633209Sbostic printf("player # %d\n", pl+1);
15733209Sbostic printhold(pl);
15833209Sbostic printf("orig_op = %d\n", orig_op);
15933209Sbostic printf("orig_op->sqr->type = %d (PRPTY)\n", op->sqr->type);
16033209Sbostic printf("orig_op->next = %d\n", op->next);
16133209Sbostic printf("orig_op->sqr->desc = %d\n", op->sqr->desc);
16233209Sbostic printf("op = %d\n", op);
16333209Sbostic printf("op->sqr->type = %d (PRPTY)\n", op->sqr->type);
16433209Sbostic printf("op->next = %d\n", op->next);
16533209Sbostic printf("op->sqr->desc = %d\n", op->sqr->desc);
16633209Sbostic printf("num = %d\n", num);
16733209Sbostic }
16833209Sbostic #ifdef DEBUG
16933209Sbostic printf("orig->num_in = %d\n", orig->num_in);
17033209Sbostic #endif
17133209Sbostic if (num == orig->num_in)
17233209Sbostic is_monop(orig, pl);
17333209Sbostic else
17433209Sbostic isnot_monop(orig);
17533209Sbostic break;
17633209Sbostic }
17733209Sbostic }
17833209Sbostic }
17933209Sbostic /*
18033209Sbostic * This routine sets things up as if it is a new monopoly
18133209Sbostic */
is_monop(mp,pl)18233209Sbostic is_monop(mp, pl)
18333209Sbostic reg MON *mp;
18433209Sbostic int pl; {
18533209Sbostic
18633209Sbostic reg char *sp;
18733209Sbostic reg int i;
18833209Sbostic
18933209Sbostic mp->owner = pl;
19033209Sbostic mp->num_own = mp->num_in;
19133209Sbostic for (i = 0; i < mp->num_in; i++)
19233209Sbostic mp->sq[i]->desc->monop = TRUE;
19333209Sbostic mp->name = mp->mon_n;
19433209Sbostic }
19533209Sbostic /*
19633209Sbostic * This routine sets things up as if it is no longer a monopoly
19733209Sbostic */
isnot_monop(mp)19833209Sbostic isnot_monop(mp)
19933209Sbostic reg MON *mp; {
20033209Sbostic
20133209Sbostic reg char *sp;
20233209Sbostic reg int i;
20333209Sbostic
20433209Sbostic mp->owner = -1;
20533209Sbostic for (i = 0; i < mp->num_in; i++)
20633209Sbostic mp->sq[i]->desc->monop = FALSE;
20733209Sbostic mp->name = mp->not_m;
20833209Sbostic }
20933209Sbostic /*
21033209Sbostic * This routine gives a list of the current player's routine
21133209Sbostic */
list()21233209Sbostic list() {
21333209Sbostic
21433209Sbostic printhold(player);
21533209Sbostic }
21633209Sbostic /*
21733209Sbostic * This routine gives a list of a given players holdings
21833209Sbostic */
list_all()21933209Sbostic list_all() {
22033209Sbostic
22133209Sbostic reg int pl;
22233209Sbostic
22333209Sbostic while ((pl=getinp("Whose holdings do you want to see? ", name_list)) < num_play)
22433209Sbostic printhold(pl);
22533209Sbostic }
22633209Sbostic /*
22733209Sbostic * This routine gives the players a chance before it exits.
22833209Sbostic */
22946751Sbostic void
quit()23033209Sbostic quit() {
23133209Sbostic
23233209Sbostic putchar('\n');
23333209Sbostic if (getyn("Do you all really want to quit? ", yn) == 0)
23433209Sbostic exit(0);
23546751Sbostic signal(SIGINT, quit);
23633209Sbostic }
23733209Sbostic /*
23833209Sbostic * This routine copies one structure to another
23933209Sbostic */
cpy_st(s1,s2,size)24033209Sbostic cpy_st(s1, s2, size)
24133209Sbostic reg int *s1, *s2, size; {
24233209Sbostic
24333209Sbostic size /= 2;
24433209Sbostic while (size--)
24533209Sbostic *s1++ = *s2++;
24633209Sbostic }
247