133206Sbostic /*
2*60815Sbostic * Copyright (c) 1980, 1993
3*60815Sbostic * The Regents of the University of California. All rights reserved.
433206Sbostic *
542583Sbostic * %sccs.include.redist.c%
633206Sbostic */
733206Sbostic
833206Sbostic #ifndef lint
9*60815Sbostic static char sccsid[] = "@(#)execute.c 8.1 (Berkeley) 05/31/93";
1033206Sbostic #endif /* not lint */
1133206Sbostic
1233206Sbostic # include "monop.ext"
1333206Sbostic # include <sys/types.h>
1433206Sbostic # include <sys/stat.h>
1533206Sbostic # include <sys/time.h>
1633206Sbostic
1733206Sbostic # define SEGSIZE 8192
1833206Sbostic
1933206Sbostic typedef struct stat STAT;
2033206Sbostic typedef struct tm TIME;
2133206Sbostic
2233206Sbostic extern char etext[], /* end of text space */
2333206Sbostic rub();
2433206Sbostic
2533206Sbostic static char buf[257],
2633206Sbostic *yn_only[] = { "yes", "no"};
2733206Sbostic
2833206Sbostic static bool new_play; /* set if move on to new player */
2933206Sbostic
3033206Sbostic /*
3133206Sbostic * This routine executes the given command by index number
3233206Sbostic */
execute(com_num)3333206Sbostic execute(com_num)
3433206Sbostic reg int com_num; {
3533206Sbostic
3633206Sbostic new_play = FALSE; /* new_play is true if fixing */
3733206Sbostic (*func[com_num])();
3833206Sbostic notify();
3933206Sbostic force_morg();
4033206Sbostic if (new_play)
4133206Sbostic next_play();
4233206Sbostic else if (num_doub)
4333206Sbostic printf("%s rolled doubles. Goes again\n", cur_p->name);
4433206Sbostic }
4533206Sbostic /*
4633206Sbostic * This routine moves a piece around.
4733206Sbostic */
do_move()4833206Sbostic do_move() {
4933206Sbostic
5033206Sbostic reg int r1, r2;
5133206Sbostic reg bool was_jail;
5233206Sbostic
5333206Sbostic new_play = was_jail = FALSE;
5433206Sbostic printf("roll is %d, %d\n", r1=roll(1, 6), r2=roll(1, 6));
5533206Sbostic if (cur_p->loc == JAIL) {
5633206Sbostic was_jail++;
5733206Sbostic if (!move_jail(r1, r2)) {
5833206Sbostic new_play++;
5933206Sbostic goto ret;
6033206Sbostic }
6133206Sbostic }
6233206Sbostic else {
6333206Sbostic if (r1 == r2 && ++num_doub == 3) {
6433206Sbostic printf("That's 3 doubles. You go to jail\n");
6533206Sbostic goto_jail();
6633206Sbostic new_play++;
6733206Sbostic goto ret;
6833206Sbostic }
6933206Sbostic move(r1+r2);
7033206Sbostic }
7133206Sbostic if (r1 != r2 || was_jail)
7233206Sbostic new_play++;
7333206Sbostic ret:
7433206Sbostic return;
7533206Sbostic }
7633206Sbostic /*
7733206Sbostic * This routine moves a normal move
7833206Sbostic */
move(rl)7933206Sbostic move(rl)
8033206Sbostic reg int rl; {
8133206Sbostic
8233206Sbostic reg int old_loc;
8333206Sbostic
8433206Sbostic old_loc = cur_p->loc;
8533206Sbostic cur_p->loc = (cur_p->loc + rl) % N_SQRS;
8633206Sbostic if (cur_p->loc < old_loc && rl > 0) {
8733206Sbostic cur_p->money += 200;
8833206Sbostic printf("You pass %s and get $200\n", board[0].name);
8933206Sbostic }
9033206Sbostic show_move();
9133206Sbostic }
9233206Sbostic /*
9333206Sbostic * This routine shows the results of a move
9433206Sbostic */
show_move()9533206Sbostic show_move() {
9633206Sbostic
9733206Sbostic reg SQUARE *sqp;
9833206Sbostic
9933206Sbostic sqp = &board[cur_p->loc];
10033206Sbostic printf("That puts you on %s\n", sqp->name);
10133206Sbostic switch (sqp->type) {
10233206Sbostic case SAFE:
10333206Sbostic printf("That is a safe place\n");
10433206Sbostic break;
10533206Sbostic case CC:
10633216Sbostic cc(); break;
10733206Sbostic case CHANCE:
10833216Sbostic chance(); break;
10933216Sbostic case INC_TAX:
11033216Sbostic inc_tax(); break;
11133216Sbostic case GOTO_J:
11233216Sbostic goto_jail(); break;
11333216Sbostic case LUX_TAX:
11433216Sbostic lux_tax(); break;
11533206Sbostic case PRPTY:
11633206Sbostic case RR:
11733206Sbostic case UTIL:
11833206Sbostic if (sqp->owner < 0) {
11933206Sbostic printf("That would cost $%d\n", sqp->cost);
12033206Sbostic if (getyn("Do you want to buy? ") == 0) {
12133206Sbostic buy(player, sqp);
12233206Sbostic cur_p->money -= sqp->cost;
12333206Sbostic }
12433206Sbostic else if (num_play > 2)
12533206Sbostic bid(sqp);
12633206Sbostic }
12733206Sbostic else if (sqp->owner == player)
12833206Sbostic printf("You own it.\n");
12933206Sbostic else
13033206Sbostic rent(sqp);
13133206Sbostic }
13233206Sbostic }
13333206Sbostic /*
13433206Sbostic * This routine saves the current game for use at a later date
13533206Sbostic */
save()13633206Sbostic save() {
13733206Sbostic
13833206Sbostic reg char *sp;
13933206Sbostic reg int outf, num;
14046751Sbostic time_t t;
14146751Sbostic int *dat_end;
14246751Sbostic struct stat sb;
14333206Sbostic unsgn start, end;
14433206Sbostic
14533206Sbostic printf("Which file do you wish to save it in? ");
14633206Sbostic sp = buf;
14733206Sbostic while ((*sp++=getchar()) != '\n')
14833206Sbostic continue;
14933206Sbostic *--sp = '\0';
15033206Sbostic
15133206Sbostic /*
15233206Sbostic * check for existing files, and confirm overwrite if needed
15333206Sbostic */
15433206Sbostic
15546751Sbostic if (stat(buf, &sb) > -1
15633206Sbostic && getyn("File exists. Do you wish to overwrite? ", yn_only) > 0)
15733206Sbostic return;
15833206Sbostic
15933206Sbostic if ((outf=creat(buf, 0644)) < 0) {
16033206Sbostic perror(buf);
16133206Sbostic return;
16233206Sbostic }
16333206Sbostic printf("\"%s\" ", buf);
16446751Sbostic time(&t); /* get current time */
16546751Sbostic strcpy(buf, ctime(&t));
16633206Sbostic for (sp = buf; *sp != '\n'; sp++)
16733206Sbostic continue;
16833206Sbostic *sp = '\0';
16933206Sbostic # if 0
17033206Sbostic start = (((int) etext + (SEGSIZE-1)) / SEGSIZE ) * SEGSIZE;
17133206Sbostic # else
17233206Sbostic start = 0;
17333206Sbostic # endif
17433206Sbostic end = sbrk(0);
17533206Sbostic while (start < end) { /* write out entire data space */
17633206Sbostic num = start + 16 * 1024 > end ? end - start : 16 * 1024;
17733206Sbostic write(outf, start, num);
17833206Sbostic start += num;
17933206Sbostic }
18033206Sbostic close(outf);
18133206Sbostic printf("[%s]\n", buf);
18233206Sbostic }
18333206Sbostic /*
18433206Sbostic * This routine restores an old game from a file
18533206Sbostic */
restore()18633206Sbostic restore() {
18733206Sbostic
18833206Sbostic reg char *sp;
18933206Sbostic
19033206Sbostic printf("Which file do you wish to restore from? ");
19133206Sbostic for (sp = buf; (*sp=getchar()) != '\n'; sp++)
19233206Sbostic continue;
19333206Sbostic *sp = '\0';
19433206Sbostic rest_f(buf);
19533206Sbostic }
19633206Sbostic /*
19733206Sbostic * This does the actual restoring. It returns TRUE if the
19833206Sbostic * backup was successful, else false.
19933206Sbostic */
rest_f(file)20033206Sbostic rest_f(file)
20133206Sbostic reg char *file; {
20233206Sbostic
20333206Sbostic reg char *sp;
20433206Sbostic reg int inf, num;
20533206Sbostic char buf[80];
20633206Sbostic unsgn start, end;
20733206Sbostic STAT sbuf;
20833206Sbostic
20933206Sbostic if ((inf=open(file, 0)) < 0) {
21033206Sbostic perror(file);
21133206Sbostic return FALSE;
21233206Sbostic }
21333206Sbostic printf("\"%s\" ", file);
21433206Sbostic if (fstat(inf, &sbuf) < 0) { /* get file stats */
21533206Sbostic perror(file);
21633206Sbostic exit(1);
21733206Sbostic }
21833206Sbostic # if 0
21933206Sbostic start = (((int) etext + (SEGSIZE-1)) / SEGSIZE ) * SEGSIZE;
22033206Sbostic # else
22133206Sbostic start = 0;
22233206Sbostic # endif
22333206Sbostic brk(end = start + sbuf.st_size);
22433206Sbostic while (start < end) { /* write out entire data space */
22533206Sbostic num = start + 16 * 1024 > end ? end - start : 16 * 1024;
22633206Sbostic read(inf, start, num);
22733206Sbostic start += num;
22833206Sbostic }
22933206Sbostic close(inf);
23046751Sbostic strcpy(buf, ctime(&sbuf.st_mtime));
23133206Sbostic for (sp = buf; *sp != '\n'; sp++)
23233206Sbostic continue;
23333206Sbostic *sp = '\0';
23433206Sbostic printf("[%s]\n", buf);
23533206Sbostic return TRUE;
23633206Sbostic }
237