Anonymous user
Playing cards: Difference between revisions
→{{header|PHP}}: Refactored, implemented Iterator interface and a sort method for CardCollection, and various compare methods for Card
m (→{{header|PHP}}: Removed redundant Deck::__toString() and added convenience method CardCollection::toString()) |
(→{{header|PHP}}: Refactored, implemented Iterator interface and a sort method for CardCollection, and various compare methods for Card) |
||
Line 2,091:
=={{header|PHP}}==
{{works with|PHP|5.3+}}
Implementation:
<lang php>class Card
Line 2,100 ⟶ 2,101:
protected $suit;
protected $suitOrder;
protected $pip;
protected $pipOrder;
protected $order;
public function __construct( $suit, $pip )
Line 2,126 ⟶ 2,133:
{
return $this->pip;
}
public function getSuitOrder()
{
// lazy evaluate suit order
if( !isset( $this->suitOrder ) )
{
// cache suit order
$this->suitOrder = array_search( $this->suit, self::$suits );
}
return $this->suitOrder;
}
public function getPipOrder()
{
// lazy evaluate pip order
if( !isset( $this->pipOrder ) )
{
// cache pip order
$this->pipOrder = array_search( $this->pip, self::$pips );
}
return $this->pipOrder;
}
public function getOrder()
{
// lazy evaluate order
if( !isset( $this->order ) )
{
$suitOrder = $this->getSuitOrder();
$pipOrder = $this->getPipOrder();
// cache order
$this->order = $pipOrder * count( self::$suits ) + $suitOrder;
}
return $this->order;
}
public function compareSuit( Card $other )
{
return $this->getSuitOrder() - $other->getSuitOrder();
}
public function comparePip( Card $other )
{
return $this->getPipOrder() - $other->getPipOrder();
}
public function compare( Card $other )
{
return $this->getOrder() - $other->getOrder();
}
Line 2,145 ⟶ 2,205:
class CardCollection
implements Countable, Iterator
{
protected $cards = array();
Line 2,157 ⟶ 2,217:
}
/**
* Countable::count() implementation
*/
public function count()
{
Line 2,162 ⟶ 2,225:
}
/**
* Iterator::key() implementation
*/
public function key()
{
return key( $this->cards );
}
/**
* Iterator::valid() implementation
*/
public function valid()
{
return null !== $this->key();
}
/**
* Iterator::next() implementation
*/
public function next()
{
next( $this->cards );
}
/**
* Iterator::current() implementation
*/
public function current()
{
return current( $this->cards );
}
/**
* Iterator::rewind() implementation
*/
public function rewind()
{
reset( $this->cards );
}
public function sort( $comparer = null )
{
$comparer = $comparer ?: function( $a, $b ) {
return $a->compare( $b );
};
if( !is_callable( $comparer ) )
{
throw new InvalidArgumentException( 'Invalid comparer; comparer should be callable' );
}
usort( $this->cards, $comparer );
return $this;
}
Line 2,180 ⟶ 2,292:
protected function addCard( Card $card )
{
if( in_array( $card, $this->cards ) )
{
throw new DomainException( 'Card is already present in this collection' );
}
$this->cards[] = $card;
}
Line 2,192 ⟶ 2,309:
{
foreach( Card::getPips() as $pip )
{
$this->addCard( new Card( $suit, $pip ) );
}
Line 2,205 ⟶ 2,322:
public function deal( $amount = 1 )
{
if( !is_int( $
{
throw new InvalidArgumentException( 'Invalid amount; amount should be an integer, larger than 0' );
}
if( $amount > count( $this->cards ) )
{
throw new RangeException( 'Invalid amount; requested amount is larger than the amount of available cards' );
}
Line 2,229 ⟶ 2,350:
// show the cards in the new deck
echo count( $deck ) . ' cards in the new deck: ' . PHP_EOL . $deck . PHP_EOL;
// sort the deck, default sort
$deck->sort();
// show the cards in the sorted deck
echo PHP_EOL . count( $deck ) . ' cards in the new deck, default sort: ' . PHP_EOL . $deck . PHP_EOL;
// sort the deck, custom sort
$deck->sort( function( $a, $b ) {
return $a->compareSuit( $b ) ?: $a->comparePip( $b );
} );
// show the cards in the sorted deck
echo PHP_EOL . count( $deck ) . ' cards in the new deck, custom sort: ' . PHP_EOL . $deck . PHP_EOL;
// shuffle the deck
Line 2,239 ⟶ 2,374:
for( $p = 1; $p <= 4; $p++ )
{
// deal player cards, and sort them, using default sort
$playerCards = $deck->deal( 4 )->sort();
// show player cards
echo 'Player ' . $p . ' got dealt the following ' . count( $playerCards ) . ' cards: ' . $playerCards . PHP_EOL;
Line 2,250 ⟶ 2,385:
This will output something like:
<pre>52 cards in the new deck:
♠2 ♠3 ♠4 ♠5 ♠6 ♠7 ♠8 ♠9 ♠T ♠J ♠Q ♠K ♠A ♥2 ♥3 ♥4 ♥5 ♥6 ♥7 ♥8 ♥9 ♥T ♥J ♥Q ♥K ♥A ♦2 ♦3 ♦4 ♦5 ♦6 ♦7 ♦8 ♦9 ♦T ♦J ♦Q ♦K ♦A ♣2 ♣3 ♣4 ♣5 ♣6 ♣7 ♣8 ♣9 ♣T ♣J ♣Q ♣K ♣A
52 cards in the new deck, default sort:
♠2 ♥2 ♦2 ♣2 ♠3 ♥3 ♦3 ♣3 ♠4 ♥4 ♦4 ♣4 ♠5 ♥5 ♦5 ♣5 ♠6 ♥6 ♦6 ♣6 ♠7 ♥7 ♦7 ♣7 ♠8 ♥8 ♦8 ♣8 ♠9 ♥9 ♦9 ♣9 ♠T ♥T ♦T ♣T ♠J ♥J ♦J ♣J ♠Q ♥Q ♦Q ♣Q ♠K ♥K ♦K ♣K ♠A ♥A ♦A ♣A
52 cards in the new deck, custom sort:
♠2 ♠3 ♠4 ♠5 ♠6 ♠7 ♠8 ♠9 ♠T ♠J ♠Q ♠K ♠A ♥2 ♥3 ♥4 ♥5 ♥6 ♥7 ♥8 ♥9 ♥T ♥J ♥Q ♥K ♥A ♦2 ♦3 ♦4 ♦5 ♦6 ♦7 ♦8 ♦9 ♦T ♦J ♦Q ♦K ♦A ♣2 ♣3 ♣4 ♣5 ♣6 ♣7 ♣8 ♣9 ♣T ♣J ♣Q ♣K ♣A
52 cards in the new deck, shuffled:
Player 1 got dealt the following 4 cards:
Player 2 got dealt the following 4 cards:
Player 3 got dealt the following 4 cards:
Player 4 got dealt the following 4 cards:
36 cards remaining in the deck:
=={{header|PicoLisp}}==
|