A game card
- namespace Card;
- require_once 'So-o.php';
- define(__NAMESPACE__ . '\TWO', 0);
- define(__NAMESPACE__ . '\THREE', 1);
- define(__NAMESPACE__ . '\FOUR', 2);
- define(__NAMESPACE__ . '\FIVE', 3);
- define(__NAMESPACE__ . '\SIX', 4);
- define(__NAMESPACE__ . '\SEVEN', 5);
- define(__NAMESPACE__ . '\EIGHT', 6);
- define(__NAMESPACE__ . '\NINE', 7);
- define(__NAMESPACE__ . '\TEN', 8);
- define(__NAMESPACE__ . '\JACK', 9);
- define(__NAMESPACE__ . '\QUEEN', 10);
- define(__NAMESPACE__ . '\KING', 11);
- define(__NAMESPACE__ . '\ACE', 12);
- define(__NAMESPACE__ . '\CLUBS', 0);
- define(__NAMESPACE__ . '\DIAMONDS', 1);
- define(__NAMESPACE__ . '\HEARTS', 2);
- 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.
- 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.
- function i_init($self, $rank, $suit) {
- supersend('init', func_get_args());
- sendmsg($self, 'set', 'rank', $rank);
- sendmsg($self, 'set', 'suit', $suit);
- return $self;
- }
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
.
- function i_rank($self) {
- return sendmsg($self, 'get', 'rank');
- }
rank
returns the rank property of $self
.
- function i_suit($self) {
- return sendmsg($self, 'get', 'suit');
- }
suit
returns the suit property of $self
.
- function i_compare($self, $aCard) {
- $rank1=sendmsg($self, 'get', 'rank');
- $rank2=sendmsg($aCard, 'get', 'rank');
- return $rank1 == $rank2 ? 0 : $rank1 > $rank2 ? 1 : -1;
- }
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.
- function i_toString($self) {
- 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');
- static $ssuit=array(CLUBS => 'c', DIAMONDS => 'd', HEARTS => 'h', SPADES => 's');
- $rank=sendmsg($self, 'get', 'rank');
- $suit=sendmsg($self, 'get', 'suit');
- return $srank[$rank] . $ssuit[$suit];
- }
toString
returns a string containing the rank and the suit of $self
.
- 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.
- require_once 'So-o.php';
- require_once 'Card.php';
Loads So-o and the Card class.
- $card_2C=sendmsg($Card, 'new', \Card\TWO, \Card\CLUBS);
- echo '2c (two of clubs) -> ';
- sendmsg($card_2C, 'print', true);
Creates a card, the 2 of clubs, and displays it.
- $card_Kh=sendmsg($Card, 'new', \Card\KING, \Card\HEARTS);
- echo 'Kh (king of hearts) -> ';
- 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