xref: /csrg-svn/games/monop/execute.c (revision 34788)
133206Sbostic /*
2*34788Sbostic  * Copyright (c) 1980 Regents of the University of California.
333206Sbostic  * All rights reserved.
433206Sbostic  *
533206Sbostic  * Redistribution and use in source and binary forms are permitted
6*34788Sbostic  * provided that the above copyright notice and this paragraph are
7*34788Sbostic  * duplicated in all such forms and that any documentation,
8*34788Sbostic  * advertising materials, and other materials related to such
9*34788Sbostic  * distribution and use acknowledge that the software was developed
10*34788Sbostic  * by the University of California, Berkeley.  The name of the
11*34788Sbostic  * University may not be used to endorse or promote products derived
12*34788Sbostic  * from this software without specific prior written permission.
13*34788Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34788Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34788Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1633206Sbostic  */
1733206Sbostic 
1833206Sbostic #ifndef lint
19*34788Sbostic static char sccsid[] = "@(#)execute.c	5.3 (Berkeley) 06/18/88";
2033206Sbostic #endif /* not lint */
2133206Sbostic 
2233206Sbostic # include	"monop.ext"
2333206Sbostic # include	<sys/types.h>
2433206Sbostic # include	<sys/stat.h>
2533206Sbostic # include	<sys/time.h>
2633206Sbostic 
2733206Sbostic # define	SEGSIZE	8192
2833206Sbostic 
2933206Sbostic typedef	struct stat	STAT;
3033206Sbostic typedef	struct tm	TIME;
3133206Sbostic 
3233206Sbostic extern char	etext[],	/* end of text space			*/
3333206Sbostic 		rub();
3433206Sbostic 
3533206Sbostic static char	buf[257],
3633206Sbostic 		*yn_only[]	= { "yes", "no"};
3733206Sbostic 
3833206Sbostic static bool	new_play;	/* set if move on to new player		*/
3933206Sbostic 
4033206Sbostic /*
4133206Sbostic  *	This routine executes the given command by index number
4233206Sbostic  */
4333206Sbostic execute(com_num)
4433206Sbostic reg int	com_num; {
4533206Sbostic 
4633206Sbostic 	new_play = FALSE;	/* new_play is true if fixing	*/
4733206Sbostic 	(*func[com_num])();
4833206Sbostic 	notify();
4933206Sbostic 	force_morg();
5033206Sbostic 	if (new_play)
5133206Sbostic 		next_play();
5233206Sbostic 	else if (num_doub)
5333206Sbostic 		printf("%s rolled doubles.  Goes again\n", cur_p->name);
5433206Sbostic }
5533206Sbostic /*
5633206Sbostic  *	This routine moves a piece around.
5733206Sbostic  */
5833206Sbostic do_move() {
5933206Sbostic 
6033206Sbostic 	reg int		r1, r2;
6133206Sbostic 	reg bool	was_jail;
6233206Sbostic 
6333206Sbostic 	new_play = was_jail = FALSE;
6433206Sbostic 	printf("roll is %d, %d\n", r1=roll(1, 6), r2=roll(1, 6));
6533206Sbostic 	if (cur_p->loc == JAIL) {
6633206Sbostic 		was_jail++;
6733206Sbostic 		if (!move_jail(r1, r2)) {
6833206Sbostic 			new_play++;
6933206Sbostic 			goto ret;
7033206Sbostic 		}
7133206Sbostic 	}
7233206Sbostic 	else {
7333206Sbostic 		if (r1 == r2 && ++num_doub == 3) {
7433206Sbostic 			printf("That's 3 doubles.  You go to jail\n");
7533206Sbostic 			goto_jail();
7633206Sbostic 			new_play++;
7733206Sbostic 			goto ret;
7833206Sbostic 		}
7933206Sbostic 		move(r1+r2);
8033206Sbostic 	}
8133206Sbostic 	if (r1 != r2 || was_jail)
8233206Sbostic 		new_play++;
8333206Sbostic ret:
8433206Sbostic 	return;
8533206Sbostic }
8633206Sbostic /*
8733206Sbostic  *	This routine moves a normal move
8833206Sbostic  */
8933206Sbostic move(rl)
9033206Sbostic reg int	rl; {
9133206Sbostic 
9233206Sbostic 	reg int	old_loc;
9333206Sbostic 
9433206Sbostic 	old_loc = cur_p->loc;
9533206Sbostic 	cur_p->loc = (cur_p->loc + rl) % N_SQRS;
9633206Sbostic 	if (cur_p->loc < old_loc && rl > 0) {
9733206Sbostic 		cur_p->money += 200;
9833206Sbostic 		printf("You pass %s and get $200\n", board[0].name);
9933206Sbostic 	}
10033206Sbostic 	show_move();
10133206Sbostic }
10233206Sbostic /*
10333206Sbostic  *	This routine shows the results of a move
10433206Sbostic  */
10533206Sbostic show_move() {
10633206Sbostic 
10733206Sbostic 	reg SQUARE	*sqp;
10833206Sbostic 
10933206Sbostic 	sqp = &board[cur_p->loc];
11033206Sbostic 	printf("That puts you on %s\n", sqp->name);
11133206Sbostic 	switch (sqp->type) {
11233206Sbostic 	  case SAFE:
11333206Sbostic 		printf("That is a safe place\n");
11433206Sbostic 		break;
11533206Sbostic 	  case CC:
11633216Sbostic 		cc(); break;
11733206Sbostic 	  case CHANCE:
11833216Sbostic 		chance(); break;
11933216Sbostic 	  case INC_TAX:
12033216Sbostic 		inc_tax(); break;
12133216Sbostic 	  case GOTO_J:
12233216Sbostic 		goto_jail(); break;
12333216Sbostic 	  case LUX_TAX:
12433216Sbostic 		lux_tax(); break;
12533206Sbostic 	  case PRPTY:
12633206Sbostic 	  case RR:
12733206Sbostic 	  case UTIL:
12833206Sbostic 		if (sqp->owner < 0) {
12933206Sbostic 			printf("That would cost $%d\n", sqp->cost);
13033206Sbostic 			if (getyn("Do you want to buy? ") == 0) {
13133206Sbostic 				buy(player, sqp);
13233206Sbostic 				cur_p->money -= sqp->cost;
13333206Sbostic 			}
13433206Sbostic 			else if (num_play > 2)
13533206Sbostic 				bid(sqp);
13633206Sbostic 		}
13733206Sbostic 		else if (sqp->owner == player)
13833206Sbostic 			printf("You own it.\n");
13933206Sbostic 		else
14033206Sbostic 			rent(sqp);
14133206Sbostic 	}
14233206Sbostic }
14333206Sbostic /*
14433206Sbostic  *	This routine saves the current game for use at a later date
14533206Sbostic  */
14633206Sbostic save() {
14733206Sbostic 
14833206Sbostic 	reg char	*sp;
14933206Sbostic 	reg int		outf, num;
15033206Sbostic 	TIME		tme, *tp;
15133206Sbostic 	int		*dat_end, junk[18];
15233206Sbostic 	unsgn		start, end;
15333206Sbostic 
15433206Sbostic 	tp = &tme;
15533206Sbostic 	printf("Which file do you wish to save it in? ");
15633206Sbostic 	sp = buf;
15733206Sbostic 	while ((*sp++=getchar()) != '\n')
15833206Sbostic 		continue;
15933206Sbostic 	*--sp = '\0';
16033206Sbostic 
16133206Sbostic 	/*
16233206Sbostic 	 * check for existing files, and confirm overwrite if needed
16333206Sbostic 	 */
16433206Sbostic 
16533206Sbostic 	if (stat(buf, junk) > -1
16633206Sbostic 	    && getyn("File exists.  Do you wish to overwrite? ", yn_only) > 0)
16733206Sbostic 		return;
16833206Sbostic 
16933206Sbostic 	if ((outf=creat(buf, 0644)) < 0) {
17033206Sbostic 		perror(buf);
17133206Sbostic 		return;
17233206Sbostic 	}
17333206Sbostic 	printf("\"%s\" ", buf);
17433206Sbostic 	time(tp);			/* get current time		*/
17533206Sbostic 	strcpy(buf, ctime(tp));
17633206Sbostic 	for (sp = buf; *sp != '\n'; sp++)
17733206Sbostic 		continue;
17833206Sbostic 	*sp = '\0';
17933206Sbostic # if 0
18033206Sbostic 	start = (((int) etext + (SEGSIZE-1)) / SEGSIZE ) * SEGSIZE;
18133206Sbostic # else
18233206Sbostic 	start = 0;
18333206Sbostic # endif
18433206Sbostic 	end = sbrk(0);
18533206Sbostic 	while (start < end) {		/* write out entire data space */
18633206Sbostic 		num = start + 16 * 1024 > end ? end - start : 16 * 1024;
18733206Sbostic 		write(outf, start, num);
18833206Sbostic 		start += num;
18933206Sbostic 	}
19033206Sbostic 	close(outf);
19133206Sbostic 	printf("[%s]\n", buf);
19233206Sbostic }
19333206Sbostic /*
19433206Sbostic  *	This routine restores an old game from a file
19533206Sbostic  */
19633206Sbostic restore() {
19733206Sbostic 
19833206Sbostic 	reg char	*sp;
19933206Sbostic 
20033206Sbostic 	printf("Which file do you wish to restore from? ");
20133206Sbostic 	for (sp = buf; (*sp=getchar()) != '\n'; sp++)
20233206Sbostic 		continue;
20333206Sbostic 	*sp = '\0';
20433206Sbostic 	rest_f(buf);
20533206Sbostic }
20633206Sbostic /*
20733206Sbostic  *	This does the actual restoring.  It returns TRUE if the
20833206Sbostic  * backup was successful, else false.
20933206Sbostic  */
21033206Sbostic rest_f(file)
21133206Sbostic reg char	*file; {
21233206Sbostic 
21333206Sbostic 	reg char	*sp;
21433206Sbostic 	reg int		inf, num;
21533206Sbostic 	char		buf[80];
21633206Sbostic 	unsgn		start, end;
21733206Sbostic 	STAT		sbuf;
21833206Sbostic 
21933206Sbostic 	if ((inf=open(file, 0)) < 0) {
22033206Sbostic 		perror(file);
22133206Sbostic 		return FALSE;
22233206Sbostic 	}
22333206Sbostic 	printf("\"%s\" ", file);
22433206Sbostic 	if (fstat(inf, &sbuf) < 0) {		/* get file stats	*/
22533206Sbostic 		perror(file);
22633206Sbostic 		exit(1);
22733206Sbostic 	}
22833206Sbostic # if 0
22933206Sbostic 	start = (((int) etext + (SEGSIZE-1)) / SEGSIZE ) * SEGSIZE;
23033206Sbostic # else
23133206Sbostic 	start = 0;
23233206Sbostic # endif
23333206Sbostic 	brk(end = start + sbuf.st_size);
23433206Sbostic 	while (start < end) {		/* write out entire data space */
23533206Sbostic 		num = start + 16 * 1024 > end ? end - start : 16 * 1024;
23633206Sbostic 		read(inf, start, num);
23733206Sbostic 		start += num;
23833206Sbostic 	}
23933206Sbostic 	close(inf);
24033206Sbostic 	strcpy(buf, ctime(sbuf.st_mtime));
24133206Sbostic 	for (sp = buf; *sp != '\n'; sp++)
24233206Sbostic 		continue;
24333206Sbostic 	*sp = '\0';
24433206Sbostic 	printf("[%s]\n", buf);
24533206Sbostic 	return TRUE;
24633206Sbostic }
247