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.4 2005/08/03 13:31:00 eirikn Exp $
53 #define CTRL(X) (X - 'A' + 1)
55 char linebuf[LINESIZE];
57 const char *const rankname[RANKS] = {
58 "ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
59 "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING"
62 const char *const rankchar[RANKS] = {
63 "A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"
66 const char *const suitname[SUITS] = {"SPADES", "HEARTS", "DIAMONDS", "CLUBS"};
68 const char *const suitchar[SUITS] = {"S", "H", "D", "C"};
70 static bool incard(CARD *);
71 static bool msgcrd(CARD, bool, const char *, bool);
72 static void printcard(WINDOW *, int, CARD, bool);
73 static int readchar(void);
74 static void wait_for(int);
78 * Call msgcrd in one of two forms
81 msgcard(CARD c, bool brief)
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(CARD c, bool brfrank, const char *mid, bool brfsuit)
96 if (c.rank == EMPTY || c.suit == EMPTY)
99 addmsg("%1.1s", rankchar[c.rank]);
101 addmsg("%s", rankname[c.rank]);
105 addmsg("%1.1s", suitchar[c.suit]);
107 addmsg("%s", suitname[c.suit]);
116 printcard(WINDOW *win, int cardno, CARD c, bool blank)
118 prcard(win, cardno * 2, cardno, c, blank);
123 * Print out a card on the window at the specified location
126 prcard(WINDOW *win, int y, int x, CARD c, bool blank)
131 mvwaddstr(win, y + 0, x, "+-----+");
132 mvwaddstr(win, y + 1, x, "| |");
133 mvwaddstr(win, y + 2, x, "| |");
134 mvwaddstr(win, y + 3, x, "| |");
135 mvwaddstr(win, y + 4, x, "+-----+");
137 mvwaddch(win, y + 1, x + 1, rankchar[c.rank][0]);
138 waddch(win, suitchar[c.suit][0]);
139 mvwaddch(win, y + 3, x + 4, rankchar[c.rank][0]);
140 waddch(win, suitchar[c.suit][0]);
146 * Print a hand of n cards
149 prhand(CARD h[], int n, WINDOW *win, bool blank)
154 for (i = 0; i < n; i++)
155 printcard(win, i, *h++, blank);
161 * reads a card, supposedly in hand, accepting unambiguous brief
162 * input, returns the index of the card found...
165 infrom(CARD hand[], int n, const char *prompt)
171 printf("\nINFROM: %d = n < 1!!\n", n);
176 if (incard(&crd)) { /* if card is full card */
177 if (!isone(crd, hand, n))
178 msg("That's not in your hand");
180 for (i = 0; i < n; i++)
181 if (hand[i].rank == crd.rank &&
182 hand[i].suit == crd.suit)
185 printf("\nINFROM: isone or something messed up\n");
190 } else /* if not full card... */
191 if (crd.rank != EMPTY) {
192 for (i = 0; i < n; i++)
193 if (hand[i].rank == crd.rank)
196 msg("No such rank in your hand");
198 for (j = i + 1; j < n; j++)
199 if (hand[j].rank == crd.rank)
202 msg("Ambiguous rank");
207 msg("Sorry, I missed that");
214 * Inputs a card in any format. It reads a line ending with a CR
215 * and then parses it.
227 if (!(line = getline()))
230 while (*p1 != ' ' && *p1 != '\0')
236 /* IMPORTANT: no real card has 2 char first name */
237 if (strlen(p) == 2) { /* check for short form */
239 for (i = 0; i < RANKS; i++) {
240 if (*p == *rankchar[i]) {
246 goto gotit; /* it's nothing... */
247 ++p; /* advance to next char */
249 for (i = 0; i < SUITS; i++) {
250 if (*p == *suitchar[i]) {
260 for (i = 0; i < RANKS; i++) {
261 if (!strcmp(p, rankname[i]) || !strcmp(p, rankchar[i])) {
269 while (*p1 != ' ' && *p1 != '\0')
274 if (!strcmp("OF", p)) {
276 while (*p1 != ' ' && *p1 != '\0')
283 for (i = 0; i < SUITS; i++) {
284 if (!strcmp(p, suitname[i]) || !strcmp(p, suitchar[i])) {
299 * Reads and converts to upper case
315 * Reads in a decimal number and makes sure it is between "lo" and
319 number(int lo, int hi, const char *prompt)
326 if (!(p = getline()) || *p == '\0') {
327 msg(quiet ? "Not a number" :
328 "That doesn't look like a number");
336 while (isdigit(*p)) {
337 sum = 10 * sum + (*p - '0');
341 if (*p != ' ' && *p != '\t' && *p != '\0')
343 if (sum >= lo && sum <= hi)
346 msg("that doesn't look like a number, try again --> ");
348 msg("%d is not between %d and %d inclusive, try again --> ",
356 * Display a message at the top of the screen.
358 char Msgbuf[BUFSIZ] = {'\0'};
360 static int Newpos = 0;
363 msg(const char *fmt, ...)
368 vsprintf(&Msgbuf[Newpos], fmt, ap);
375 * Add things to the current message
378 addmsg(const char *fmt, ...)
383 vsprintf(&Msgbuf[Newpos], fmt, ap);
396 static int lastline = 0;
400 /* All messages should start with uppercase */
401 mvaddch(lastline + Y_MSG_START, SCORE_X, ' ');
402 if (islower(Msgbuf[0]) && Msgbuf[1] != ')')
403 Msgbuf[0] = toupper(Msgbuf[0]);
406 if (len / MSG_X + Lineno >= MSG_Y) {
407 while (Lineno < MSG_Y) {
408 wmove(Msgwin, Lineno++, 0);
413 mvaddch(Lineno + Y_MSG_START, SCORE_X, '*');
416 mvwaddstr(Msgwin, Lineno, 0, mp);
417 if ((len = strlen(mp)) > MSG_X) {
419 for (mp = &mp[MSG_X - 1]; *mp != ' '; mp--)
424 wmove(Msgwin, Lineno, mp - omp);
427 if (++Lineno >= MSG_Y)
429 } while (len > MSG_X);
440 * Wait for the user to type ' ' before doing anything else
445 static char prompt[] = {'-', '-', 'M', 'o', 'r', 'e', '-', '-', '\0'};
447 if (Mpos + (int)sizeof(prompt) < MSG_X)
448 wmove(Msgwin, Lineno > 0 ? Lineno - 1 : MSG_Y - 1, Mpos);
450 mvwaddch(Msgwin, Lineno, 0, ' ');
452 if (++Lineno >= MSG_Y)
455 waddstr(Msgwin, prompt);
462 * Sit around until the guy types the right key
470 while ((c = readchar()) != '\n')
473 while (readchar() != ch)
479 * Reads and returns a character, checking for gross input errors
489 while (read(STDIN_FILENO, &c, sizeof(char)) <= 0)
490 if (cnt++ > 100) { /* if we are getting infinite EOFs */
491 bye(); /* quit the game */
494 if (c == CTRL('L')) {
506 * Reads the next line up to '\n' or EOF. Multiple spaces are
507 * compressed to one space; a space is inserted before a ','
518 getyx(stdscr, oy, ox);
520 /* loop reading in the string, and put it in a temporary buffer */
521 for (sp = linebuf; (c = readchar()) != '\n'; clrtoeol(), refresh()) {
525 if (c == erasechar()) { /* process erase character */
530 for (i = strlen(unctrl(*sp)); i; i--)
535 if (c == killchar()) { /* process kill
541 if (sp == linebuf && c == ' ')
543 if (sp >= &linebuf[LINESIZE - 1] || !(isprint(c) || c == ' '))
559 intr(__unused int signo)
567 * Leave the program, cleaning things up as we go.
572 signal(SIGINT, SIG_IGN);
573 mvcur(0, COLS - 1, LINES - 1, 0);