2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)move.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/mille/move.c,v 1.6 1999/12/12 06:17:24 billf Exp $
35 * $DragonFly: src/games/mille/move.c,v 1.5 2006/08/27 17:17:23 pavalos Exp $
45 * @(#)move.c 1.2 (Berkeley) 3/28/83
49 #define CTRL(c) (c - 'A' + 1)
51 const char *Movenames
[] = {
52 "M_DISCARD", "M_DRAW", "M_PLAY", "M_ORDER"
55 static void check_go (void);
56 static void getmove (void);
57 static int haspicked (PLAY
*);
58 static bool playcard (PLAY
*);
77 if (pp
->hand
[Card_no
] == C_INIT
)
81 error("no card there");
83 if (issafety(pp
->hand
[Card_no
])) {
84 error("discard a safety?");
88 Discard
= pp
->hand
[Card_no
];
89 pp
->hand
[Card_no
] = C_INIT
;
96 error("must pick first");
99 goodplay
= playcard(pp
);
104 error("no more cards");
105 else if (haspicked(pp
))
106 error("already picked");
108 pp
->hand
[0] = *--Topcard
;
111 fprintf(outf
, "DOMOVE: Draw %s\n", C_name
[*Topcard
]);
116 if (issafety(*Topcard
))
117 pp
->safety
[*Topcard
-S_CONV
] = S_IN_HAND
;
119 if (pp
->hand
[1] == C_INIT
&& Topcard
> Deck
) {
121 pp
->hand
[1] = *--Topcard
;
124 fprintf(outf
, "DOMOVE: Draw %s\n", C_name
[*Topcard
]);
128 pp
->new_battle
= FALSE
;
129 pp
->new_speed
= FALSE
;
137 * move blank card to top by one of two methods. If the
138 * computer's hand was sorted, the randomness for picking
139 * between equally valued cards would be lost
141 if (Order
&& Movetype
!= M_DRAW
&& goodplay
&& pp
== &Player
[PLAYER
])
144 for (i
= 1; i
< HAND_SZ
; i
++)
145 if (pp
->hand
[i
] == C_INIT
) {
146 for (j
= 0; pp
->hand
[j
] == C_INIT
; j
++)
151 pp
->hand
[i
] = pp
->hand
[j
];
152 pp
->hand
[j
] = C_INIT
;
161 * Check and see if either side can go. If they cannot,
172 for (pp
= Player
; pp
< &Player
[2]; pp
++) {
173 op
= (pp
== &Player
[COMP
] ? &Player
[PLAYER
] : &Player
[COMP
]);
174 for (i
= 0; i
< HAND_SZ
; i
++) {
176 if (issafety(card
) || canplay(pp
, op
, card
)) {
179 fprintf(outf
, "CHECK_GO: can play %s (%d), ", C_name
[card
], card
);
180 fprintf(outf
, "issafety(card) = %d, ", issafety(card
));
181 fprintf(outf
, "canplay(pp, op, card) = %d\n", canplay(pp
, op
, card
));
188 fprintf(outf
, "CHECK_GO: cannot play %s\n",
203 * check and see if player has picked
205 switch (pp
->hand
[Card_no
]) {
209 return error("must pick first");
210 case C_GAS_SAFE
: case C_SPARE_SAFE
:
211 case C_DRIVE_SAFE
: case C_RIGHT_WAY
:
215 card
= pp
->hand
[Card_no
];
218 fprintf(outf
, "PLAYCARD: Card = %s\n", C_name
[card
]);
223 if (pp
->nummiles
[C_200
] == 2)
224 return error("only two 200's per hand");
225 case C_100
: case C_75
:
226 if (pp
->speed
== C_LIMIT
)
227 return error("limit of 50");
229 if (pp
->mileage
+ Value
[card
] > End
)
230 return error("puts you over %d", End
);
233 return error("cannot move now");
234 pp
->nummiles
[card
]++;
238 if ((pp
->mileage
+= v
) == End
)
242 case C_GAS
: case C_SPARE
: case C_REPAIRS
:
243 if (pp
->battle
!= opposite(card
))
244 return error("can't play \"%s\"", C_name
[card
]);
246 if (pp
->safety
[S_RIGHT_WAY
] == S_PLAYED
)
251 if (pp
->battle
!= C_INIT
&& pp
->battle
!= C_STOP
252 && !isrepair(pp
->battle
))
253 return error("cannot play \"Go\" on a \"%s\"",
260 if (pp
->speed
!= C_LIMIT
)
261 return error("not limited");
262 pp
->speed
= C_END_LIMIT
;
265 case C_EMPTY
: case C_FLAT
: case C_CRASH
:
267 pp
= &Player
[other(Play
)];
269 return error("opponent cannot go");
270 else if (pp
->safety
[safety(card
) - S_CONV
] == S_PLAYED
)
272 return error("opponent is protected");
274 pp
->new_battle
= TRUE
;
280 pp
= &Player
[other(Play
)];
281 if (pp
->speed
== C_LIMIT
)
282 return error("opponent has limit");
283 if (pp
->safety
[S_RIGHT_WAY
] == S_PLAYED
)
286 pp
->new_speed
= TRUE
;
290 case C_GAS_SAFE
: case C_SPARE_SAFE
:
291 case C_DRIVE_SAFE
: case C_RIGHT_WAY
:
292 if (pp
->battle
== opposite(card
)
293 || (card
== C_RIGHT_WAY
&& pp
->speed
== C_LIMIT
)) {
294 if (!(card
== C_RIGHT_WAY
&& !isrepair(pp
->battle
))) {
298 if (card
== C_RIGHT_WAY
&& pp
->speed
== C_LIMIT
)
301 || (pp
->new_speed
&& card
== C_RIGHT_WAY
)) {
302 pp
->coups
[card
- S_CONV
] = TRUE
;
303 pp
->total
+= SC_COUP
;
304 pp
->hand_tot
+= SC_COUP
;
305 pp
->coupscore
+= SC_COUP
;
309 * if not coup, must pick first
311 else if (pp
->hand
[0] == C_INIT
&& Topcard
> Deck
)
313 pp
->safety
[card
- S_CONV
] = S_PLAYED
;
314 pp
->total
+= SC_SAFETY
;
315 pp
->hand_tot
+= SC_SAFETY
;
316 if ((pp
->safescore
+= SC_SAFETY
) == NUM_SAFE
* SC_SAFETY
) {
317 pp
->total
+= SC_ALL_SAFE
;
318 pp
->hand_tot
+= SC_ALL_SAFE
;
320 if (card
== C_RIGHT_WAY
) {
321 if (pp
->speed
== C_LIMIT
)
323 if (pp
->battle
== C_STOP
|| pp
->battle
== C_INIT
) {
327 if (!pp
->can_go
&& isrepair(pp
->battle
))
334 error("no card there");
338 if (pp
== &Player
[PLAYER
])
340 pp
->hand
[Card_no
] = C_INIT
;
341 Next
= (Next
== -1 ? FALSE
: TRUE
);
353 static bool last_ex
= FALSE
; /* set if last command was E */
363 leaveok(Board
, FALSE
);
365 while ((c
= readch()) == killchar() || c
== erasechar())
369 if (isprint(c
) && !isspace(c
)) {
377 case 'U': /* Use Card */
378 case 'D': /* Discard Card */
379 if ((Card_no
= getcard()) < 0)
381 Movetype
= (c
== 'U' ? M_PLAY
: M_DISCARD
);
383 case 'O': /* Order */
385 if (Window
== W_SMALL
) {
387 mvwaddstr(Score
, 12, 21,
390 mvwaddstr(Score
, 12, 21,
397 rub(0); /* Same as a rubout */
399 case 'W': /* Window toggle */
400 Window
= nextwin(Window
);
405 case 'R': /* Redraw screen */
409 case 'S': /* Save game */
413 case 'E': /* Extrapolate */
418 if (Window
!= W_FULL
)
425 error("%c: command not implemented", c
);
428 case '\r': /* Ignore RETURNs and */
429 case '\n': /* Line Feeds */
430 case ' ': /* Spaces */
431 case '\0': /* and nulls */
434 case 'Z': /* Debug code */
435 if (!Debug
&& outf
== NULL
) {
436 char buf
[MAXPATHLEN
];
439 leaveok(Board
, FALSE
);
442 while ((*sp
= readch()) != '\n') {
443 if (*sp
== killchar())
445 else if (*sp
== erasechar()) {
456 addstr(unctrl(*sp
++));
460 leaveok(Board
, TRUE
);
461 if ((outf
= fopen(buf
, "w")) == NULL
)
469 error("unknown command: %s", unctrl(c
));
474 leaveok(Board
, TRUE
);
477 * return whether or not the player has picked
487 switch (pp
->hand
[Card_no
]) {
488 case C_GAS_SAFE
: case C_SPARE_SAFE
:
489 case C_DRIVE_SAFE
: case C_RIGHT_WAY
:
496 return (pp
->hand
[card
] != C_INIT
);
513 oppos
= opposite(card
);
514 Numgos
+= Numcards
[oppos
] - Numseen
[oppos
];
528 static const char *names
[] = {
539 static int last_prompt
= -1;
541 if (promptno
== last_prompt
)
542 move(MOVE_Y
, MOVE_X
+ strlen(names
[promptno
]) + 1);
544 move(MOVE_Y
, MOVE_X
);
545 if (promptno
== MOVEPROMPT
)
547 addstr(names
[promptno
]);
548 if (promptno
== MOVEPROMPT
)
551 last_prompt
= promptno
;
564 for ( ; cp
< &hand
[-1]; cp
++)
565 for (tp
= cp
+ 1; tp
< hand
; tp
++)