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