CODE

Includes the declarations of the functions by So-o and the constructors of the classes Card, Hand and Deck.

int main(int argc,char*argv[]){

instance deck, hand;

list keep = list_new();

regex_t regexp;

regmatch_t pmatch[2];

regcomp(&regexp,"([1-5])", REG_EXTENDED );

char line[16], num[2];

char*p,*beg,*end;

int i;

The list keep is used to determine which cards are kept between the two hands of a game round.
regexp contains the regular expression used to extract from the input a card number which is to be kept in a hand.

defclassCard();

defclassHand();

defclassDeck();

Defines the classes Card, Hand and Deck.
The class Object is automatically defined by the function defclass if necessary.

deck = sendmsg(Deck,"new",1).p;

Creates a deck of cards which is automatically shuffled when all the cards have been drawn.

for(;;){

Plays a turn indefinitely as long as the user doesn't quit the game.

}

exit(0);

}

Quits the program at the end of the main loop.

sendmsg(deck,"shuffle");

hand = sendmsg(deck,"hand").p;

sendmsg(hand,"print",1);

For every turn, shuffles the deck of games, draws a hand and displays it.

printf("Keep (1-5...)? ");

if(!fgets(line,sizeof(line), stdin))

break;

Asks the user to choose which cards she wants to keep. Reads the answer.

for(i =0; i <5; i++)

list_put(keep, i,0);

Initializes the list of cards to keep with negative values.

p = line;

while(*p){

if(regexec(&regexp, p,2, pmatch,0)!=0)

break;

if(pmatch[1].rm_so==-1)

break;

beg = p+pmatch[1].rm_so;

end = p+pmatch[1].rm_eo;

strncpy(num, beg, end-beg);

i =atoi(num);

list_put(keep, i-1,(void*)1);

p = end;

}

Extracts from the input digits from 1 to 5 and marks the corresponding cards for keeping.

for(i =0; i <5; i++){

if(!list_get(keep, i))

sendmsg(hand,"setCard", i+1, sendmsg(deck,"deal").p);

}

Redraws a hand while keeping the selected cards.

sendmsg(hand,"print",1);

Displays the final hand.

printf("Play or (q)uit? ");

if(!fgets(line,sizeof(line), stdin))

break;

for(p = line;*p ==' '; p++)

;

if(*p =='q'||*p =='Q')

break;

Asks the user if she wants to play again or quit the game. Quits the game if the awswer starts with q or Q or if the input is closed.