Playing cards: Difference between revisions

Added implementation for PHP
(added Racket)
(Added implementation for PHP)
Line 1,299:
d := make(deck, 52)
for i := range d {
d[i] = card(i)/lang>
 
Example (first 4 cards of a shuffled deck):
d[i] = card(i)
}
return d
Line 2,086 ⟶ 2,089:
Top card: 3♦
Deck, re-shuffled: K♦ 4♣ J♠ 2♥ J♥ K♣ 6♣ 5♠ 3♥ 6♦ 5♦ 4♠ J♣ 4♦ 6♥ K♥ 7♥ 7♦ 2♦ 4♥ 6♠ 7♣ 9♦ 3♣ 3♠ 2♣ 2♠ 8♦ 5♣ 9♠ 5♥ J♦ 9♥ Q♦ Q♣ Q♥ Q♠ 8♥ 8♠ K♠ 9♣ 8♣ 7♠</pre>
 
=={{header|PHP}}==
Implementation:
<lang php>class Card
{
// if unable to save as UTF-8, use other, non-UTF-8, symbols here
protected static $suits = array( '♠', '♥', '♦', '♣' );
 
protected static $pips = array( '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' );
 
protected $suit;
 
protected $pip;
 
public function __construct( $suit, $pip )
{
if( !in_array( $suit, self::$suits ) )
{
throw new InvalidArgumentException( 'Invalid suit' );
}
if( !in_array( $pip, self::$pips ) )
{
throw new InvalidArgumentException( 'Invalid pip' );
}
 
$this->suit = $suit;
$this->pip = $pip;
}
 
public function getSuit()
{
return $this->suit;
}
 
public function getPip()
{
return $this->pip;
}
 
public function __toString()
{
return $this->suit . $this->pip;
}
 
public static function getSuits()
{
return self::$suits;
}
 
public static function getPips()
{
return self::$pips;
}
}
 
class CardCollection
implements Countable
{
protected $cards = array();
 
protected function __construct( array $cards = array() )
{
foreach( $cards as $card )
{
$this->addCard( $card );
}
}
 
public function count()
{
return count( $this->cards );
}
 
public function toArray()
{
return $this->cards;
}
 
public function __toString()
{
return implode( ' ', $this->cards );
}
 
protected function addCard( Card $card )
{
$this->cards[] = $card;
}
}
 
class Deck
extends CardCollection
{
public function __construct( $shuffled = false )
{
foreach( Card::getSuits() as $suit )
{
foreach( Card::getPips() as $pip )
{
$this->addCard( new Card( $suit, $pip ) );
}
}
 
if( $shuffled )
{
$this->shuffle();
}
}
 
public function deal( $amount = 1 )
{
$cardCount = count( $this->cards );
if( !is_int( $amount ) || $amount < 1 || $cardCount == 0 || $amount > $cardCount )
{
return false;
}
 
return new CardCollection( array_splice( $this->cards, -$amount ) );
}
 
public function shuffle()
{
shuffle( $this->cards );
}
 
public function __toString()
{
return implode( ' ', $this->cards );
}
}</lang>
 
Usage:
<lang php>// if viewing in a browser, lets output a text/plain header with utf-8 charset
header( 'Content-Type: text/plain; charset=utf-8' );
 
// create a new Deck
$deck = new Deck();
 
// show the cards in the new deck
echo count( $deck ) . ' cards in the new deck: ' . PHP_EOL . $deck . PHP_EOL;
 
// shuffle the deck
$deck->shuffle();
 
// show the cards in the shuffled deck
echo PHP_EOL . count( $deck ) . ' cards in the new deck, shuffled: ' . PHP_EOL . $deck . PHP_EOL . PHP_EOL;
 
// deal four cards per player, to four players
for( $p = 1; $p <= 4; $p++ )
{
// deal player cards
$playerCards = $deck->deal( 4 );
// show player cards
echo 'Player ' . $p . ' got dealt the following ' . count( $playerCards ) . ' cards: ' . $playerCards . PHP_EOL;
}
 
// show the remaining cards in the deck
echo PHP_EOL . count( $deck ) . ' cards remaining in the deck: ' . PHP_EOL . $deck . PHP_EOL;</lang>
 
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, shuffled:
♦K ♥5 ♠3 ♦4 ♠A ♠2 ♣7 ♥3 ♦3 ♣9 ♥A ♠T ♦Q ♣4 ♣A ♦T ♥K ♥8 ♦2 ♥6 ♣3 ♣5 ♠Q ♠7 ♠5 ♦5 ♠6 ♥9 ♥Q ♠K ♠J ♣Q ♣2 ♥2 ♣K ♦9 ♦7 ♦J ♦8 ♥T ♣J ♠9 ♠4 ♦A ♣T ♦6 ♥7 ♥4 ♣6 ♠8 ♥J ♣8
 
Player 1 got dealt the following 4 cards: ♣6 ♠8 ♥J ♣8
Player 2 got dealt the following 4 cards: ♣T ♦6 ♥7 ♥4
Player 3 got dealt the following 4 cards: ♣J ♠9 ♠4 ♦A
Player 4 got dealt the following 4 cards: ♦7 ♦J ♦8 ♥T
 
36 cards remaining in the deck:
♦K ♥5 ♠3 ♦4 ♠A ♠2 ♣7 ♥3 ♦3 ♣9 ♥A ♠T ♦Q ♣4 ♣A ♦T ♥K ♥8 ♦2 ♥6 ♣3 ♣5 ♠Q ♠7 ♠5 ♦5 ♠6 ♥9 ♥Q ♠K ♠J ♣Q ♣2 ♥2 ♣K ♦9</pre>
 
=={{header|PicoLisp}}==
Line 2,372 ⟶ 2,547:
(set-box! deck (shuffle (unbox deck))))
 
;; deal a card from thetA deck:2 3 4 5 6 7 8 9 10 J Q K>;
enum Suit he deck:
(define (deck-deal deck)
(begin0 (first (unbox deck))