2 * Copyright (c) 1980, 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
36 static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 5/31/93";
38 static const char rcsid[] =
39 "$FreeBSD: src/games/cribbage/io.c,v 1.5.2.2 2001/02/18 02:20:31 kris Exp $";
65 #define CTRL(X) (X - 'A' + 1)
67 char linebuf[LINESIZE];
69 char *rankname[RANKS] = {
70 "ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
71 "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING"
74 char *rankchar[RANKS] = {
75 "A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"
78 char *suitname[SUITS] = {"SPADES", "HEARTS", "DIAMONDS", "CLUBS"};
80 char *suitchar[SUITS] = {"S", "H", "D", "C"};
84 * Call msgcrd in one of two forms
92 return (msgcrd(c, TRUE, NULL, TRUE));
94 return (msgcrd(c, FALSE, " of ", FALSE));
99 * Print the value of a card in ascii
102 msgcrd(c, brfrank, mid, brfsuit)
104 BOOLEAN brfrank, brfsuit;
107 if (c.rank == EMPTY || c.suit == EMPTY)
110 addmsg("%1.1s", rankchar[c.rank]);
112 addmsg("%s", rankname[c.rank]);
116 addmsg("%1.1s", suitchar[c.suit]);
118 addmsg("%s", suitname[c.suit]);
127 printcard(win, cardno, c, blank)
133 prcard(win, cardno * 2, cardno, c, blank);
138 * Print out a card on the window at the specified location
141 prcard(win, y, x, c, blank)
150 mvwaddstr(win, y + 0, x, "+-----+");
151 mvwaddstr(win, y + 1, x, "| |");
152 mvwaddstr(win, y + 2, x, "| |");
153 mvwaddstr(win, y + 3, x, "| |");
154 mvwaddstr(win, y + 4, x, "+-----+");
156 mvwaddch(win, y + 1, x + 1, rankchar[c.rank][0]);
157 waddch(win, suitchar[c.suit][0]);
158 mvwaddch(win, y + 3, x + 4, rankchar[c.rank][0]);
159 waddch(win, suitchar[c.suit][0]);
165 * Print a hand of n cards
168 prhand(h, n, win, blank)
177 for (i = 0; i < n; i++)
178 printcard(win, i, *h++, blank);
184 * reads a card, supposedly in hand, accepting unambigous brief
185 * input, returns the index of the card found...
188 infrom(hand, n, prompt)
197 printf("\nINFROM: %d = n < 1!!\n", n);
202 if (incard(&crd)) { /* if card is full card */
203 if (!isone(crd, hand, n))
204 msg("That's not in your hand");
206 for (i = 0; i < n; i++)
207 if (hand[i].rank == crd.rank &&
208 hand[i].suit == crd.suit)
211 printf("\nINFROM: isone or something messed up\n");
216 } else /* if not full card... */
217 if (crd.rank != EMPTY) {
218 for (i = 0; i < n; i++)
219 if (hand[i].rank == crd.rank)
222 msg("No such rank in your hand");
224 for (j = i + 1; j < n; j++)
225 if (hand[j].rank == crd.rank)
228 msg("Ambiguous rank");
233 msg("Sorry, I missed that");
240 * Inputs a card in any format. It reads a line ending with a CR
241 * and then parses it.
254 if (!(line = getline()))
257 while (*p1 != ' ' && *p1 != '\0')
263 /* IMPORTANT: no real card has 2 char first name */
264 if (strlen(p) == 2) { /* check for short form */
266 for (i = 0; i < RANKS; i++) {
267 if (*p == *rankchar[i]) {
273 goto gotit; /* it's nothing... */
274 ++p; /* advance to next char */
276 for (i = 0; i < SUITS; i++) {
277 if (*p == *suitchar[i]) {
287 for (i = 0; i < RANKS; i++) {
288 if (!strcmp(p, rankname[i]) || !strcmp(p, rankchar[i])) {
296 while (*p1 != ' ' && *p1 != '\0')
301 if (!strcmp("OF", p)) {
303 while (*p1 != ' ' && *p1 != '\0')
310 for (i = 0; i < SUITS; i++) {
311 if (!strcmp(p, suitname[i]) || !strcmp(p, suitchar[i])) {
326 * Reads and converts to upper case
342 * Reads in a decimal number and makes sure it is between "lo" and
346 number(lo, hi, prompt)
355 if (!(p = getline()) || *p == '\0') {
356 msg(quiet ? "Not a number" :
357 "That doesn't look like a number");
365 while (isdigit(*p)) {
366 sum = 10 * sum + (*p - '0');
370 if (*p != ' ' && *p != '\t' && *p != '\0')
372 if (sum >= lo && sum <= hi)
375 msg("that doesn't look like a number, try again --> ");
377 msg("%d is not between %d and %d inclusive, try again --> ",
385 * Display a message at the top of the screen.
387 char Msgbuf[BUFSIZ] = {'\0'};
389 static int Newpos = 0;
393 msg(const char *fmt, ...)
407 (void)vsprintf(&Msgbuf[Newpos], fmt, ap);
414 * Add things to the current message
418 addmsg(const char *fmt, ...)
420 addmsg(fmt, va_alist)
432 (void)vsprintf(&Msgbuf[Newpos], fmt, ap);
445 static int lastline = 0;
449 /* All messages should start with uppercase */
450 mvaddch(lastline + Y_MSG_START, SCORE_X, ' ');
451 if (islower(Msgbuf[0]) && Msgbuf[1] != ')')
452 Msgbuf[0] = toupper(Msgbuf[0]);
455 if (len / MSG_X + Lineno >= MSG_Y) {
456 while (Lineno < MSG_Y) {
457 wmove(Msgwin, Lineno++, 0);
462 mvaddch(Lineno + Y_MSG_START, SCORE_X, '*');
465 mvwaddstr(Msgwin, Lineno, 0, mp);
466 if ((len = strlen(mp)) > MSG_X) {
468 for (mp = &mp[MSG_X - 1]; *mp != ' '; mp--)
473 wmove(Msgwin, Lineno, mp - omp);
476 if (++Lineno >= MSG_Y)
478 } while (len > MSG_X);
489 * Wait for the user to type ' ' before doing anything else
494 static char prompt[] = {'-', '-', 'M', 'o', 'r', 'e', '-', '-', '\0'};
496 if (Mpos + sizeof prompt < MSG_X)
497 wmove(Msgwin, Lineno > 0 ? Lineno - 1 : MSG_Y - 1, Mpos);
499 mvwaddch(Msgwin, Lineno, 0, ' ');
501 if (++Lineno >= MSG_Y)
504 waddstr(Msgwin, prompt);
511 * Sit around until the guy types the right key
520 while ((c = readchar()) != '\n')
523 while (readchar() != ch)
529 * Reads and returns a character, checking for gross input errors
539 while (read(STDIN_FILENO, &c, sizeof(char)) <= 0)
540 if (cnt++ > 100) { /* if we are getting infinite EOFs */
541 bye(); /* quit the game */
544 if (c == CTRL('L')) {
556 * Reads the next line up to '\n' or EOF. Multiple spaces are
557 * compressed to one space; a space is inserted before a ','
568 getyx(stdscr, oy, ox);
570 /* loop reading in the string, and put it in a temporary buffer */
571 for (sp = linebuf; (c = readchar()) != '\n'; clrtoeol(), refresh()) {
575 if (c == erasechar()) { /* process erase character */
580 for (i = strlen(unctrl(*sp)); i; i--)
585 if (c == killchar()) { /* process kill
591 if (sp == linebuf && c == ' ')
593 if (sp >= &linebuf[LINESIZE - 1] || !(isprint(c) || c == ' '))
618 * Leave the program, cleaning things up as we go.
623 signal(SIGINT, SIG_IGN);
624 mvcur(0, COLS - 1, LINES - 1, 0);