BSD 4_1_snap development
[unix-history] / usr / src / games / monop / execute.c
CommitLineData
269111fa
C
1# include "monop.ext"
2# include <sys/types.h>
3# include <sys/stat.h>
4# include <time.h>
5
6# define SEGSIZE 8192
7
8typedef struct stat STAT;
9typedef struct tm TIME;
10
11extern char etext[], /* end of text space */
12 rub();
13
14static char buf[257],
15 *yn_only[] = { "yes", "no"};
16
17static bool new_play; /* set if move on to new player */
18
19/*
20 * This routine executes the given command by index number
21 */
22execute(com_num)
23reg int com_num; {
24
25 new_play = FALSE; /* new_play is true if fixing */
26 (*func[com_num])();
27 notify();
28 force_morg();
29 if (new_play)
30 next_play();
31 else if (num_doub)
32 printf("%s rolled doubles. Goes again\n", cur_p->name);
33}
34/*
35 * This routine moves a piece around.
36 */
37do_move() {
38
39 reg int r1, r2;
40 reg bool was_jail;
41
42 new_play = was_jail = FALSE;
43 printf("roll is %d, %d\n", r1=roll(1, 6), r2=roll(1, 6));
44 if (cur_p->loc == JAIL) {
45 was_jail++;
46 if (!move_jail(r1, r2)) {
47 new_play++;
48 goto ret;
49 }
50 }
51 else {
52 if (r1 == r2 && ++num_doub == 3) {
53 printf("That's 3 doubles. You go to jail\n");
54 goto_jail();
55 new_play++;
56 goto ret;
57 }
58 move(r1+r2);
59 }
60 if (r1 != r2 || was_jail)
61 new_play++;
62ret:
63 return;
64}
65/*
66 * This routine moves a normal move
67 */
68move(rl)
69reg int rl; {
70
71 reg int old_loc;
72
73 old_loc = cur_p->loc;
74 cur_p->loc = (cur_p->loc + rl) % N_SQRS;
75 if (cur_p->loc < old_loc && rl > 0) {
76 cur_p->money += 200;
77 printf("You pass %s and get $200\n", board[0].name);
78 }
79 show_move();
80}
81/*
82 * This routine shows the results of a move
83 */
84show_move() {
85
86 reg SQUARE *sqp;
87
88 sqp = &board[cur_p->loc];
89 printf("That puts you on %s\n", sqp->name);
90 switch (sqp->type) {
91 case SAFE:
92 printf("That is a safe place\n");
93 break;
94 case CC:
95 case CHANCE:
96 case SPEC:
97 (*((int (*)())(sqp->desc)))();
98 break;
99 case PRPTY:
100 case RR:
101 case UTIL:
102 if (sqp->owner < 0) {
103 printf("That would cost $%d\n", sqp->cost);
104 if (getyn("Do you want to buy? ") == 0) {
105 buy(player, sqp);
106 cur_p->money -= sqp->cost;
107 }
108 else if (num_play > 2)
109 bid(sqp);
110 }
111 else if (sqp->owner == player)
112 printf("You own it.\n");
113 else
114 rent(sqp);
115 }
116}
117/*
118 * This routine saves the current game for use at a later date
119 */
120save() {
121
122 reg char *sp;
123 reg int outf, num;
124 TIME tme, *tp;
125 int *dat_end, junk[18];
126 unsgn start, end;
127
128 tp = &tme;
129 printf("Which file do you wish to save it in? ");
130 sp = buf;
131 while ((*sp++=getchar()) != '\n')
132 continue;
133 *--sp = '\0';
134
135 /*
136 * check for existing files, and confirm overwrite if needed
137 */
138
139 if (stat(buf, junk) > -1
140 && getyn("File exists. Do you wish to overwrite? ", yn_only) > 0)
141 return;
142
143 if ((outf=creat(buf, 0644)) < 0) {
144 perror(buf);
145 return;
146 }
147 printf("\"%s\" ", buf);
148 time(tp); /* get current time */
149 strcpy(buf, ctime(tp));
150 for (sp = buf; *sp != '\n'; sp++)
151 continue;
152 *sp = '\0';
153# if 0
154 start = (((int) etext + (SEGSIZE-1)) / SEGSIZE ) * SEGSIZE;
155# else
156 start = 0;
157# endif
158 end = sbrk(0);
159 while (start < end) { /* write out entire data space */
160 num = start + 16 * 1024 > end ? end - start : 16 * 1024;
161 write(outf, start, num);
162 start += num;
163 }
164 close(outf);
165 printf("[%s]\n", buf);
166}
167/*
168 * This routine restores an old game from a file
169 */
170restore() {
171
172 reg char *sp;
173
174 printf("Which file do you wish to restore from? ");
175 for (sp = buf; (*sp=getchar()) != '\n'; sp++)
176 continue;
177 *sp = '\0';
178 rest_f(buf);
179}
180/*
181 * This does the actual restoring. It returns TRUE if the
182 * backup was successful, else false.
183 */
184rest_f(file)
185reg char *file; {
186
187 reg char *sp;
188 reg int inf, num;
189 char buf[80];
190 unsgn start, end;
191 STAT sbuf;
192
193 if ((inf=open(file, 0)) < 0) {
194 perror(file);
195 return FALSE;
196 }
197 printf("\"%s\" ", file);
198 if (fstat(inf, &sbuf) < 0) { /* get file stats */
199 perror(file);
200 exit(1);
201 }
202# if 0
203 start = (((int) etext + (SEGSIZE-1)) / SEGSIZE ) * SEGSIZE;
204# else
205 start = 0;
206# endif
207 brk(end = start + sbuf.st_size);
208 while (start < end) { /* write out entire data space */
209 num = start + 16 * 1024 > end ? end - start : 16 * 1024;
210 read(inf, start, num);
211 start += num;
212 }
213 close(inf);
214 strcpy(buf, ctime(sbuf.st_mtime));
215 for (sp = buf; *sp != '\n'; sp++)
216 continue;
217 *sp = '\0';
218 printf("[%s]\n", buf);
219 return TRUE;
220}