19

A game card

  1. namespace Card;
  2.  
  3. require_once 'So-o.php';
  4.  
  5. define(__NAMESPACE__ . '\TWO',      0);
  6. define(__NAMESPACE__ . '\THREE',    1);
  7. define(__NAMESPACE__ . '\FOUR',     2);
  8. define(__NAMESPACE__ . '\FIVE',     3);
  9. define(__NAMESPACE__ . '\SIX',      4);
  10. define(__NAMESPACE__ . '\SEVEN',    5);
  11. define(__NAMESPACE__ . '\EIGHT',    6);
  12. define(__NAMESPACE__ . '\NINE',     7);
  13. define(__NAMESPACE__ . '\TEN',      8);
  14. define(__NAMESPACE__ . '\JACK',     9);
  15. define(__NAMESPACE__ . '\QUEEN',    10);
  16. define(__NAMESPACE__ . '\KING',     11);
  17. define(__NAMESPACE__ . '\ACE',      12);
  18.  
  19. define(__NAMESPACE__ . '\CLUBS',    0);
  20. define(__NAMESPACE__ . '\DIAMONDS', 1);
  21. define(__NAMESPACE__ . '\HEARTS',   2);
  22. define(__NAMESPACE__ . '\SPADES',   3);

Places the code of the class in its own namespace. Loads So-o. Defines a constant for every card rank and suit.

  1. defclass('Card', null, 1, null, array('rank', 'suit'), null, array('init', 'rank', 'suit', 'compare', 'toString'));

The Card class inherits from the Object class. An instance has 2 properties: rank, its value, and suit, its color. An instance redefines the messages init and toString. It implements the messages rank, suit and compare.

  1. function i_init($self, $rank, $suit) {
  2.     supersend('init', func_get_args());
  3.  
  4.     sendmsg($self, 'set', 'rank', $rank);
  5.     sendmsg($self, 'set', 'suit', $suit);
  6.  
  7.     return $self;
  8. }

init takes 2 arguments: $rank and $suit, the value and the color of a card. It initializes the properties rank and suit of $self with $rank and $suit.

  1. function i_rank($self) {
  2.     return sendmsg($self, 'get', 'rank');
  3. }

rank returns the rank property of $self.

  1. function i_suit($self) {
  2.     return sendmsg($self, 'get', 'suit');
  3. }

suit returns the suit property of $self.

  1. function i_compare($self, $aCard) {
  2.     $rank1=sendmsg($self, 'get', 'rank');
  3.     $rank2=sendmsg($aCard, 'get', 'rank');
  4.  
  5.     return $rank1 == $rank2 ? 0 : $rank1 > $rank2 ? 1 : -1;
  6. }

compare compares $self with $aCard, another instance of Card. It returns 0 if the rank of $self is equal to the rank of $aCard, -1 if it's lower, 1 if it's higher. compare is used to sort an instance of Hand.

  1. function i_toString($self) {
  2.     static $srank=array(TWO => '2', THREE => '3', FOUR => '4', FIVE => '5', SIX => '6', SEVEN => '7', EIGHT => '8', NINE => '9', TEN => '10', JACK => 'J', QUEEN => 'Q', KING => 'K', ACE => 'A');
  3.     static $ssuit=array(CLUBS => 'c', DIAMONDS => 'd', HEARTS => 'h', SPADES => 's');
  4.  
  5.     $rank=sendmsg($self, 'get', 'rank');
  6.     $suit=sendmsg($self, 'get', 'suit');
  7.  
  8.     return $srank[$rank] . $ssuit[$suit];
  9. }

toString returns a string containing the rank and the suit of $self.

  1. set_include_path(getcwd() . PATH_SEPARATOR . dirname(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 'Card.php';

Loads So-o and the Card class.

  1. $card_2C=sendmsg($Card, 'new', \Card\TWO, \Card\CLUBS);
  2. echo '2c (two of clubs) -> ';
  3. sendmsg($card_2C, 'print', true);

Creates a card, the 2 of clubs, and displays it.

  1. $card_Kh=sendmsg($Card, 'new', \Card\KING, \Card\HEARTS);
  2. echo 'Kh (king of hearts) -> ';
  3. sendmsg($card_Kh, 'print', true);

Creates another card, the king of hearts, and displays it.

$ php -f testCard.php
2c (two of clubs) -> 2c
Kh (king of hearts) -> Kh

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