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.2 2003/06/17 04:25:23 dillon Exp $
61 #define CTRL(X) (X - 'A' + 1)
63 char linebuf[LINESIZE];
65 char *rankname[RANKS] = {
66 "ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
67 "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING"
70 char *rankchar[RANKS] = {
71 "A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"
74 char *suitname[SUITS] = {"SPADES", "HEARTS", "DIAMONDS", "CLUBS"};
76 char *suitchar[SUITS] = {"S", "H", "D", "C"};
80 * Call msgcrd in one of two forms
88 return (msgcrd(c, TRUE, NULL, TRUE));
90 return (msgcrd(c, FALSE, " of ", FALSE));
95 * Print the value of a card in ascii
98 msgcrd(c, brfrank, mid, brfsuit)
100 BOOLEAN brfrank, brfsuit;
103 if (c.rank == EMPTY || c.suit == EMPTY)
106 addmsg("%1.1s", rankchar[c.rank]);
108 addmsg("%s", rankname[c.rank]);
112 addmsg("%1.1s", suitchar[c.suit]);
114 addmsg("%s", suitname[c.suit]);
123 printcard(win, cardno, c, blank)
129 prcard(win, cardno * 2, cardno, c, blank);
134 * Print out a card on the window at the specified location
137 prcard(win, y, x, c, blank)
146 mvwaddstr(win, y + 0, x, "+-----+");
147 mvwaddstr(win, y + 1, x, "| |");
148 mvwaddstr(win, y + 2, x, "| |");
149 mvwaddstr(win, y + 3, x, "| |");
150 mvwaddstr(win, y + 4, x, "+-----+");
152 mvwaddch(win, y + 1, x + 1, rankchar[c.rank][0]);
153 waddch(win, suitchar[c.suit][0]);
154 mvwaddch(win, y + 3, x + 4, rankchar[c.rank][0]);
155 waddch(win, suitchar[c.suit][0]);
161 * Print a hand of n cards
164 prhand(h, n, win, blank)
173 for (i = 0; i < n; i++)
174 printcard(win, i, *h++, blank);
180 * reads a card, supposedly in hand, accepting unambigous brief
181 * input, returns the index of the card found...
184 infrom(hand, n, prompt)
193 printf("\nINFROM: %d = n < 1!!\n", n);
198 if (incard(&crd)) { /* if card is full card */
199 if (!isone(crd, hand, n))
200 msg("That's not in your hand");
202 for (i = 0; i < n; i++)
203 if (hand[i].rank == crd.rank &&
204 hand[i].suit == crd.suit)
207 printf("\nINFROM: isone or something messed up\n");
212 } else /* if not full card... */
213 if (crd.rank != EMPTY) {
214 for (i = 0; i < n; i++)
215 if (hand[i].rank == crd.rank)
218 msg("No such rank in your hand");
220 for (j = i + 1; j < n; j++)
221 if (hand[j].rank == crd.rank)
224 msg("Ambiguous rank");
229 msg("Sorry, I missed that");
236 * Inputs a card in any format. It reads a line ending with a CR
237 * and then parses it.
250 if (!(line = getline()))
253 while (*p1 != ' ' && *p1 != '\0')
259 /* IMPORTANT: no real card has 2 char first name */
260 if (strlen(p) == 2) { /* check for short form */
262 for (i = 0; i < RANKS; i++) {
263 if (*p == *rankchar[i]) {
269 goto gotit; /* it's nothing... */
270 ++p; /* advance to next char */
272 for (i = 0; i < SUITS; i++) {
273 if (*p == *suitchar[i]) {
283 for (i = 0; i < RANKS; i++) {
284 if (!strcmp(p, rankname[i]) || !strcmp(p, rankchar[i])) {
292 while (*p1 != ' ' && *p1 != '\0')
297 if (!strcmp("OF", p)) {
299 while (*p1 != ' ' && *p1 != '\0')
306 for (i = 0; i < SUITS; i++) {
307 if (!strcmp(p, suitname[i]) || !strcmp(p, suitchar[i])) {
322 * Reads and converts to upper case
338 * Reads in a decimal number and makes sure it is between "lo" and
342 number(lo, hi, prompt)
351 if (!(p = getline()) || *p == '\0') {
352 msg(quiet ? "Not a number" :
353 "That doesn't look like a number");
361 while (isdigit(*p)) {
362 sum = 10 * sum + (*p - '0');
366 if (*p != ' ' && *p != '\t' && *p != '\0')
368 if (sum >= lo && sum <= hi)
371 msg("that doesn't look like a number, try again --> ");
373 msg("%d is not between %d and %d inclusive, try again --> ",
381 * Display a message at the top of the screen.
383 char Msgbuf[BUFSIZ] = {'\0'};
385 static int Newpos = 0;
389 msg(const char *fmt, ...)
403 (void)vsprintf(&Msgbuf[Newpos], fmt, ap);
410 * Add things to the current message
414 addmsg(const char *fmt, ...)
416 addmsg(fmt, va_alist)
428 (void)vsprintf(&Msgbuf[Newpos], fmt, ap);
441 static int lastline = 0;
445 /* All messages should start with uppercase */
446 mvaddch(lastline + Y_MSG_START, SCORE_X, ' ');
447 if (islower(Msgbuf[0]) && Msgbuf[1] != ')')
448 Msgbuf[0] = toupper(Msgbuf[0]);
451 if (len / MSG_X + Lineno >= MSG_Y) {
452 while (Lineno < MSG_Y) {
453 wmove(Msgwin, Lineno++, 0);
458 mvaddch(Lineno + Y_MSG_START, SCORE_X, '*');
461 mvwaddstr(Msgwin, Lineno, 0, mp);
462 if ((len = strlen(mp)) > MSG_X) {
464 for (mp = &mp[MSG_X - 1]; *mp != ' '; mp--)
469 wmove(Msgwin, Lineno, mp - omp);
472 if (++Lineno >= MSG_Y)
474 } while (len > MSG_X);
485 * Wait for the user to type ' ' before doing anything else
490 static char prompt[] = {'-', '-', 'M', 'o', 'r', 'e', '-', '-', '\0'};
492 if (Mpos + sizeof prompt < MSG_X)
493 wmove(Msgwin, Lineno > 0 ? Lineno - 1 : MSG_Y - 1, Mpos);
495 mvwaddch(Msgwin, Lineno, 0, ' ');
497 if (++Lineno >= MSG_Y)
500 waddstr(Msgwin, prompt);
507 * Sit around until the guy types the right key
516 while ((c = readchar()) != '\n')
519 while (readchar() != ch)
525 * Reads and returns a character, checking for gross input errors
535 while (read(STDIN_FILENO, &c, sizeof(char)) <= 0)
536 if (cnt++ > 100) { /* if we are getting infinite EOFs */
537 bye(); /* quit the game */
540 if (c == CTRL('L')) {
552 * Reads the next line up to '\n' or EOF. Multiple spaces are
553 * compressed to one space; a space is inserted before a ','
564 getyx(stdscr, oy, ox);
566 /* loop reading in the string, and put it in a temporary buffer */
567 for (sp = linebuf; (c = readchar()) != '\n'; clrtoeol(), refresh()) {
571 if (c == erasechar()) { /* process erase character */
576 for (i = strlen(unctrl(*sp)); i; i--)
581 if (c == killchar()) { /* process kill
587 if (sp == linebuf && c == ' ')
589 if (sp >= &linebuf[LINESIZE - 1] || !(isprint(c) || c == ' '))
614 * Leave the program, cleaning things up as we go.
619 signal(SIGINT, SIG_IGN);
620 mvcur(0, COLS - 1, LINES - 1, 0);