]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/io.c
2 * Copyright (c) 1980 Regents of the University of California.
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 /*static char sccsid[] = "from: @(#)io.c 5.8 (Berkeley) 2/28/91";*/
36 static char rcsid
[] = "$Id: io.c,v 1.6 1993/12/08 07:50:47 mycroft Exp $";
45 # include "cribbage.h"
53 # define CTRL(X) ('X' - 'A' + 1)
55 # ifdef notdef /* defined in curses.h */
56 # define erasechar() _tty.sg_erase
57 # define killchar() _tty.sg_kill
60 char linebuf
[ LINESIZE
];
62 char *rankname
[ RANKS
] = { "ACE", "TWO", "THREE", "FOUR",
63 "FIVE", "SIX", "SEVEN", "EIGHT",
64 "NINE", "TEN", "JACK", "QUEEN",
67 char *rankchar
[ RANKS
] = { "A", "2", "3", "4", "5", "6", "7",
68 "8", "9", "T", "J", "Q", "K" };
70 char *suitname
[ SUITS
] = { "SPADES", "HEARTS", "DIAMONDS",
73 char *suitchar
[ SUITS
] = { "S", "H", "D", "C" };
79 * Display a message at the top of the screen.
81 char Msgbuf
[BUFSIZ
] = { '\0' };
85 static int Newpos
= 0;
92 (void)vsprintf(&Msgbuf
[Newpos
], fmt
, ap
);
98 * Add things to the current message
100 addmsg(char *fmt
, ...)
105 (void)vsprintf(&Msgbuf
[Newpos
], fmt
, ap
);
118 register char *mp
, *omp
;
119 static int lastline
= 0;
122 * All messages should start with uppercase
124 mvaddch(lastline
+ Y_MSG_START
, SCORE_X
, ' ');
125 if (islower(Msgbuf
[0]) && Msgbuf
[1] != ')')
126 Msgbuf
[0] = toupper(Msgbuf
[0]);
129 if (len
/ MSG_X
+ Lineno
>= MSG_Y
) {
130 while (Lineno
< MSG_Y
) {
131 wmove(Msgwin
, Lineno
++, 0);
136 mvaddch(Lineno
+ Y_MSG_START
, SCORE_X
, '*');
139 mvwaddstr(Msgwin
, Lineno
, 0, mp
);
140 if ((len
= strlen(mp
)) > MSG_X
) {
142 for (mp
= &mp
[MSG_X
-1]; *mp
!= ' '; mp
--)
147 wmove(Msgwin
, Lineno
, mp
- omp
);
150 if (++Lineno
>= MSG_Y
)
152 } while (len
> MSG_X
);
163 * Call msgcrd in one of two forms
170 return msgcrd(c
, TRUE
, (char *) NULL
, TRUE
);
172 return msgcrd(c
, FALSE
, " of ", FALSE
);
179 * Print the value of a card in ascii
181 msgcrd(c
, brfrank
, mid
, brfsuit
)
184 BOOLEAN brfrank
, brfsuit
;
186 if (c
.rank
== EMPTY
|| c
.suit
== EMPTY
)
189 addmsg("%1.1s", rankchar
[c
.rank
]);
191 addmsg(rankname
[c
.rank
]);
195 addmsg("%1.1s", suitchar
[c
.suit
]);
197 addmsg(suitname
[c
.suit
]);
205 printcard(win
, cardno
, c
, blank
)
211 prcard(win
, cardno
* 2, cardno
, c
, blank
);
216 * Print out a card on the window at the specified location
218 prcard(win
, y
, x
, c
, blank
)
226 mvwaddstr(win
, y
+ 0, x
, "+-----+");
227 mvwaddstr(win
, y
+ 1, x
, "| |");
228 mvwaddstr(win
, y
+ 2, x
, "| |");
229 mvwaddstr(win
, y
+ 3, x
, "| |");
230 mvwaddstr(win
, y
+ 4, x
, "+-----+");
232 mvwaddch(win
, y
+ 1, x
+ 1, rankchar
[c
.rank
][0]);
233 waddch(win
, suitchar
[c
.suit
][0]);
234 mvwaddch(win
, y
+ 3, x
+ 4, rankchar
[c
.rank
][0]);
235 waddch(win
, suitchar
[c
.suit
][0]);
241 * Print a hand of n cards
243 prhand(h
, n
, win
, blank
)
252 for (i
= 0; i
< n
; i
++)
253 printcard(win
, i
, *h
++, blank
);
261 * reads a card, supposedly in hand, accepting unambigous brief
262 * input, returns the index of the card found...
264 infrom(hand
, n
, prompt
)
273 printf("\nINFROM: %d = n < 1!!\n", n
);
278 if (incard(&crd
)) { /* if card is full card */
279 if (!isone(crd
, hand
, n
))
280 msg("That's not in your hand");
282 for (i
= 0; i
< n
; i
++)
283 if (hand
[i
].rank
== crd
.rank
&&
284 hand
[i
].suit
== crd
.suit
)
287 printf("\nINFROM: isone or something messed up\n");
293 else /* if not full card... */
294 if (crd
.rank
!= EMPTY
) {
295 for (i
= 0; i
< n
; i
++)
296 if (hand
[i
].rank
== crd
.rank
)
299 msg("No such rank in your hand");
301 for (j
= i
+ 1; j
< n
; j
++)
302 if (hand
[j
].rank
== crd
.rank
)
305 msg("Ambiguous rank");
311 msg("Sorry, I missed that");
320 * Inputs a card in any format. It reads a line ending with a CR
321 * and then parses it.
334 if (!(line
= getline()))
337 while( *p1
!= ' ' && *p1
!= NULL
) ++p1
;
339 if( *p
== NULL
) goto gotit
;
340 /* IMPORTANT: no real card has 2 char first name */
341 if( strlen(p
) == 2 ) { /* check for short form */
343 for( i
= 0; i
< RANKS
; i
++ ) {
344 if( *p
== *rankchar
[i
] ) {
349 if( rnk
== EMPTY
) goto gotit
; /* it's nothing... */
350 ++p
; /* advance to next char */
352 for( i
= 0; i
< SUITS
; i
++ ) {
353 if( *p
== *suitchar
[i
] ) {
358 if( sut
!= EMPTY
) retval
= TRUE
;
362 for( i
= 0; i
< RANKS
; i
++ ) {
363 if( !strcmp( p
, rankname
[i
] ) || !strcmp( p
, rankchar
[i
] ) ) {
368 if( rnk
== EMPTY
) goto gotit
;
370 while( *p1
!= ' ' && *p1
!= NULL
) ++p1
;
372 if( *p
== NULL
) goto gotit
;
373 if( !strcmp( "OF", p
) ) {
375 while( *p1
!= ' ' && *p1
!= NULL
) ++p1
;
377 if( *p
== NULL
) goto gotit
;
380 for( i
= 0; i
< SUITS
; i
++ ) {
381 if( !strcmp( p
, suitname
[i
] ) || !strcmp( p
, suitchar
[i
] ) ) {
386 if( sut
!= EMPTY
) retval
= TRUE
;
397 * Reads and converts to upper case
412 * Reads in a decimal number and makes sure it is between "lo" and
415 number(lo
, hi
, prompt
)
426 if(!(p
= getline()) || *p
== NULL
) {
427 msg(quiet
? "Not a number" : "That doesn't look like a number");
435 while (isdigit(*p
)) {
436 sum
= 10 * sum
+ (*p
- '0');
440 if (*p
!= ' ' && *p
!= '\t' && *p
!= NULL
)
442 if (sum
>= lo
&& sum
<= hi
)
445 msg("that doesn't look like a number, try again --> ");
447 msg("%d is not between %d and %d inclusive, try again --> ",
455 * Perform an add onto the message buffer
464 * Do the printf into Msgbuf
466 junk
._flag
= _IOWRT
+ _IOSTRG
;
467 junk
._ptr
= &Msgbuf
[Newpos
];
469 _doprnt(fmt
, args
, &junk
);
471 Newpos
= strlen(Msgbuf
);
477 * Wait for the user to type ' ' before doing anything else
482 static char prompt
[] = { '-', '-', 'M', 'o', 'r', 'e', '-', '-', '\0' };
484 if (Mpos
+ sizeof prompt
< MSG_X
)
485 wmove(Msgwin
, Lineno
> 0 ? Lineno
- 1 : MSG_Y
- 1, Mpos
);
487 mvwaddch(Msgwin
, Lineno
, 0, ' ');
489 if (++Lineno
>= MSG_Y
)
492 waddstr(Msgwin
, prompt
);
499 * Sit around until the guy types the right key
507 while ((c
= readchar()) != '\n')
510 while (readchar() != ch
)
516 * Reads and returns a character, checking for gross input errors
520 register int cnt
, y
, x
;
525 while (read(0, &c
, 1) <= 0)
526 if (cnt
++ > 100) { /* if we are getting infinite EOFs */
527 bye(); /* quit the game */
542 * Reads the next line up to '\n' or EOF. Multiple spaces are
543 * compressed to one space; a space is inserted before a ','
549 register int c
, oy
, ox
;
550 register WINDOW
*oscr
;
554 getyx(stdscr
, oy
, ox
);
557 * loop reading in the string, and put it in a temporary buffer
559 for (sp
= linebuf
; (c
= readchar()) != '\n'; clrtoeol(), refresh()) {
562 else if (c
== erasechar()) { /* process erase character */
567 for (i
= strlen(unctrl(*sp
)); i
; i
--)
572 else if (c
== killchar()) { /* process kill character */
577 else if (sp
== linebuf
&& c
== ' ')
579 if (sp
>= &linebuf
[LINESIZE
-1] || !(isprint(c
) || c
== ' '))
602 * Leave the program, cleaning things up as we go.
606 signal(SIGINT
, SIG_IGN
);
607 mvcur(0, COLS
- 1, LINES
- 1, 0);