]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - mille/move.c
1 /* $NetBSD: move.c,v 1.19 2019/02/03 03:19:25 mrg Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)move.c 8.1 (Berkeley) 5/31/93";
37 __RCSID("$NetBSD: move.c,v 1.19 2019/02/03 03:19:25 mrg Exp $");
44 #include <sys/param.h>
53 * @(#)move.c 1.2 (Berkeley) 3/28/83
57 #define CTRL(c) (c - 'A' + 1)
59 static void check_go(void);
60 static int playcard(PLAY
*);
61 static void getmove(void);
62 static int haspicked(const PLAY
*);
72 for (i
= 0, j
= 0; i
< HAND_SZ
; i
++)
73 if (pp
->hand
[i
] != -1)
88 if (pp
->hand
[Card_no
] == C_INIT
)
92 error("no card there");
94 if (is_safety(pp
->hand
[Card_no
])) {
95 error("discard a safety?");
99 Discard
= pp
->hand
[Card_no
];
100 pp
->hand
[Card_no
] = C_INIT
;
107 error("must pick first");
110 goodplay
= playcard(pp
);
115 error("no more cards");
116 else if (haspicked(pp
))
117 error("already picked");
119 pp
->hand
[0] = *--Topcard
;
122 fprintf(outf
, "DOMOVE: Draw %s\n", C_name
[*Topcard
]);
127 if (is_safety(*Topcard
))
128 pp
->safety
[*Topcard
-S_CONV
] = S_IN_HAND
;
130 if (pp
->hand
[1] == C_INIT
&& Topcard
> Deck
) {
132 pp
->hand
[1] = *--Topcard
;
135 fprintf(outf
, "DOMOVE: Draw %s\n", C_name
[*Topcard
]);
139 pp
->new_battle
= FALSE
;
140 pp
->new_speed
= FALSE
;
148 * move blank card to top by one of two methods. If the
149 * computer's hand was sorted, the randomness for picking
150 * between equally valued cards would be lost
152 if (Order
&& Movetype
!= M_DRAW
&& goodplay
&& pp
== &Player
[PLAYER
])
155 for (i
= 1; i
< HAND_SZ
; i
++)
156 if (pp
->hand
[i
] == C_INIT
) {
157 for (j
= 0; pp
->hand
[j
] == C_INIT
; j
++)
162 pp
->hand
[i
] = pp
->hand
[j
];
163 pp
->hand
[j
] = C_INIT
;
172 * Check and see if either side can go. If they cannot,
182 for (pp
= Player
; pp
< &Player
[2]; pp
++) {
183 op
= (pp
== &Player
[COMP
] ? &Player
[PLAYER
] : &Player
[COMP
]);
184 for (i
= 0; i
< HAND_SZ
; i
++) {
186 if (is_safety(card
) || canplay(pp
, op
, card
)) {
189 fprintf(outf
, "CHECK_GO: can play %s (%d), ", C_name
[card
], card
);
190 fprintf(outf
, "is_safety(card) = %d, ", is_safety(card
));
191 fprintf(outf
, "canplay(pp, op, card) = %d\n", canplay(pp
, op
, card
));
198 fprintf(outf
, "CHECK_GO: cannot play %s\n",
213 * check and see if player has picked
215 switch (pp
->hand
[Card_no
]) {
219 return error("must pick first");
220 case C_GAS_SAFE
: case C_SPARE_SAFE
:
221 case C_DRIVE_SAFE
: case C_RIGHT_WAY
:
225 card
= pp
->hand
[Card_no
];
228 fprintf(outf
, "PLAYCARD: Card = %s\n", C_name
[card
]);
233 if (pp
->nummiles
[C_200
] == 2)
234 return error("only two 200's per hand");
236 case C_100
: case C_75
:
237 if (pp
->speed
== C_LIMIT
)
238 return error("limit of 50");
241 if (pp
->mileage
+ Value
[card
] > End
)
242 return error("puts you over %d", End
);
246 return error("cannot move now");
247 pp
->nummiles
[card
]++;
251 if ((pp
->mileage
+= v
) == End
)
255 case C_GAS
: case C_SPARE
: case C_REPAIRS
:
256 if (pp
->battle
!= opposite(card
))
257 return error("can't play \"%s\"", C_name
[card
]);
259 if (pp
->safety
[S_RIGHT_WAY
] == S_PLAYED
)
264 if (pp
->battle
!= C_INIT
&& pp
->battle
!= C_STOP
265 && !is_repair(pp
->battle
))
266 return error("cannot play \"Go\" on a \"%s\"",
273 if (pp
->speed
!= C_LIMIT
)
274 return error("not limited");
275 pp
->speed
= C_END_LIMIT
;
278 case C_EMPTY
: case C_FLAT
: case C_CRASH
:
280 pp
= &Player
[other(Play
)];
282 return error("opponent cannot go");
283 else if (pp
->safety
[safety(card
) - S_CONV
] == S_PLAYED
)
285 return error("opponent is protected");
287 pp
->new_battle
= TRUE
;
293 pp
= &Player
[other(Play
)];
294 if (pp
->speed
== C_LIMIT
)
295 return error("opponent has limit");
296 if (pp
->safety
[S_RIGHT_WAY
] == S_PLAYED
)
299 pp
->new_speed
= TRUE
;
303 case C_GAS_SAFE
: case C_SPARE_SAFE
:
304 case C_DRIVE_SAFE
: case C_RIGHT_WAY
:
305 if (pp
->battle
== opposite(card
)
306 || (card
== C_RIGHT_WAY
&& pp
->speed
== C_LIMIT
)) {
307 if (!(card
== C_RIGHT_WAY
&& !is_repair(pp
->battle
))) {
311 if (card
== C_RIGHT_WAY
&& pp
->speed
== C_LIMIT
)
314 || (pp
->new_speed
&& card
== C_RIGHT_WAY
)) {
315 pp
->coups
[card
- S_CONV
] = TRUE
;
316 pp
->total
+= SC_COUP
;
317 pp
->hand_tot
+= SC_COUP
;
318 pp
->coupscore
+= SC_COUP
;
322 * if not coup, must pick first
324 else if (pp
->hand
[0] == C_INIT
&& Topcard
> Deck
)
326 pp
->safety
[card
- S_CONV
] = S_PLAYED
;
327 pp
->total
+= SC_SAFETY
;
328 pp
->hand_tot
+= SC_SAFETY
;
329 if ((pp
->safescore
+= SC_SAFETY
) == NUM_SAFE
* SC_SAFETY
) {
330 pp
->total
+= SC_ALL_SAFE
;
331 pp
->hand_tot
+= SC_ALL_SAFE
;
333 if (card
== C_RIGHT_WAY
) {
334 if (pp
->speed
== C_LIMIT
)
336 if (pp
->battle
== C_STOP
|| pp
->battle
== C_INIT
) {
340 if (!pp
->can_go
&& is_repair(pp
->battle
))
347 error("no card there");
351 if (pp
== &Player
[PLAYER
])
353 pp
->hand
[Card_no
] = C_INIT
;
354 Next
= (Next
== (bool)-1 ? FALSE
: TRUE
);
363 static bool last_ex
= FALSE
; /* set if last command was E */
373 leaveok(Board
, FALSE
);
375 while ((c
= readch()) == killchar() || c
== erasechar())
377 if (islower((unsigned char)c
))
378 c
= toupper((unsigned char)c
);
379 if (isprint((unsigned char)c
) && !isspace((unsigned char)c
)) {
387 case 'U': /* Use Card */
388 case 'D': /* Discard Card */
389 if ((Card_no
= getcard()) < 0)
391 Movetype
= (c
== 'U' ? M_PLAY
: M_DISCARD
);
393 case 'O': /* Order */
395 if (Window
== W_SMALL
) {
397 mvwaddstr(Score
, 12, 21,
400 mvwaddstr(Score
, 12, 21,
407 rub(0); /* Same as a rubout */
409 case 'W': /* Window toggle */
410 Window
= nextwin(Window
);
415 case 'R': /* Redraw screen */
419 case 'S': /* Save game */
423 case 'E': /* Extrapolate */
428 if (Window
!= W_FULL
)
435 error("%c: command not implemented", c
);
438 case '\r': /* Ignore RETURNs and */
439 case '\n': /* Line Feeds */
440 case ' ': /* Spaces */
441 case '\0': /* and nulls */
444 case 'Z': /* Debug code */
445 if (!Debug
&& outf
== NULL
) {
446 char buf
[MAXPATHLEN
];
450 leaveok(Board
, FALSE
);
454 while ((*sp
= readch()) != '\n') {
455 if (*sp
== killchar())
457 else if (*sp
== erasechar()) {
468 addstr(unctrl(*sp
++));
472 leaveok(Board
, TRUE
);
473 if ((outf
= fopen(buf
, "w")) == NULL
)
481 error("unknown command: %s", unctrl(c
));
486 leaveok(Board
, TRUE
);
490 * return whether or not the player has picked
493 haspicked(const PLAY
*pp
)
499 switch (pp
->hand
[Card_no
]) {
500 case C_GAS_SAFE
: case C_SPARE_SAFE
:
501 case C_DRIVE_SAFE
: case C_RIGHT_WAY
:
508 return (pp
->hand
[card
] != C_INIT
);
524 oppos
= opposite(card
);
525 Numgos
+= Numcards
[oppos
] - Numseen
[oppos
];
539 static const char *const names
[] = {
550 static int last_prompt
= -1;
552 if (promptno
== last_prompt
)
553 move(MOVE_Y
, MOVE_X
+ strlen(names
[promptno
]) + 1);
555 move(MOVE_Y
, MOVE_X
);
556 if (promptno
== MOVEPROMPT
)
558 addstr(names
[promptno
]);
559 if (promptno
== MOVEPROMPT
)
562 last_prompt
= promptno
;
575 for ( ; cp
< &hand
[-1]; cp
++)
576 for (tp
= cp
+ 1; tp
< hand
; tp
++)