]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/io.c
1 /* $NetBSD: io.c,v 1.14 1999/09/30 18:01:32 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. 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
36 #include <sys/cdefs.h>
39 static char sccsid
[] = "@(#)io.c 8.1 (Berkeley) 5/31/93";
41 __RCSID("$NetBSD: io.c,v 1.14 1999/09/30 18:01:32 jsm Exp $");
68 #define CTRL(X) (X - 'A' + 1)
70 char linebuf
[LINESIZE
];
72 const char *const rankname
[RANKS
] = {
73 "ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
74 "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING"
77 const char *const rankchar
[RANKS
] = {
78 "A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"
81 const char *const suitname
[SUITS
] = {"SPADES", "HEARTS", "DIAMONDS", "CLUBS"};
83 const char *const suitchar
[SUITS
] = {"S", "H", "D", "C"};
87 * Call msgcrd in one of two forms
95 return (msgcrd(c
, TRUE
, NULL
, TRUE
));
97 return (msgcrd(c
, FALSE
, " of ", FALSE
));
102 * Print the value of a card in ascii
105 msgcrd(c
, brfrank
, mid
, brfsuit
)
107 BOOLEAN brfrank
, brfsuit
;
110 if (c
.rank
== EMPTY
|| c
.suit
== EMPTY
)
113 addmsg("%1.1s", rankchar
[c
.rank
]);
115 addmsg(rankname
[c
.rank
]);
119 addmsg("%1.1s", suitchar
[c
.suit
]);
121 addmsg(suitname
[c
.suit
]);
130 printcard(win
, cardno
, c
, blank
)
136 prcard(win
, cardno
* 2, cardno
, c
, blank
);
141 * Print out a card on the window at the specified location
144 prcard(win
, y
, x
, c
, blank
)
153 mvwaddstr(win
, y
+ 0, x
, "+-----+");
154 mvwaddstr(win
, y
+ 1, x
, "| |");
155 mvwaddstr(win
, y
+ 2, x
, "| |");
156 mvwaddstr(win
, y
+ 3, x
, "| |");
157 mvwaddstr(win
, y
+ 4, x
, "+-----+");
159 mvwaddch(win
, y
+ 1, x
+ 1, rankchar
[c
.rank
][0]);
160 waddch(win
, suitchar
[c
.suit
][0]);
161 mvwaddch(win
, y
+ 3, x
+ 4, rankchar
[c
.rank
][0]);
162 waddch(win
, suitchar
[c
.suit
][0]);
168 * Print a hand of n cards
171 prhand(h
, n
, win
, blank
)
180 for (i
= 0; i
< n
; i
++)
181 printcard(win
, i
, *h
++, blank
);
187 * reads a card, supposedly in hand, accepting unambigous brief
188 * input, returns the index of the card found...
191 infrom(hand
, n
, prompt
)
200 printf("\nINFROM: %d = n < 1!!\n", n
);
205 if (incard(&crd
)) { /* if card is full card */
206 if (!is_one(crd
, hand
, n
))
207 msg("That's not in your hand");
209 for (i
= 0; i
< n
; i
++)
210 if (hand
[i
].rank
== crd
.rank
&&
211 hand
[i
].suit
== crd
.suit
)
214 printf("\nINFROM: is_one or something messed up\n");
219 } else /* if not full card... */
220 if (crd
.rank
!= EMPTY
) {
221 for (i
= 0; i
< n
; i
++)
222 if (hand
[i
].rank
== crd
.rank
)
225 msg("No such rank in your hand");
227 for (j
= i
+ 1; j
< n
; j
++)
228 if (hand
[j
].rank
== crd
.rank
)
231 msg("Ambiguous rank");
236 msg("Sorry, I missed that");
243 * Inputs a card in any format. It reads a line ending with a CR
244 * and then parses it.
257 if (!(line
= getline()))
260 while (*p1
!= ' ' && *p1
!= '\0')
266 /* IMPORTANT: no real card has 2 char first name */
267 if (strlen(p
) == 2) { /* check for short form */
269 for (i
= 0; i
< RANKS
; i
++) {
270 if (*p
== *rankchar
[i
]) {
276 goto gotit
; /* it's nothing... */
277 ++p
; /* advance to next char */
279 for (i
= 0; i
< SUITS
; i
++) {
280 if (*p
== *suitchar
[i
]) {
290 for (i
= 0; i
< RANKS
; i
++) {
291 if (!strcmp(p
, rankname
[i
]) || !strcmp(p
, rankchar
[i
])) {
299 while (*p1
!= ' ' && *p1
!= '\0')
304 if (!strcmp("OF", p
)) {
306 while (*p1
!= ' ' && *p1
!= '\0')
313 for (i
= 0; i
< SUITS
; i
++) {
314 if (!strcmp(p
, suitname
[i
]) || !strcmp(p
, suitchar
[i
])) {
329 * Reads and converts to upper case
345 * Reads in a decimal number and makes sure it is between "lo" and
349 number(lo
, hi
, prompt
)
358 if (!(p
= getline()) || *p
== '\0') {
359 msg(quiet
? "Not a number" :
360 "That doesn't look like a number");
368 while (isdigit(*p
)) {
369 sum
= 10 * sum
+ (*p
- '0');
373 if (*p
!= ' ' && *p
!= '\t' && *p
!= '\0')
375 if (sum
>= lo
&& sum
<= hi
)
378 msg("that doesn't look like a number, try again --> ");
380 msg("%d is not between %d and %d inclusive, try again --> ",
388 * Display a message at the top of the screen.
390 char Msgbuf
[BUFSIZ
] = {'\0'};
392 static int Newpos
= 0;
396 msg(const char *fmt
, ...)
410 (void)vsprintf(&Msgbuf
[Newpos
], fmt
, ap
);
411 Newpos
= strlen(Msgbuf
);
418 * Add things to the current message
422 addmsg(const char *fmt
, ...)
424 addmsg(fmt
, va_alist
)
436 (void)vsprintf(&Msgbuf
[Newpos
], fmt
, ap
);
437 Newpos
= strlen(Msgbuf
);
450 static int lastline
= 0;
454 /* All messages should start with uppercase */
455 mvaddch(lastline
+ Y_MSG_START
, SCORE_X
, ' ');
456 if (islower(Msgbuf
[0]) && Msgbuf
[1] != ')')
457 Msgbuf
[0] = toupper(Msgbuf
[0]);
460 if (len
/ MSG_X
+ Lineno
>= MSG_Y
) {
461 while (Lineno
< MSG_Y
) {
462 wmove(Msgwin
, Lineno
++, 0);
467 mvaddch(Lineno
+ Y_MSG_START
, SCORE_X
, '*');
470 mvwaddstr(Msgwin
, Lineno
, 0, mp
);
471 if ((len
= strlen(mp
)) > MSG_X
) {
473 for (mp
= &mp
[MSG_X
- 1]; *mp
!= ' '; mp
--)
478 wmove(Msgwin
, Lineno
, mp
- omp
);
481 if (++Lineno
>= MSG_Y
)
483 } while (len
> MSG_X
);
494 * Wait for the user to type ' ' before doing anything else
499 static const char prompt
[] = {'-', '-', 'M', 'o', 'r', 'e', '-', '-', '\0'};
501 if ((int)(Mpos
+ sizeof prompt
) < MSG_X
)
502 wmove(Msgwin
, Lineno
> 0 ? Lineno
- 1 : MSG_Y
- 1, Mpos
);
504 mvwaddch(Msgwin
, Lineno
, 0, ' ');
506 if (++Lineno
>= MSG_Y
)
509 waddstr(Msgwin
, prompt
);
516 * Sit around until the guy types the right key
525 while ((c
= readchar()) != '\n')
528 while (readchar() != ch
)
534 * Reads and returns a character, checking for gross input errors
544 while (read(STDIN_FILENO
, &c
, sizeof(char)) <= 0)
545 if (cnt
++ > 100) { /* if we are getting infinite EOFs */
546 bye(); /* quit the game */
549 if (c
== CTRL('L')) {
561 * Reads the next line up to '\n' or EOF. Multiple spaces are
562 * compressed to one space; a space is inserted before a ','
573 getyx(stdscr
, oy
, ox
);
575 /* loop reading in the string, and put it in a temporary buffer */
576 for (sp
= linebuf
; (c
= readchar()) != '\n'; clrtoeol(), refresh()) {
580 if (c
== erasechar()) { /* process erase character */
585 for (i
= strlen(unctrl(*sp
)); i
; i
--)
590 if (c
== killchar()) { /* process kill
596 if (sp
== linebuf
&& c
== ' ')
598 if (sp
>= &linebuf
[LINESIZE
- 1] || !(isprint(c
) || c
== ' '))
615 int signo
__attribute__((__unused__
));
623 * Leave the program, cleaning things up as we go.
628 signal(SIGINT
, SIG_IGN
);
629 mvcur(0, COLS
- 1, LINES
- 1, 0);