Home | History | Annotate | Line # | Download | only in mille
move.c revision 1.8
      1 /*	$NetBSD: move.c,v 1.8 1999/03/29 05:12:39 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)move.c	8.1 (Berkeley) 5/31/93";
     40 #else
     41 __RCSID("$NetBSD: move.c,v 1.8 1999/03/29 05:12:39 mrg Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <termios.h>
     46 
     47 #include	"mille.h"
     48 #ifndef	unctrl
     49 #include	"unctrl.h"
     50 #endif
     51 
     52 /*
     53  * @(#)move.c	1.2 (Berkeley) 3/28/83
     54  */
     55 
     56 #undef	CTRL
     57 #define	CTRL(c)		(c - 'A' + 1)
     58 
     59 char	*Movenames[] = {
     60 		"M_DISCARD", "M_DRAW", "M_PLAY", "M_ORDER"
     61 	};
     62 
     63 void
     64 domove()
     65 {
     66 	PLAY	*pp;
     67 	int	i, j;
     68 	bool	goodplay;
     69 
     70 	pp = &Player[Play];
     71 	if (Play == PLAYER)
     72 		getmove();
     73 	else
     74 		calcmove();
     75 	Next = FALSE;
     76 	goodplay = TRUE;
     77 	switch (Movetype) {
     78 	  case M_DISCARD:
     79 		if (haspicked(pp)) {
     80 			if (pp->hand[Card_no] == C_INIT)
     81 				if (Card_no == 6)
     82 					Finished = TRUE;
     83 				else
     84 					error("no card there");
     85 			else {
     86 				if (issafety(pp->hand[Card_no])) {
     87 					error("discard a safety?");
     88 					goodplay = FALSE;
     89 					break;
     90 				}
     91 				Discard = pp->hand[Card_no];
     92 				pp->hand[Card_no] = C_INIT;
     93 				Next = TRUE;
     94 				if (Play == PLAYER)
     95 					account(Discard);
     96 			}
     97 		}
     98 		else
     99 			error("must pick first");
    100 		break;
    101 	  case M_PLAY:
    102 		goodplay = playcard(pp);
    103 		break;
    104 	  case M_DRAW:
    105 		Card_no = 0;
    106 		if (Topcard <= Deck)
    107 			error("no more cards");
    108 		else if (haspicked(pp))
    109 			error("already picked");
    110 		else {
    111 			pp->hand[0] = *--Topcard;
    112 #ifdef DEBUG
    113 			if (Debug)
    114 				fprintf(outf, "DOMOVE: Draw %s\n", C_name[*Topcard]);
    115 #endif
    116 acc:
    117 			if (Play == COMP) {
    118 				account(*Topcard);
    119 				if (issafety(*Topcard))
    120 					pp->safety[*Topcard-S_CONV] = S_IN_HAND;
    121 			}
    122 			if (pp->hand[1] == C_INIT && Topcard > Deck) {
    123 				Card_no = 1;
    124 				pp->hand[1] = *--Topcard;
    125 #ifdef DEBUG
    126 				if (Debug)
    127 					fprintf(outf, "DOMOVE: Draw %s\n", C_name[*Topcard]);
    128 #endif
    129 				goto acc;
    130 			}
    131 			pp->new_battle = FALSE;
    132 			pp->new_speed = FALSE;
    133 		}
    134 		break;
    135 
    136 	  case M_ORDER:
    137 		break;
    138 	}
    139 	/*
    140 	 * move blank card to top by one of two methods.  If the
    141 	 * computer's hand was sorted, the randomness for picking
    142 	 * between equally valued cards would be lost
    143 	 */
    144 	if (Order && Movetype != M_DRAW && goodplay && pp == &Player[PLAYER])
    145 		sort(pp->hand);
    146 	else
    147 		for (i = 1; i < HAND_SZ; i++)
    148 			if (pp->hand[i] == C_INIT) {
    149 				for (j = 0; pp->hand[j] == C_INIT; j++)
    150 					if (j >= HAND_SZ) {
    151 						j = 0;
    152 						break;
    153 					}
    154 				pp->hand[i] = pp->hand[j];
    155 				pp->hand[j] = C_INIT;
    156 			}
    157 	if (Topcard <= Deck)
    158 		check_go();
    159 	if (Next)
    160 		nextplay();
    161 }
    162 
    163 /*
    164  *	Check and see if either side can go.  If they cannot,
    165  * the game is over
    166  */
    167 void
    168 check_go()
    169 {
    170 	CARD	card;
    171 	PLAY	*pp, *op;
    172 	int	i;
    173 
    174 	for (pp = Player; pp < &Player[2]; pp++) {
    175 		op = (pp == &Player[COMP] ? &Player[PLAYER] : &Player[COMP]);
    176 		for (i = 0; i < HAND_SZ; i++) {
    177 			card = pp->hand[i];
    178 			if (issafety(card) || canplay(pp, op, card)) {
    179 #ifdef DEBUG
    180 				if (Debug) {
    181 					fprintf(outf, "CHECK_GO: can play %s (%d), ", C_name[card], card);
    182 					fprintf(outf, "issafety(card) = %d, ", issafety(card));
    183 					fprintf(outf, "canplay(pp, op, card) = %d\n", canplay(pp, op, card));
    184 				}
    185 #endif
    186 				return;
    187 			}
    188 #ifdef DEBUG
    189 			else if (Debug)
    190 				fprintf(outf, "CHECK_GO: cannot play %s\n",
    191 				    C_name[card]);
    192 #endif
    193 		}
    194 	}
    195 	Finished = TRUE;
    196 }
    197 
    198 int
    199 playcard(pp)
    200 	PLAY	*pp;
    201 {
    202 	int	v;
    203 	CARD	card;
    204 
    205 	/*
    206 	 * check and see if player has picked
    207 	 */
    208 	switch (pp->hand[Card_no]) {
    209 	  default:
    210 		if (!haspicked(pp))
    211 mustpick:
    212 			return error("must pick first");
    213 	  case C_GAS_SAFE:	case C_SPARE_SAFE:
    214 	  case C_DRIVE_SAFE:	case C_RIGHT_WAY:
    215 		break;
    216 	}
    217 
    218 	card = pp->hand[Card_no];
    219 #ifdef DEBUG
    220 	if (Debug)
    221 		fprintf(outf, "PLAYCARD: Card = %s\n", C_name[card]);
    222 #endif
    223 	Next = FALSE;
    224 	switch (card) {
    225 	  case C_200:
    226 		if (pp->nummiles[C_200] == 2)
    227 			return error("only two 200's per hand");
    228 	  case C_100:	case C_75:
    229 		if (pp->speed == C_LIMIT)
    230 			return error("limit of 50");
    231 	  case C_50:
    232 		if (pp->mileage + Value[card] > End)
    233 			return error("puts you over %d", End);
    234 	  case C_25:
    235 		if (!pp->can_go)
    236 			return error("cannot move now");
    237 		pp->nummiles[card]++;
    238 		v = Value[card];
    239 		pp->total += v;
    240 		pp->hand_tot += v;
    241 		if ((pp->mileage += v) == End)
    242 			check_ext(FALSE);
    243 		break;
    244 
    245 	  case C_GAS:	case C_SPARE:	case C_REPAIRS:
    246 		if (pp->battle != opposite(card))
    247 			return error("can't play \"%s\"", C_name[card]);
    248 		pp->battle = card;
    249 		if (pp->safety[S_RIGHT_WAY] == S_PLAYED)
    250 			pp->can_go = TRUE;
    251 		break;
    252 
    253 	  case C_GO:
    254 		if (pp->battle != C_INIT && pp->battle != C_STOP
    255 		    && !isrepair(pp->battle))
    256 			return error("cannot play \"Go\" on a \"%s\"",
    257 			    C_name[pp->battle]);
    258 		pp->battle = C_GO;
    259 		pp->can_go = TRUE;
    260 		break;
    261 
    262 	  case C_END_LIMIT:
    263 		if (pp->speed != C_LIMIT)
    264 			return error("not limited");
    265 		pp->speed = C_END_LIMIT;
    266 		break;
    267 
    268 	  case C_EMPTY:	case C_FLAT:	case C_CRASH:
    269 	  case C_STOP:
    270 		pp = &Player[other(Play)];
    271 		if (!pp->can_go)
    272 			return error("opponent cannot go");
    273 		else if (pp->safety[safety(card) - S_CONV] == S_PLAYED)
    274 protected:
    275 			return error("opponent is protected");
    276 		pp->battle = card;
    277 		pp->new_battle = TRUE;
    278 		pp->can_go = FALSE;
    279 		pp = &Player[Play];
    280 		break;
    281 
    282 	  case C_LIMIT:
    283 		pp = &Player[other(Play)];
    284 		if (pp->speed == C_LIMIT)
    285 			return error("opponent has limit");
    286 		if (pp->safety[S_RIGHT_WAY] == S_PLAYED)
    287 			goto protected;
    288 		pp->speed = C_LIMIT;
    289 		pp->new_speed = TRUE;
    290 		pp = &Player[Play];
    291 		break;
    292 
    293 	  case C_GAS_SAFE:	case C_SPARE_SAFE:
    294 	  case C_DRIVE_SAFE:	case C_RIGHT_WAY:
    295 		if (pp->battle == opposite(card)
    296 		    || (card == C_RIGHT_WAY && pp->speed == C_LIMIT)) {
    297 			if (!(card == C_RIGHT_WAY && !isrepair(pp->battle))) {
    298 				pp->battle = C_GO;
    299 				pp->can_go = TRUE;
    300 			}
    301 			if (card == C_RIGHT_WAY && pp->speed == C_LIMIT)
    302 				pp->speed = C_INIT;
    303 			if (pp->new_battle
    304 			    || (pp->new_speed && card == C_RIGHT_WAY)) {
    305 				pp->coups[card - S_CONV] = TRUE;
    306 				pp->total += SC_COUP;
    307 				pp->hand_tot += SC_COUP;
    308 				pp->coupscore += SC_COUP;
    309 			}
    310 		}
    311 		/*
    312 		 * if not coup, must pick first
    313 		 */
    314 		else if (pp->hand[0] == C_INIT && Topcard > Deck)
    315 			goto mustpick;
    316 		pp->safety[card - S_CONV] = S_PLAYED;
    317 		pp->total += SC_SAFETY;
    318 		pp->hand_tot += SC_SAFETY;
    319 		if ((pp->safescore += SC_SAFETY) == NUM_SAFE * SC_SAFETY) {
    320 			pp->total += SC_ALL_SAFE;
    321 			pp->hand_tot += SC_ALL_SAFE;
    322 		}
    323 		if (card == C_RIGHT_WAY) {
    324 			if (pp->speed == C_LIMIT)
    325 				pp->speed = C_INIT;
    326 			if (pp->battle == C_STOP || pp->battle == C_INIT) {
    327 				pp->can_go = TRUE;
    328 				pp->battle = C_INIT;
    329 			}
    330 			if (!pp->can_go && isrepair(pp->battle))
    331 				pp->can_go = TRUE;
    332 		}
    333 		Next = -1;
    334 		break;
    335 
    336 	  case C_INIT:
    337 		error("no card there");
    338 		Next = -1;
    339 		break;
    340 	}
    341 	if (pp == &Player[PLAYER])
    342 		account(card);
    343 	pp->hand[Card_no] = C_INIT;
    344 	Next = (Next == (bool)-1 ? FALSE : TRUE);
    345 	return TRUE;
    346 }
    347 
    348 void
    349 getmove()
    350 {
    351 	char	c;
    352 #ifdef EXTRAP
    353 	static bool	last_ex = FALSE;	/* set if last command was E */
    354 
    355 	if (last_ex) {
    356 		undoex();
    357 		prboard();
    358 		last_ex = FALSE;
    359 	}
    360 #endif
    361 	for (;;) {
    362 		prompt(MOVEPROMPT);
    363 		leaveok(Board, FALSE);
    364 		refresh();
    365 		while ((c = readch()) == killchar() || c == erasechar())
    366 			continue;
    367 		if (islower(c))
    368 			c = toupper(c);
    369 		if (isprint(c) && !isspace(c)) {
    370 			addch(c);
    371 			refresh();
    372 		}
    373 		switch (c) {
    374 		  case 'P':		/* Pick */
    375 			Movetype = M_DRAW;
    376 			goto ret;
    377 		  case 'U':		/* Use Card */
    378 		  case 'D':		/* Discard Card */
    379 			if ((Card_no = getcard()) < 0)
    380 				break;
    381 			Movetype = (c == 'U' ? M_PLAY : M_DISCARD);
    382 			goto ret;
    383 		  case 'O':		/* Order */
    384 			Order = !Order;
    385 			if (Window == W_SMALL) {
    386 				if (!Order)
    387 					mvwaddstr(Score, 12, 21,
    388 						  "o: order hand");
    389 				else
    390 					mvwaddstr(Score, 12, 21,
    391 						  "o: stop ordering");
    392 				wclrtoeol(Score);
    393 			}
    394 			Movetype = M_ORDER;
    395 			goto ret;
    396 		  case 'Q':		/* Quit */
    397 			rub(0);		/* Same as a rubout */
    398 			break;
    399 		  case 'W':		/* Window toggle */
    400 			Window = nextwin(Window);
    401 			newscore();
    402 			prscore(TRUE);
    403 			wrefresh(Score);
    404 			break;
    405 		  case 'R':		/* Redraw screen */
    406 		  case CTRL('L'):
    407 			wrefresh(curscr);
    408 			break;
    409 		  case 'S':		/* Save game */
    410 			On_exit = FALSE;
    411 			save();
    412 			break;
    413 		  case 'E':		/* Extrapolate */
    414 #ifdef EXTRAP
    415 			if (last_ex)
    416 				break;
    417 			Finished = TRUE;
    418 			if (Window != W_FULL)
    419 				newscore();
    420 			prscore(FALSE);
    421 			wrefresh(Score);
    422 			last_ex = TRUE;
    423 			Finished = FALSE;
    424 #else
    425 			error("%c: command not implemented", c);
    426 #endif
    427 			break;
    428 		  case '\r':		/* Ignore RETURNs and	*/
    429 		  case '\n':		/* Line Feeds		*/
    430 		  case ' ':		/* Spaces		*/
    431 		  case '\0':		/* and nulls		*/
    432 			break;
    433 #ifdef DEBUG
    434 		  case 'Z':		/* Debug code */
    435 			if (!Debug && outf == NULL) {
    436 				char	buf[MAXPATHLEN];
    437 
    438 				prompt(FILEPROMPT);
    439 				leaveok(Board, FALSE);
    440 				refresh();
    441 				sp = buf;
    442 				while ((*sp = readch()) != '\n') {
    443 					if (*sp == killchar())
    444 						goto over;
    445 					else if (*sp == erasechar()) {
    446 						if (--sp < buf)
    447 							sp = buf;
    448 						else {
    449 							addch('\b');
    450 							if (*sp < ' ')
    451 							    addch('\b');
    452 							clrtoeol();
    453 						}
    454 					}
    455 					else
    456 						addstr(unctrl(*sp++));
    457 					refresh();
    458 				}
    459 				*sp = '\0';
    460 				leaveok(Board, TRUE);
    461 				if ((outf = fopen(buf, "w")) == NULL)
    462 					warn("%s", buf);
    463 				setbuf(outf, (char *)NULL);
    464 			}
    465 			Debug = !Debug;
    466 			break;
    467 #endif
    468 		  default:
    469 			error("unknown command: %s", unctrl(c));
    470 			break;
    471 		}
    472 	}
    473 ret:
    474 	leaveok(Board, TRUE);
    475 }
    476 
    477 /*
    478  * return whether or not the player has picked
    479  */
    480 int
    481 haspicked(pp)
    482 	PLAY	*pp;
    483 {
    484 	int	card;
    485 
    486 	if (Topcard <= Deck)
    487 		return TRUE;
    488 	switch (pp->hand[Card_no]) {
    489 	  case C_GAS_SAFE:	case C_SPARE_SAFE:
    490 	  case C_DRIVE_SAFE:	case C_RIGHT_WAY:
    491 		card = 1;
    492 		break;
    493 	  default:
    494 		card = 0;
    495 		break;
    496 	}
    497 	return (pp->hand[card] != C_INIT);
    498 }
    499 
    500 void
    501 account(card)
    502 	CARD	card;
    503 {
    504 	CARD	oppos;
    505 
    506 	if (card == C_INIT)
    507 		return;
    508 	++Numseen[card];
    509 	if (Play == COMP)
    510 		switch (card) {
    511 		  case C_GAS_SAFE:
    512 		  case C_SPARE_SAFE:
    513 		  case C_DRIVE_SAFE:
    514 			oppos = opposite(card);
    515 			Numgos += Numcards[oppos] - Numseen[oppos];
    516 			break;
    517 		  case C_CRASH:
    518 		  case C_FLAT:
    519 		  case C_EMPTY:
    520 		  case C_STOP:
    521 			Numgos++;
    522 			break;
    523 		}
    524 }
    525 
    526 void
    527 prompt(promptno)
    528 	int	promptno;
    529 {
    530 	static char	*names[] = {
    531 				">>:Move:",
    532 				"Really?",
    533 				"Another hand?",
    534 				"Another game?",
    535 				"Save game?",
    536 				"Same file?",
    537 				"file:",
    538 				"Extension?",
    539 				"Overwrite file?",
    540 			};
    541 	static int	last_prompt = -1;
    542 
    543 	if (promptno == last_prompt)
    544 		move(MOVE_Y, MOVE_X + strlen(names[promptno]) + 1);
    545 	else {
    546 		move(MOVE_Y, MOVE_X);
    547 		if (promptno == MOVEPROMPT)
    548 			standout();
    549 		addstr(names[promptno]);
    550 		if (promptno == MOVEPROMPT)
    551 			standend();
    552 		addch(' ');
    553 		last_prompt = promptno;
    554 	}
    555 	clrtoeol();
    556 }
    557 
    558 void
    559 sort(hand)
    560 	CARD	*hand;
    561 {
    562 	CARD	*cp, *tp;
    563 	CARD	temp;
    564 
    565 	cp = hand;
    566 	hand += HAND_SZ;
    567 	for ( ; cp < &hand[-1]; cp++)
    568 		for (tp = cp + 1; tp < hand; tp++)
    569 			if (*cp > *tp) {
    570 				temp = *cp;
    571 				*cp = *tp;
    572 				*tp = temp;
    573 			}
    574 }
    575