]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/io.c
1 /* $NetBSD: io.c,v 1.17 2004/01/26 09:58:35 jsm 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.17 2004/01/26 09:58:35 jsm 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(c
, brfrank
, mid
, brfsuit
)
98 BOOLEAN brfrank
, brfsuit
;
101 if (c
.rank
== EMPTY
|| c
.suit
== EMPTY
)
104 addmsg("%1.1s", rankchar
[c
.rank
]);
106 addmsg(rankname
[c
.rank
]);
110 addmsg("%1.1s", suitchar
[c
.suit
]);
112 addmsg(suitname
[c
.suit
]);
121 printcard(win
, cardno
, c
, blank
)
127 prcard(win
, cardno
* 2, cardno
, c
, blank
);
132 * Print out a card on the window at the specified location
135 prcard(win
, y
, x
, c
, blank
)
144 mvwaddstr(win
, y
+ 0, x
, "+-----+");
145 mvwaddstr(win
, y
+ 1, x
, "| |");
146 mvwaddstr(win
, y
+ 2, x
, "| |");
147 mvwaddstr(win
, y
+ 3, x
, "| |");
148 mvwaddstr(win
, y
+ 4, x
, "+-----+");
150 mvwaddch(win
, y
+ 1, x
+ 1, rankchar
[c
.rank
][0]);
151 waddch(win
, suitchar
[c
.suit
][0]);
152 mvwaddch(win
, y
+ 3, x
+ 4, rankchar
[c
.rank
][0]);
153 waddch(win
, suitchar
[c
.suit
][0]);
159 * Print a hand of n cards
162 prhand(h
, n
, win
, blank
)
171 for (i
= 0; i
< n
; i
++)
172 printcard(win
, i
, *h
++, blank
);
178 * reads a card, supposedly in hand, accepting unambigous brief
179 * input, returns the index of the card found...
182 infrom(hand
, n
, prompt
)
191 printf("\nINFROM: %d = n < 1!!\n", n
);
196 if (incard(&crd
)) { /* if card is full card */
197 if (!is_one(crd
, hand
, n
))
198 msg("That's not in your hand");
200 for (i
= 0; i
< n
; i
++)
201 if (hand
[i
].rank
== crd
.rank
&&
202 hand
[i
].suit
== crd
.suit
)
205 printf("\nINFROM: is_one or something messed up\n");
210 } else /* if not full card... */
211 if (crd
.rank
!= EMPTY
) {
212 for (i
= 0; i
< n
; i
++)
213 if (hand
[i
].rank
== crd
.rank
)
216 msg("No such rank in your hand");
218 for (j
= i
+ 1; j
< n
; j
++)
219 if (hand
[j
].rank
== crd
.rank
)
222 msg("Ambiguous rank");
227 msg("Sorry, I missed that");
234 * Inputs a card in any format. It reads a line ending with a CR
235 * and then parses it.
248 if (!(line
= getline()))
251 while (*p1
!= ' ' && *p1
!= '\0')
257 /* IMPORTANT: no real card has 2 char first name */
258 if (strlen(p
) == 2) { /* check for short form */
260 for (i
= 0; i
< RANKS
; i
++) {
261 if (*p
== *rankchar
[i
]) {
267 goto gotit
; /* it's nothing... */
268 ++p
; /* advance to next char */
270 for (i
= 0; i
< SUITS
; i
++) {
271 if (*p
== *suitchar
[i
]) {
281 for (i
= 0; i
< RANKS
; i
++) {
282 if (!strcmp(p
, rankname
[i
]) || !strcmp(p
, rankchar
[i
])) {
290 while (*p1
!= ' ' && *p1
!= '\0')
295 if (!strcmp("OF", p
)) {
297 while (*p1
!= ' ' && *p1
!= '\0')
304 for (i
= 0; i
< SUITS
; i
++) {
305 if (!strcmp(p
, suitname
[i
]) || !strcmp(p
, suitchar
[i
])) {
320 * Reads and converts to upper case
336 * Reads in a decimal number and makes sure it is between "lo" and
340 number(lo
, hi
, prompt
)
349 if (!(p
= getline()) || *p
== '\0') {
350 msg(quiet
? "Not a number" :
351 "That doesn't look like a number");
359 while (isdigit(*p
)) {
360 sum
= 10 * sum
+ (*p
- '0');
364 if (*p
!= ' ' && *p
!= '\t' && *p
!= '\0')
366 if (sum
>= lo
&& sum
<= hi
)
369 msg("that doesn't look like a number, try again --> ");
371 msg("%d is not between %d and %d inclusive, try again --> ",
379 * Display a message at the top of the screen.
381 char Msgbuf
[BUFSIZ
] = {'\0'};
383 static int Newpos
= 0;
386 msg(const char *fmt
, ...)
391 (void)vsprintf(&Msgbuf
[Newpos
], fmt
, ap
);
392 Newpos
= strlen(Msgbuf
);
399 * Add things to the current message
402 addmsg(const char *fmt
, ...)
407 (void)vsprintf(&Msgbuf
[Newpos
], fmt
, ap
);
408 Newpos
= strlen(Msgbuf
);
421 static int lastline
= 0;
425 /* All messages should start with uppercase */
426 mvaddch(lastline
+ Y_MSG_START
, SCORE_X
, ' ');
427 if (islower(Msgbuf
[0]) && Msgbuf
[1] != ')')
428 Msgbuf
[0] = toupper(Msgbuf
[0]);
431 if (len
/ MSG_X
+ Lineno
>= MSG_Y
) {
432 while (Lineno
< MSG_Y
) {
433 wmove(Msgwin
, Lineno
++, 0);
438 mvaddch(Lineno
+ Y_MSG_START
, SCORE_X
, '*');
441 mvwaddstr(Msgwin
, Lineno
, 0, mp
);
442 if ((len
= strlen(mp
)) > MSG_X
) {
444 for (mp
= &mp
[MSG_X
- 1]; *mp
!= ' '; mp
--)
449 wmove(Msgwin
, Lineno
, mp
- omp
);
452 if (++Lineno
>= MSG_Y
)
454 } while (len
> MSG_X
);
465 * Wait for the user to type ' ' before doing anything else
470 static const char prompt
[] = {'-', '-', 'M', 'o', 'r', 'e', '-', '-', '\0'};
472 if ((int)(Mpos
+ sizeof prompt
) < MSG_X
)
473 wmove(Msgwin
, Lineno
> 0 ? Lineno
- 1 : MSG_Y
- 1, Mpos
);
475 mvwaddch(Msgwin
, Lineno
, 0, ' ');
477 if (++Lineno
>= MSG_Y
)
480 waddstr(Msgwin
, prompt
);
487 * Sit around until the guy types the right key
496 while ((c
= readchar()) != '\n')
499 while (readchar() != ch
)
505 * Reads and returns a character, checking for gross input errors
515 while (read(STDIN_FILENO
, &c
, sizeof(char)) <= 0)
516 if (cnt
++ > 100) { /* if we are getting infinite EOFs */
517 bye(); /* quit the game */
520 if (c
== CTRL('L')) {
532 * Reads the next line up to '\n' or EOF. Multiple spaces are
533 * compressed to one space; a space is inserted before a ','
544 getyx(stdscr
, oy
, ox
);
546 /* loop reading in the string, and put it in a temporary buffer */
547 for (sp
= linebuf
; (c
= readchar()) != '\n'; clrtoeol(), refresh()) {
551 if (c
== erasechar()) { /* process erase character */
556 for (i
= strlen(unctrl(*sp
)); i
; i
--)
561 if (c
== killchar()) { /* process kill
567 if (sp
== linebuf
&& c
== ' ')
569 if (sp
>= &linebuf
[LINESIZE
- 1] || !(isprint(c
) || c
== ' '))
586 int signo
__attribute__((__unused__
));
594 * Leave the program, cleaning things up as we go.
599 signal(SIGINT
, SIG_IGN
);
600 mvcur(0, COLS
- 1, LINES
- 1, 0);