]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/io.c
1 /* $NetBSD: io.c,v 1.7 1995/03/21 15:08:53 cgd 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static char sccsid
[] = "@(#)io.c 8.1 (Berkeley) 5/31/93";
40 static char rcsid
[] = "$NetBSD: io.c,v 1.7 1995/03/21 15:08:53 cgd Exp $";
67 #define CTRL(X) (X - 'A' + 1)
69 char linebuf
[LINESIZE
];
71 char *rankname
[RANKS
] = {
72 "ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
73 "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING"
76 char *rankchar
[RANKS
] = {
77 "A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"
80 char *suitname
[SUITS
] = {"SPADES", "HEARTS", "DIAMONDS", "CLUBS"};
82 char *suitchar
[SUITS
] = {"S", "H", "D", "C"};
86 * Call msgcrd in one of two forms
94 return (msgcrd(c
, TRUE
, NULL
, TRUE
));
96 return (msgcrd(c
, FALSE
, " of ", FALSE
));
101 * Print the value of a card in ascii
104 msgcrd(c
, brfrank
, mid
, brfsuit
)
106 BOOLEAN brfrank
, brfsuit
;
109 if (c
.rank
== EMPTY
|| c
.suit
== EMPTY
)
112 addmsg("%1.1s", rankchar
[c
.rank
]);
114 addmsg(rankname
[c
.rank
]);
118 addmsg("%1.1s", suitchar
[c
.suit
]);
120 addmsg(suitname
[c
.suit
]);
129 printcard(win
, cardno
, c
, blank
)
135 prcard(win
, cardno
* 2, cardno
, c
, blank
);
140 * Print out a card on the window at the specified location
143 prcard(win
, y
, x
, c
, blank
)
152 mvwaddstr(win
, y
+ 0, x
, "+-----+");
153 mvwaddstr(win
, y
+ 1, x
, "| |");
154 mvwaddstr(win
, y
+ 2, x
, "| |");
155 mvwaddstr(win
, y
+ 3, x
, "| |");
156 mvwaddstr(win
, y
+ 4, x
, "+-----+");
158 mvwaddch(win
, y
+ 1, x
+ 1, rankchar
[c
.rank
][0]);
159 waddch(win
, suitchar
[c
.suit
][0]);
160 mvwaddch(win
, y
+ 3, x
+ 4, rankchar
[c
.rank
][0]);
161 waddch(win
, suitchar
[c
.suit
][0]);
167 * Print a hand of n cards
170 prhand(h
, n
, win
, blank
)
179 for (i
= 0; i
< n
; i
++)
180 printcard(win
, i
, *h
++, blank
);
186 * reads a card, supposedly in hand, accepting unambigous brief
187 * input, returns the index of the card found...
190 infrom(hand
, n
, prompt
)
199 printf("\nINFROM: %d = n < 1!!\n", n
);
204 if (incard(&crd
)) { /* if card is full card */
205 if (!isone(crd
, hand
, n
))
206 msg("That's not in your hand");
208 for (i
= 0; i
< n
; i
++)
209 if (hand
[i
].rank
== crd
.rank
&&
210 hand
[i
].suit
== crd
.suit
)
213 printf("\nINFROM: isone or something messed up\n");
218 } else /* if not full card... */
219 if (crd
.rank
!= EMPTY
) {
220 for (i
= 0; i
< n
; i
++)
221 if (hand
[i
].rank
== crd
.rank
)
224 msg("No such rank in your hand");
226 for (j
= i
+ 1; j
< n
; j
++)
227 if (hand
[j
].rank
== crd
.rank
)
230 msg("Ambiguous rank");
235 msg("Sorry, I missed that");
242 * Inputs a card in any format. It reads a line ending with a CR
243 * and then parses it.
256 if (!(line
= getline()))
259 while (*p1
!= ' ' && *p1
!= NULL
)
265 /* IMPORTANT: no real card has 2 char first name */
266 if (strlen(p
) == 2) { /* check for short form */
268 for (i
= 0; i
< RANKS
; i
++) {
269 if (*p
== *rankchar
[i
]) {
275 goto gotit
; /* it's nothing... */
276 ++p
; /* advance to next char */
278 for (i
= 0; i
< SUITS
; i
++) {
279 if (*p
== *suitchar
[i
]) {
289 for (i
= 0; i
< RANKS
; i
++) {
290 if (!strcmp(p
, rankname
[i
]) || !strcmp(p
, rankchar
[i
])) {
298 while (*p1
!= ' ' && *p1
!= NULL
)
303 if (!strcmp("OF", p
)) {
305 while (*p1
!= ' ' && *p1
!= NULL
)
312 for (i
= 0; i
< SUITS
; i
++) {
313 if (!strcmp(p
, suitname
[i
]) || !strcmp(p
, suitchar
[i
])) {
328 * Reads and converts to upper case
344 * Reads in a decimal number and makes sure it is between "lo" and
348 number(lo
, hi
, prompt
)
357 if (!(p
= getline()) || *p
== NULL
) {
358 msg(quiet
? "Not a number" :
359 "That doesn't look like a number");
367 while (isdigit(*p
)) {
368 sum
= 10 * sum
+ (*p
- '0');
372 if (*p
!= ' ' && *p
!= '\t' && *p
!= NULL
)
374 if (sum
>= lo
&& sum
<= hi
)
377 msg("that doesn't look like a number, try again --> ");
379 msg("%d is not between %d and %d inclusive, try again --> ",
387 * Display a message at the top of the screen.
389 char Msgbuf
[BUFSIZ
] = {'\0'};
391 static int Newpos
= 0;
395 msg(const char *fmt
, ...)
409 (void)vsprintf(&Msgbuf
[Newpos
], fmt
, ap
);
416 * Add things to the current message
420 addmsg(const char *fmt
, ...)
422 addmsg(fmt
, va_alist
)
434 (void)vsprintf(&Msgbuf
[Newpos
], fmt
, ap
);
447 static int lastline
= 0;
449 register char *mp
, *omp
;
451 /* All messages should start with uppercase */
452 mvaddch(lastline
+ Y_MSG_START
, SCORE_X
, ' ');
453 if (islower(Msgbuf
[0]) && Msgbuf
[1] != ')')
454 Msgbuf
[0] = toupper(Msgbuf
[0]);
457 if (len
/ MSG_X
+ Lineno
>= MSG_Y
) {
458 while (Lineno
< MSG_Y
) {
459 wmove(Msgwin
, Lineno
++, 0);
464 mvaddch(Lineno
+ Y_MSG_START
, SCORE_X
, '*');
467 mvwaddstr(Msgwin
, Lineno
, 0, mp
);
468 if ((len
= strlen(mp
)) > MSG_X
) {
470 for (mp
= &mp
[MSG_X
- 1]; *mp
!= ' '; mp
--)
475 wmove(Msgwin
, Lineno
, mp
- omp
);
478 if (++Lineno
>= MSG_Y
)
480 } while (len
> MSG_X
);
491 * Wait for the user to type ' ' before doing anything else
496 static char prompt
[] = {'-', '-', 'M', 'o', 'r', 'e', '-', '-', '\0'};
498 if (Mpos
+ sizeof prompt
< MSG_X
)
499 wmove(Msgwin
, Lineno
> 0 ? Lineno
- 1 : MSG_Y
- 1, Mpos
);
501 mvwaddch(Msgwin
, Lineno
, 0, ' ');
503 if (++Lineno
>= MSG_Y
)
506 waddstr(Msgwin
, prompt
);
513 * Sit around until the guy types the right key
522 while ((c
= readchar()) != '\n')
525 while (readchar() != ch
)
531 * Reads and returns a character, checking for gross input errors
541 while (read(STDIN_FILENO
, &c
, sizeof(char)) <= 0)
542 if (cnt
++ > 100) { /* if we are getting infinite EOFs */
543 bye(); /* quit the game */
546 if (c
== CTRL('L')) {
558 * Reads the next line up to '\n' or EOF. Multiple spaces are
559 * compressed to one space; a space is inserted before a ','
565 register int c
, oy
, ox
;
566 register WINDOW
*oscr
;
570 getyx(stdscr
, oy
, ox
);
572 /* loop reading in the string, and put it in a temporary buffer */
573 for (sp
= linebuf
; (c
= readchar()) != '\n'; clrtoeol(), refresh()) {
577 if (c
== erasechar()) { /* process erase character */
582 for (i
= strlen(unctrl(*sp
)); i
; i
--)
587 if (c
== killchar()) { /* process kill
593 if (sp
== linebuf
&& c
== ' ')
595 if (sp
>= &linebuf
[LINESIZE
- 1] || !(isprint(c
) || c
== ' '))
620 * Leave the program, cleaning things up as we go.
625 signal(SIGINT
, SIG_IGN
);
626 mvcur(0, COLS
- 1, LINES
- 1, 0);