1 /* $NetBSD: move.c,v 1.16 2009/05/25 23:24:54 dholland 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.16 2009/05/25 23:24:54 dholland 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");
235 case C_100
: case C_75
:
236 if (pp
->speed
== C_LIMIT
)
237 return error("limit of 50");
239 if (pp
->mileage
+ Value
[card
] > End
)
240 return error("puts you over %d", End
);
243 return error("cannot move now");
244 pp
->nummiles
[card
]++;
248 if ((pp
->mileage
+= v
) == End
)
252 case C_GAS
: case C_SPARE
: case C_REPAIRS
:
253 if (pp
->battle
!= opposite(card
))
254 return error("can't play \"%s\"", C_name
[card
]);
256 if (pp
->safety
[S_RIGHT_WAY
] == S_PLAYED
)
261 if (pp
->battle
!= C_INIT
&& pp
->battle
!= C_STOP
262 && !is_repair(pp
->battle
))
263 return error("cannot play \"Go\" on a \"%s\"",
270 if (pp
->speed
!= C_LIMIT
)
271 return error("not limited");
272 pp
->speed
= C_END_LIMIT
;
275 case C_EMPTY
: case C_FLAT
: case C_CRASH
:
277 pp
= &Player
[other(Play
)];
279 return error("opponent cannot go");
280 else if (pp
->safety
[safety(card
) - S_CONV
] == S_PLAYED
)
282 return error("opponent is protected");
284 pp
->new_battle
= TRUE
;
290 pp
= &Player
[other(Play
)];
291 if (pp
->speed
== C_LIMIT
)
292 return error("opponent has limit");
293 if (pp
->safety
[S_RIGHT_WAY
] == S_PLAYED
)
296 pp
->new_speed
= TRUE
;
300 case C_GAS_SAFE
: case C_SPARE_SAFE
:
301 case C_DRIVE_SAFE
: case C_RIGHT_WAY
:
302 if (pp
->battle
== opposite(card
)
303 || (card
== C_RIGHT_WAY
&& pp
->speed
== C_LIMIT
)) {
304 if (!(card
== C_RIGHT_WAY
&& !is_repair(pp
->battle
))) {
308 if (card
== C_RIGHT_WAY
&& pp
->speed
== C_LIMIT
)
311 || (pp
->new_speed
&& card
== C_RIGHT_WAY
)) {
312 pp
->coups
[card
- S_CONV
] = TRUE
;
313 pp
->total
+= SC_COUP
;
314 pp
->hand_tot
+= SC_COUP
;
315 pp
->coupscore
+= SC_COUP
;
319 * if not coup, must pick first
321 else if (pp
->hand
[0] == C_INIT
&& Topcard
> Deck
)
323 pp
->safety
[card
- S_CONV
] = S_PLAYED
;
324 pp
->total
+= SC_SAFETY
;
325 pp
->hand_tot
+= SC_SAFETY
;
326 if ((pp
->safescore
+= SC_SAFETY
) == NUM_SAFE
* SC_SAFETY
) {
327 pp
->total
+= SC_ALL_SAFE
;
328 pp
->hand_tot
+= SC_ALL_SAFE
;
330 if (card
== C_RIGHT_WAY
) {
331 if (pp
->speed
== C_LIMIT
)
333 if (pp
->battle
== C_STOP
|| pp
->battle
== C_INIT
) {
337 if (!pp
->can_go
&& is_repair(pp
->battle
))
344 error("no card there");
348 if (pp
== &Player
[PLAYER
])
350 pp
->hand
[Card_no
] = C_INIT
;
351 Next
= (Next
== (bool)-1 ? FALSE
: TRUE
);
360 static bool last_ex
= FALSE
; /* set if last command was E */
370 leaveok(Board
, FALSE
);
372 while ((c
= readch()) == killchar() || c
== erasechar())
374 if (islower((unsigned char)c
))
375 c
= toupper((unsigned char)c
);
376 if (isprint((unsigned char)c
) && !isspace((unsigned char)c
)) {
384 case 'U': /* Use Card */
385 case 'D': /* Discard Card */
386 if ((Card_no
= getcard()) < 0)
388 Movetype
= (c
== 'U' ? M_PLAY
: M_DISCARD
);
390 case 'O': /* Order */
392 if (Window
== W_SMALL
) {
394 mvwaddstr(Score
, 12, 21,
397 mvwaddstr(Score
, 12, 21,
404 rub(0); /* Same as a rubout */
406 case 'W': /* Window toggle */
407 Window
= nextwin(Window
);
412 case 'R': /* Redraw screen */
416 case 'S': /* Save game */
420 case 'E': /* Extrapolate */
425 if (Window
!= W_FULL
)
432 error("%c: command not implemented", c
);
435 case '\r': /* Ignore RETURNs and */
436 case '\n': /* Line Feeds */
437 case ' ': /* Spaces */
438 case '\0': /* and nulls */
441 case 'Z': /* Debug code */
442 if (!Debug
&& outf
== NULL
) {
443 char buf
[MAXPATHLEN
];
447 leaveok(Board
, FALSE
);
451 while ((*sp
= readch()) != '\n') {
452 if (*sp
== killchar())
454 else if (*sp
== erasechar()) {
465 addstr(unctrl(*sp
++));
469 leaveok(Board
, TRUE
);
470 if ((outf
= fopen(buf
, "w")) == NULL
)
472 setbuf(outf
, (char *)NULL
);
478 error("unknown command: %s", unctrl(c
));
483 leaveok(Board
, TRUE
);
487 * return whether or not the player has picked
490 haspicked(const PLAY
*pp
)
496 switch (pp
->hand
[Card_no
]) {
497 case C_GAS_SAFE
: case C_SPARE_SAFE
:
498 case C_DRIVE_SAFE
: case C_RIGHT_WAY
:
505 return (pp
->hand
[card
] != C_INIT
);
521 oppos
= opposite(card
);
522 Numgos
+= Numcards
[oppos
] - Numseen
[oppos
];
536 static const char *const names
[] = {
547 static int last_prompt
= -1;
549 if (promptno
== last_prompt
)
550 move(MOVE_Y
, MOVE_X
+ strlen(names
[promptno
]) + 1);
552 move(MOVE_Y
, MOVE_X
);
553 if (promptno
== MOVEPROMPT
)
555 addstr(names
[promptno
]);
556 if (promptno
== MOVEPROMPT
)
559 last_prompt
= promptno
;
572 for ( ; cp
< &hand
[-1]; cp
++)
573 for (tp
= cp
+ 1; tp
< hand
; tp
++)