]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/io.c
1 /* $NetBSD: io.c,v 1.20 2005/07/02 08:32:32 jmc Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)io.c 8.1 (Berkeley) 5/31/93";
37 __RCSID("$NetBSD: io.c,v 1.20 2005/07/02 08:32:32 jmc Exp $");
59 #define CTRL(X) (X - 'A' + 1)
61 char linebuf
[LINESIZE
];
63 const char *const rankname
[RANKS
] = {
64 "ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
65 "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING"
68 const char *const rankchar
[RANKS
] = {
69 "A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"
72 const char *const suitname
[SUITS
] = {"SPADES", "HEARTS", "DIAMONDS", "CLUBS"};
74 const char *const suitchar
[SUITS
] = {"S", "H", "D", "C"};
78 * Call msgcrd in one of two forms
86 return (msgcrd(c
, TRUE
, NULL
, TRUE
));
88 return (msgcrd(c
, FALSE
, " of ", FALSE
));
93 * Print the value of a card in ascii
96 msgcrd(CARD c
, BOOLEAN brfrank
, const char *mid
, BOOLEAN brfsuit
)
98 if (c
.rank
== EMPTY
|| c
.suit
== EMPTY
)
101 addmsg("%1.1s", rankchar
[c
.rank
]);
103 addmsg(rankname
[c
.rank
]);
107 addmsg("%1.1s", suitchar
[c
.suit
]);
109 addmsg(suitname
[c
.suit
]);
118 printcard(WINDOW
*win
, int cardno
, CARD c
, BOOLEAN blank
)
120 prcard(win
, cardno
* 2, cardno
, c
, blank
);
125 * Print out a card on the window at the specified location
128 prcard(WINDOW
*win
, int y
, int x
, CARD c
, BOOLEAN blank
)
133 mvwaddstr(win
, y
+ 0, x
, "+-----+");
134 mvwaddstr(win
, y
+ 1, x
, "| |");
135 mvwaddstr(win
, y
+ 2, x
, "| |");
136 mvwaddstr(win
, y
+ 3, x
, "| |");
137 mvwaddstr(win
, y
+ 4, x
, "+-----+");
139 mvwaddch(win
, y
+ 1, x
+ 1, rankchar
[c
.rank
][0]);
140 waddch(win
, suitchar
[c
.suit
][0]);
141 mvwaddch(win
, y
+ 3, x
+ 4, rankchar
[c
.rank
][0]);
142 waddch(win
, suitchar
[c
.suit
][0]);
148 * Print a hand of n cards
151 prhand(const CARD h
[], int n
, WINDOW
*win
, BOOLEAN blank
)
156 for (i
= 0; i
< n
; i
++)
157 printcard(win
, i
, *h
++, blank
);
163 * reads a card, supposedly in hand, accepting unambigous brief
164 * input, returns the index of the card found...
167 infrom(const CARD hand
[], int n
, const char *prompt
)
173 printf("\nINFROM: %d = n < 1!!\n", n
);
178 if (incard(&crd
)) { /* if card is full card */
179 if (!is_one(crd
, hand
, n
))
180 msg("That's not in your hand");
182 for (i
= 0; i
< n
; i
++)
183 if (hand
[i
].rank
== crd
.rank
&&
184 hand
[i
].suit
== crd
.suit
)
187 printf("\nINFROM: is_one or something messed up\n");
192 } else /* if not full card... */
193 if (crd
.rank
!= EMPTY
) {
194 for (i
= 0; i
< n
; i
++)
195 if (hand
[i
].rank
== crd
.rank
)
198 msg("No such rank in your hand");
200 for (j
= i
+ 1; j
< n
; j
++)
201 if (hand
[j
].rank
== crd
.rank
)
204 msg("Ambiguous rank");
209 msg("Sorry, I missed that");
216 * Inputs a card in any format. It reads a line ending with a CR
217 * and then parses it.
229 if (!(line
= getline()))
232 while (*p1
!= ' ' && *p1
!= '\0')
238 /* IMPORTANT: no real card has 2 char first name */
239 if (strlen(p
) == 2) { /* check for short form */
241 for (i
= 0; i
< RANKS
; i
++) {
242 if (*p
== *rankchar
[i
]) {
248 goto gotit
; /* it's nothing... */
249 ++p
; /* advance to next char */
251 for (i
= 0; i
< SUITS
; i
++) {
252 if (*p
== *suitchar
[i
]) {
262 for (i
= 0; i
< RANKS
; i
++) {
263 if (!strcmp(p
, rankname
[i
]) || !strcmp(p
, rankchar
[i
])) {
271 while (*p1
!= ' ' && *p1
!= '\0')
276 if (!strcmp("OF", p
)) {
278 while (*p1
!= ' ' && *p1
!= '\0')
285 for (i
= 0; i
< SUITS
; i
++) {
286 if (!strcmp(p
, suitname
[i
]) || !strcmp(p
, suitchar
[i
])) {
301 * Reads and converts to upper case
317 * Reads in a decimal number and makes sure it is between "lo" and
321 number(int lo
, int hi
, const char *prompt
)
328 if (!(p
= getline()) || *p
== '\0') {
329 msg(quiet
? "Not a number" :
330 "That doesn't look like a number");
335 if (!isdigit((unsigned char)*p
))
338 while (isdigit((unsigned char)*p
)) {
339 sum
= 10 * sum
+ (*p
- '0');
343 if (*p
!= ' ' && *p
!= '\t' && *p
!= '\0')
345 if (sum
>= lo
&& sum
<= hi
)
348 msg("that doesn't look like a number, try again --> ");
350 msg("%d is not between %d and %d inclusive, try again --> ",
358 * Display a message at the top of the screen.
360 char Msgbuf
[BUFSIZ
] = {'\0'};
362 static int Newpos
= 0;
365 msg(const char *fmt
, ...)
370 (void)vsprintf(&Msgbuf
[Newpos
], fmt
, ap
);
371 Newpos
= strlen(Msgbuf
);
378 * Add things to the current message
381 addmsg(const char *fmt
, ...)
386 (void)vsprintf(&Msgbuf
[Newpos
], fmt
, ap
);
387 Newpos
= strlen(Msgbuf
);
400 static int lastline
= 0;
404 /* All messages should start with uppercase */
405 mvaddch(lastline
+ Y_MSG_START
, SCORE_X
, ' ');
406 if (islower((unsigned char)Msgbuf
[0]) && Msgbuf
[1] != ')')
407 Msgbuf
[0] = toupper((unsigned char)Msgbuf
[0]);
410 if (len
/ MSG_X
+ Lineno
>= MSG_Y
) {
411 while (Lineno
< MSG_Y
) {
412 wmove(Msgwin
, Lineno
++, 0);
417 mvaddch(Lineno
+ Y_MSG_START
, SCORE_X
, '*');
420 mvwaddstr(Msgwin
, Lineno
, 0, mp
);
421 if ((len
= strlen(mp
)) > MSG_X
) {
423 for (mp
= &mp
[MSG_X
- 1]; *mp
!= ' '; mp
--)
428 wmove(Msgwin
, Lineno
, mp
- omp
);
431 if (++Lineno
>= MSG_Y
)
433 } while (len
> MSG_X
);
444 * Wait for the user to type ' ' before doing anything else
449 static const char prompt
[] = {'-', '-', 'M', 'o', 'r', 'e', '-', '-', '\0'};
451 if ((int)(Mpos
+ sizeof prompt
) < MSG_X
)
452 wmove(Msgwin
, Lineno
> 0 ? Lineno
- 1 : MSG_Y
- 1, Mpos
);
454 mvwaddch(Msgwin
, Lineno
, 0, ' ');
456 if (++Lineno
>= MSG_Y
)
459 waddstr(Msgwin
, prompt
);
466 * Sit around until the guy types the right key
474 while ((c
= readchar()) != '\n')
477 while (readchar() != ch
)
483 * Reads and returns a character, checking for gross input errors
493 while (read(STDIN_FILENO
, &c
, sizeof(unsigned char)) <= 0)
494 if (cnt
++ > 100) { /* if we are getting infinite EOFs */
495 bye(); /* quit the game */
498 if (c
== CTRL('L')) {
510 * Reads the next line up to '\n' or EOF. Multiple spaces are
511 * compressed to one space; a space is inserted before a ','
522 getyx(stdscr
, oy
, ox
);
524 /* loop reading in the string, and put it in a temporary buffer */
525 for (sp
= linebuf
; (c
= readchar()) != '\n'; clrtoeol(), refresh()) {
526 if (c
== erasechar()) { /* process erase character */
531 for (i
= strlen(unctrl(*sp
)); i
; i
--)
536 if (c
== killchar()) { /* process kill
542 if (sp
== linebuf
&& c
== ' ')
544 if (sp
>= &linebuf
[LINESIZE
- 1] || !(isprint(c
) || c
== ' '))
560 receive_intr(int signo
__attribute__((__unused__
)))
568 * Leave the program, cleaning things up as we go.
573 signal(SIGINT
, SIG_IGN
);
574 mvcur(0, COLS
- 1, LINES
- 1, 0);