3
136

A game of poker

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

  1. So-o
    1. So-o.php
    2. OL.php
    3. Object.php
    4. ...
    5. poker
      1. poker.php
      2. Card.php
      3. Hand.php
      4. Deck.php
      5. ...

In the folder poker, run the program poker.php:

$ php -f poker.php 
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. set_include_path(dirname(getcwd()) . PATH_SEPARATOR . getcwd());

Sets the PHP configuration parameter include_path to the current directory and the directory containing the So-o code.

  1. require_once 'So-o.php';
  2.  
  3. require_once 'Deck.php';

Loads So-o and the Deck class.

  1. $deck=sendmsg($Deck, 'new', true);
  2. sendmsg($deck, 'shuffle');

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

  1. $stdin = fopen('php://stdin', 'r');

Opens the keyboard to interact with the user.

  1. do {

Plays one turn indefinitely until the user exits the game.

  1. }
  2. while (true);
  3.  
  4. fclose($stdin);

Closes the keyboard before quitting the program.

  1.     $hand=sendmsg($deck, 'hand');
  2.     sendmsg($hand, 'print', true);

Draws a hand of five cards and displays it.

  1.     echo 'Keep (1-5...)? ';
  2.     $line = fgets($stdin);
  3.     if ($line === false) {
  4.         break;
  5.     }
  6.     trim($line);

Asks the user which cards are to be kept. Reads the answer from the keyboard. Quits if the input is closed.

  1.     $keep=array_fill(1, 5, false);
  2.     preg_match_all('/\d/', $line, $r);
  3.     foreach ($r[0] as $n) {
  4.         if ($n >=1 and $n <= 5) {
  5.             $keep[$n]=true;
  6.         }
  7.     }

Fills the array of five booleans $keep, one for each card, with false then with true for each card which must be kept.

  1.     for ($i=1; $i <= 5; $i++) {
  2.         if (!$keep[$i]) {
  3.             sendmsg($hand, 'setCard', $i, sendmsg($deck, 'deal'));
  4.         }
  5.     }
  6.     sendmsg($hand, 'print', true);

Redraws a card for each card which isn't kept. Displays the hand.

  1.     echo 'Play or (q)uit? ';
  2.     $line = fgets($stdin);
  3.     if ($line === false) {
  4.         break;
  5.     }
  6.     trim($line);
  7.     if ($line[0] == 'q' or $line[0] == 'Q') {
  8.         break;
  9.     }

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 PHP 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].