A game of poker
Download the code of the game of poker in the So-o folder:
- So-o
- Makefile
- So-o.h
- So-o.c
- OL.c
- Object.c
- ...
- poker
- Makefile
- poker.c
- Card.c
- Hand.c
- Deck.c
- ...
In the folder So-o, make sure the library libso-o.a has been compiled:
$ cd So-o
$ make
...
ar rc libso-o.a So-o.o Object.o OL.o list.o alist.o Once.o Application.o Responder.o
If you wish to add the library and the header files of So-o to your system, type make install
.
In the subfolder poker, compile the executable poker
:
$ make
gcc -g -I.. -Wno-missing-braces -O -fstrength-reduce -finline-functions -fomit-frame-pointer -c poker.c
gcc -g -I.. -Wno-missing-braces -O -fstrength-reduce -finline-functions -fomit-frame-pointer -c Deck.c
gcc -g -I.. -Wno-missing-braces -O -fstrength-reduce -finline-functions -fomit-frame-pointer -c Hand.c
gcc -g -I.. -Wno-missing-braces -O -fstrength-reduce -finline-functions -fomit-frame-pointer -c Card.c
gcc -g poker.o Deck.o Hand.o Card.o ../libso-o.a -o poker
Run the program poker:
$ ./poker
3h,4h,4s,Jd,6c -> ONEPAIR
Keep (1-5...)? 23
You have one pair. The program asks which cards you wish to keep. Enter 23 to keep the second and the third card and draw 3 new cards. The program displays the result of the second draw:
Js,4h,4s,Ks,4c -> THREEOFKIND
Play or (q)uit?
You have a three of a kind. Press Enter to play again:
3c,Ah,2s,7d,3d -> ONEPAIR
Keep (1-5...)? 15
3c,5h,3s,5d,3d -> FULLHOUSE
Play or (q)uit? q
Enter q to quit the program.
CODE
- #include "So-o.h"
- #include "Card.h"
- #include "Hand.h"
- #include "Deck.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <regex.h>
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(®exp, "([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(®exp, 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.
Comments