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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)io.c 8.1 (Berkeley) 5/31/93
30 * $FreeBSD: src/games/cribbage/io.c,v 1.5.2.2 2001/02/18 02:20:31 kris Exp $
31 * $DragonFly: src/games/cribbage/io.c,v 1.4 2005/08/03 13:31:00 eirikn Exp $
51 #define CTRL(X) (X - 'A' + 1)
53 char linebuf
[LINESIZE
];
55 const char *const rankname
[RANKS
] = {
56 "ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
57 "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING"
60 const char *const rankchar
[RANKS
] = {
61 "A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"
64 const char *const suitname
[SUITS
] = {"SPADES", "HEARTS", "DIAMONDS", "CLUBS"};
66 const char *const suitchar
[SUITS
] = {"S", "H", "D", "C"};
68 static bool incard(CARD
*);
69 static bool msgcrd(CARD
, bool, const char *, bool);
70 static void printcard(WINDOW
*, int, CARD
, bool);
71 static int readchar(void);
72 static void wait_for(int);
76 * Call msgcrd in one of two forms
79 msgcard(CARD c
, bool brief
)
82 return (msgcrd(c
, true, NULL
, true));
84 return (msgcrd(c
, false, " of ", false));
89 * Print the value of a card in ascii
92 msgcrd(CARD c
, bool brfrank
, const char *mid
, bool brfsuit
)
94 if (c
.rank
== EMPTY
|| c
.suit
== EMPTY
)
97 addmsg("%1.1s", rankchar
[c
.rank
]);
99 addmsg("%s", rankname
[c
.rank
]);
103 addmsg("%1.1s", suitchar
[c
.suit
]);
105 addmsg("%s", suitname
[c
.suit
]);
114 printcard(WINDOW
*win
, int cardno
, CARD c
, bool blank
)
116 prcard(win
, cardno
* 2, cardno
, c
, blank
);
121 * Print out a card on the window at the specified location
124 prcard(WINDOW
*win
, int y
, int x
, CARD c
, bool blank
)
129 mvwaddstr(win
, y
+ 0, x
, "+-----+");
130 mvwaddstr(win
, y
+ 1, x
, "| |");
131 mvwaddstr(win
, y
+ 2, x
, "| |");
132 mvwaddstr(win
, y
+ 3, x
, "| |");
133 mvwaddstr(win
, y
+ 4, x
, "+-----+");
135 mvwaddch(win
, y
+ 1, x
+ 1, rankchar
[c
.rank
][0]);
136 waddch(win
, suitchar
[c
.suit
][0]);
137 mvwaddch(win
, y
+ 3, x
+ 4, rankchar
[c
.rank
][0]);
138 waddch(win
, suitchar
[c
.suit
][0]);
144 * Print a hand of n cards
147 prhand(CARD h
[], int n
, WINDOW
*win
, bool blank
)
152 for (i
= 0; i
< n
; i
++)
153 printcard(win
, i
, *h
++, blank
);
159 * reads a card, supposedly in hand, accepting unambiguous brief
160 * input, returns the index of the card found...
163 infrom(CARD hand
[], int n
, const char *prompt
)
169 printf("\nINFROM: %d = n < 1!!\n", n
);
174 if (incard(&crd
)) { /* if card is full card */
175 if (!isone(crd
, hand
, n
))
176 msg("That's not in your hand");
178 for (i
= 0; i
< n
; i
++)
179 if (hand
[i
].rank
== crd
.rank
&&
180 hand
[i
].suit
== crd
.suit
)
183 printf("\nINFROM: isone or something messed up\n");
188 } else /* if not full card... */
189 if (crd
.rank
!= EMPTY
) {
190 for (i
= 0; i
< n
; i
++)
191 if (hand
[i
].rank
== crd
.rank
)
194 msg("No such rank in your hand");
196 for (j
= i
+ 1; j
< n
; j
++)
197 if (hand
[j
].rank
== crd
.rank
)
200 msg("Ambiguous rank");
205 msg("Sorry, I missed that");
212 * Inputs a card in any format. It reads a line ending with a CR
213 * and then parses it.
225 if (!(line
= get_line()))
228 while (*p1
!= ' ' && *p1
!= '\0')
234 /* IMPORTANT: no real card has 2 char first name */
235 if (strlen(p
) == 2) { /* check for short form */
237 for (i
= 0; i
< RANKS
; i
++) {
238 if (*p
== *rankchar
[i
]) {
244 goto gotit
; /* it's nothing... */
245 ++p
; /* advance to next char */
247 for (i
= 0; i
< SUITS
; i
++) {
248 if (*p
== *suitchar
[i
]) {
258 for (i
= 0; i
< RANKS
; i
++) {
259 if (!strcmp(p
, rankname
[i
]) || !strcmp(p
, rankchar
[i
])) {
267 while (*p1
!= ' ' && *p1
!= '\0')
272 if (!strcmp("OF", p
)) {
274 while (*p1
!= ' ' && *p1
!= '\0')
281 for (i
= 0; i
< SUITS
; i
++) {
282 if (!strcmp(p
, suitname
[i
]) || !strcmp(p
, suitchar
[i
])) {
297 * Reads and converts to upper case
313 * Reads in a decimal number and makes sure it is between "lo" and
317 number(int lo
, int hi
, const char *prompt
)
324 if (!(p
= get_line()) || *p
== '\0') {
325 msg(quiet
? "Not a number" :
326 "That doesn't look like a number");
334 while (isdigit(*p
)) {
335 sum
= 10 * sum
+ (*p
- '0');
339 if (*p
!= ' ' && *p
!= '\t' && *p
!= '\0')
341 if (sum
>= lo
&& sum
<= hi
)
344 msg("that doesn't look like a number, try again --> ");
346 msg("%d is not between %d and %d inclusive, try again --> ",
354 * Display a message at the top of the screen.
356 char Msgbuf
[BUFSIZ
] = {'\0'};
358 static int Newpos
= 0;
361 msg(const char *fmt
, ...)
366 vsprintf(&Msgbuf
[Newpos
], fmt
, ap
);
373 * Add things to the current message
376 addmsg(const char *fmt
, ...)
381 vsprintf(&Msgbuf
[Newpos
], fmt
, ap
);
394 static int lastline
= 0;
398 /* All messages should start with uppercase */
399 mvaddch(lastline
+ Y_MSG_START
, SCORE_X
, ' ');
400 if (islower(Msgbuf
[0]) && Msgbuf
[1] != ')')
401 Msgbuf
[0] = toupper(Msgbuf
[0]);
404 if (len
/ MSG_X
+ Lineno
>= MSG_Y
) {
405 while (Lineno
< MSG_Y
) {
406 wmove(Msgwin
, Lineno
++, 0);
411 mvaddch(Lineno
+ Y_MSG_START
, SCORE_X
, '*');
414 mvwaddstr(Msgwin
, Lineno
, 0, mp
);
415 if ((len
= strlen(mp
)) > MSG_X
) {
417 for (mp
= &mp
[MSG_X
- 1]; *mp
!= ' '; mp
--)
422 wmove(Msgwin
, Lineno
, mp
- omp
);
425 if (++Lineno
>= MSG_Y
)
427 } while (len
> MSG_X
);
438 * Wait for the user to type ' ' before doing anything else
443 static char prompt
[] = {'-', '-', 'M', 'o', 'r', 'e', '-', '-', '\0'};
445 if (Mpos
+ (int)sizeof(prompt
) < MSG_X
)
446 wmove(Msgwin
, Lineno
> 0 ? Lineno
- 1 : MSG_Y
- 1, Mpos
);
448 mvwaddch(Msgwin
, Lineno
, 0, ' ');
450 if (++Lineno
>= MSG_Y
)
453 waddstr(Msgwin
, prompt
);
460 * Sit around until the guy types the right key
468 while ((c
= readchar()) != '\n')
471 while (readchar() != ch
)
477 * Reads and returns a character, checking for gross input errors
487 while (read(STDIN_FILENO
, &c
, sizeof(char)) <= 0)
488 if (cnt
++ > 100) { /* if we are getting infinite EOFs */
489 bye(); /* quit the game */
492 if (c
== CTRL('L')) {
504 * Reads the next line up to '\n' or EOF. Multiple spaces are
505 * compressed to one space; a space is inserted before a ','
516 getyx(stdscr
, oy
, ox
);
518 /* loop reading in the string, and put it in a temporary buffer */
519 for (sp
= linebuf
; (c
= readchar()) != '\n'; clrtoeol(), refresh()) {
523 if (c
== erasechar()) { /* process erase character */
528 for (i
= strlen(unctrl(*sp
)); i
; i
--)
533 if (c
== killchar()) { /* process kill
539 if (sp
== linebuf
&& c
== ' ')
541 if (sp
>= &linebuf
[LINESIZE
- 1] || !(isprint(c
) || c
== ' '))
557 intr(int signo __unused
)
565 * Leave the program, cleaning things up as we go.
570 signal(SIGINT
, SIG_IGN
);
571 mvcur(0, COLS
- 1, LINES
- 1, 0);