1
9

A game of poker

Download the code of the game of poker in the So-o folder:

  1. So-o
    1. Makefile
    2. So-o.h
    3. So-o.c
    4. OL.c
    5. Object.c
    6. ...
    7. poker
      1. Makefile
      2. poker.c
      3. Card.c
      4. Hand.c
      5. Deck.c
      6. ...

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
  1. #include "So-o.h"
  2.  
  3. #include "Card.h"
  4. #include "Hand.h"
  5. #include "Deck.h"
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. #include <string.h>
  11. #include <regex.h>

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

  1. int main( int argc, char *argv[] ) {
  2.     instance deck, hand;
  3.     list keep = list_new();
  4.  
  5.     regex_t regexp;
  6.     regmatch_t pmatch[2];
  7.  
  8.     regcomp(&regexp, "([1-5])", REG_EXTENDED );
  9.  
  10.     char line[16], num[2];
  11.     char *p, *beg, *end;
  12.     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.

  1.     defclassCard();
  2.     defclassHand();
  3.     defclassDeck();

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

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

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

  1.     for (;;) {

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

  1.     }
  2.  
  3.     exit( 0 );
  4. }

Quits the program at the end of the main loop.

  1.         sendmsg(deck, "shuffle");
  2.         hand = sendmsg(deck, "hand").p;
  3.         sendmsg(hand, "print", 1);

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

  1.         printf("Keep (1-5...)? ");
  2.  
  3.         if (! fgets(line, sizeof (line), stdin))
  4.             break;

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

  1.         for (i = 0; i < 5; i++ )
  2.             list_put(keep, i, 0);

Initializes the list of cards to keep with negative values.

  1.         p = line;
  2.         while (*p) {
  3.             if (regexec(&regexp, p, 2, pmatch, 0) != 0 )
  4.                 break;
  5.  
  6.             if (pmatch[1].rm_so == -1)
  7.                 break;
  8.  
  9.             beg = p+pmatch[1].rm_so;
  10.             end = p+pmatch[1].rm_eo;
  11.  
  12.             strncpy(num, beg, end-beg);
  13.  
  14.             i = atoi(num);
  15.  
  16.             list_put(keep, i-1, (void *)1);
  17.  
  18.             p = end;
  19.         }

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

  1.         for (i = 0; i < 5; i++ ) {
  2.             if (!list_get(keep, i))
  3.                 sendmsg(hand, "setCard", i+1, sendmsg(deck, "deal").p);
  4.         }

Redraws a hand while keeping the selected cards.

  1.         sendmsg(hand, "print", 1);

Displays the final hand.

  1.         printf("Play or (q)uit? ");
  2.  
  3.         if (! fgets(line, sizeof (line), stdin))
  4.             break;
  5.  
  6.         for (p = line; *p == ' '; p++)
  7.             ;
  8.  
  9.         if (*p == 'q' || *p == 'Q')
  10.             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.

SEE ALSO

The C manual

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].