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
33 * @(#)io.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/cribbage/io.c,v 1.5.2.2 2001/02/18 02:20:31 kris Exp $
35 * $DragonFly: src/games/cribbage/io.c,v 1.3 2004/07/27 07:37:39 asmodai Exp $
57 #define CTRL(X) (X - 'A' + 1)
59 char linebuf[LINESIZE];
61 char *rankname[RANKS] = {
62 "ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
63 "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING"
66 char *rankchar[RANKS] = {
67 "A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"
70 char *suitname[SUITS] = {"SPADES", "HEARTS", "DIAMONDS", "CLUBS"};
72 char *suitchar[SUITS] = {"S", "H", "D", "C"};
76 * Call msgcrd in one of two forms
84 return (msgcrd(c, TRUE, NULL, TRUE));
86 return (msgcrd(c, FALSE, " of ", FALSE));
91 * Print the value of a card in ascii
94 msgcrd(c, brfrank, mid, brfsuit)
96 BOOLEAN brfrank, brfsuit;
99 if (c.rank == EMPTY || c.suit == EMPTY)
102 addmsg("%1.1s", rankchar[c.rank]);
104 addmsg("%s", rankname[c.rank]);
108 addmsg("%1.1s", suitchar[c.suit]);
110 addmsg("%s", suitname[c.suit]);
119 printcard(win, cardno, c, blank)
125 prcard(win, cardno * 2, cardno, c, blank);
130 * Print out a card on the window at the specified location
133 prcard(win, y, x, c, blank)
142 mvwaddstr(win, y + 0, x, "+-----+");
143 mvwaddstr(win, y + 1, x, "| |");
144 mvwaddstr(win, y + 2, x, "| |");
145 mvwaddstr(win, y + 3, x, "| |");
146 mvwaddstr(win, y + 4, x, "+-----+");
148 mvwaddch(win, y + 1, x + 1, rankchar[c.rank][0]);
149 waddch(win, suitchar[c.suit][0]);
150 mvwaddch(win, y + 3, x + 4, rankchar[c.rank][0]);
151 waddch(win, suitchar[c.suit][0]);
157 * Print a hand of n cards
160 prhand(h, n, win, blank)
169 for (i = 0; i < n; i++)
170 printcard(win, i, *h++, blank);
176 * reads a card, supposedly in hand, accepting unambigous brief
177 * input, returns the index of the card found...
180 infrom(hand, n, prompt)
189 printf("\nINFROM: %d = n < 1!!\n", n);
194 if (incard(&crd)) { /* if card is full card */
195 if (!isone(crd, hand, n))
196 msg("That's not in your hand");
198 for (i = 0; i < n; i++)
199 if (hand[i].rank == crd.rank &&
200 hand[i].suit == crd.suit)
203 printf("\nINFROM: isone or something messed up\n");
208 } else /* if not full card... */
209 if (crd.rank != EMPTY) {
210 for (i = 0; i < n; i++)
211 if (hand[i].rank == crd.rank)
214 msg("No such rank in your hand");
216 for (j = i + 1; j < n; j++)
217 if (hand[j].rank == crd.rank)
220 msg("Ambiguous rank");
225 msg("Sorry, I missed that");
232 * Inputs a card in any format. It reads a line ending with a CR
233 * and then parses it.
246 if (!(line = getline()))
249 while (*p1 != ' ' && *p1 != '\0')
255 /* IMPORTANT: no real card has 2 char first name */
256 if (strlen(p) == 2) { /* check for short form */
258 for (i = 0; i < RANKS; i++) {
259 if (*p == *rankchar[i]) {
265 goto gotit; /* it's nothing... */
266 ++p; /* advance to next char */
268 for (i = 0; i < SUITS; i++) {
269 if (*p == *suitchar[i]) {
279 for (i = 0; i < RANKS; i++) {
280 if (!strcmp(p, rankname[i]) || !strcmp(p, rankchar[i])) {
288 while (*p1 != ' ' && *p1 != '\0')
293 if (!strcmp("OF", p)) {
295 while (*p1 != ' ' && *p1 != '\0')
302 for (i = 0; i < SUITS; i++) {
303 if (!strcmp(p, suitname[i]) || !strcmp(p, suitchar[i])) {
318 * Reads and converts to upper case
334 * Reads in a decimal number and makes sure it is between "lo" and
338 number(lo, hi, prompt)
347 if (!(p = getline()) || *p == '\0') {
348 msg(quiet ? "Not a number" :
349 "That doesn't look like a number");
357 while (isdigit(*p)) {
358 sum = 10 * sum + (*p - '0');
362 if (*p != ' ' && *p != '\t' && *p != '\0')
364 if (sum >= lo && sum <= hi)
367 msg("that doesn't look like a number, try again --> ");
369 msg("%d is not between %d and %d inclusive, try again --> ",
377 * Display a message at the top of the screen.
379 char Msgbuf[BUFSIZ] = {'\0'};
381 static int Newpos = 0;
384 msg(const char *fmt, ...)
389 (void)vsprintf(&Msgbuf[Newpos], fmt, ap);
396 * Add things to the current message
399 addmsg(const char *fmt, ...)
404 (void)vsprintf(&Msgbuf[Newpos], fmt, ap);
417 static int lastline = 0;
421 /* All messages should start with uppercase */
422 mvaddch(lastline + Y_MSG_START, SCORE_X, ' ');
423 if (islower(Msgbuf[0]) && Msgbuf[1] != ')')
424 Msgbuf[0] = toupper(Msgbuf[0]);
427 if (len / MSG_X + Lineno >= MSG_Y) {
428 while (Lineno < MSG_Y) {
429 wmove(Msgwin, Lineno++, 0);
434 mvaddch(Lineno + Y_MSG_START, SCORE_X, '*');
437 mvwaddstr(Msgwin, Lineno, 0, mp);
438 if ((len = strlen(mp)) > MSG_X) {
440 for (mp = &mp[MSG_X - 1]; *mp != ' '; mp--)
445 wmove(Msgwin, Lineno, mp - omp);
448 if (++Lineno >= MSG_Y)
450 } while (len > MSG_X);
461 * Wait for the user to type ' ' before doing anything else
466 static char prompt[] = {'-', '-', 'M', 'o', 'r', 'e', '-', '-', '\0'};
468 if (Mpos + sizeof prompt < MSG_X)
469 wmove(Msgwin, Lineno > 0 ? Lineno - 1 : MSG_Y - 1, Mpos);
471 mvwaddch(Msgwin, Lineno, 0, ' ');
473 if (++Lineno >= MSG_Y)
476 waddstr(Msgwin, prompt);
483 * Sit around until the guy types the right key
492 while ((c = readchar()) != '\n')
495 while (readchar() != ch)
501 * Reads and returns a character, checking for gross input errors
511 while (read(STDIN_FILENO, &c, sizeof(char)) <= 0)
512 if (cnt++ > 100) { /* if we are getting infinite EOFs */
513 bye(); /* quit the game */
516 if (c == CTRL('L')) {
528 * Reads the next line up to '\n' or EOF. Multiple spaces are
529 * compressed to one space; a space is inserted before a ','
540 getyx(stdscr, oy, ox);
542 /* loop reading in the string, and put it in a temporary buffer */
543 for (sp = linebuf; (c = readchar()) != '\n'; clrtoeol(), refresh()) {
547 if (c == erasechar()) { /* process erase character */
552 for (i = strlen(unctrl(*sp)); i; i--)
557 if (c == killchar()) { /* process kill
563 if (sp == linebuf && c == ' ')
565 if (sp >= &linebuf[LINESIZE - 1] || !(isprint(c) || c == ' '))
590 * Leave the program, cleaning things up as we go.
595 signal(SIGINT, SIG_IGN);
596 mvcur(0, COLS - 1, LINES - 1, 0);