Spades.c
/*
* ========================================================================
* Input routines
* ========================================================================
*/
/*
* standard cards are always 4 bytes long including the NUL
*/
char * read_spades_card_string[] = {
"Enter the card you wish to play:", /* Action: Play a card */
};
enum read_spades_card_actions {
READ_SPADES_CARD_PLAY = 0,
};
int read_spades_card(int fd,
enum read_spades_card_actions action, char buf[4])
{
int read_len, i;
char read_buf[16];
write (fd, read_spades_card_string[action],
strlen(read_spades_card_string[action]));
if (0 >= (read_len = read(fd, read_buf, 16))) {
return(read_len);
}
/* Eliminate initial spaces */
for (i = 0; i < read_len - 4; i++) {
if (!isspace((int)read_buf[i])) {
break;
}
}
buf[0] = toupper(read_buf[i++]);
buf[1] = toupper(read_buf[i++]);
buf[2] = toupper(read_buf[i++]);
buf[3] = '\0';
return(1);
}
int read_spades_bid(int fd, int * bid)
{
int read_len;
char read_buf[16];
write (fd, "Enter the number of tricks you will take:" ,
strlen( "Enter the number of tricks you will take:"));
if (0 >= (read_len = read(fd, read_buf, 16))) {
return(read_len);
}
*bid = strtoul(read_buf, NULL, 10);
return(1);
}
next slide