Playing cards: Difference between revisions

→‎{{header|PHP}}: Added Hand implementation. Refactored Deck::deal() to allow dealing into existing CardCollection (inspired by D language implementation)
(→‎{{header|PHP}}: Refactored, implemented Iterator interface and a sort method for CardCollection, and various compare methods for Card)
(→‎{{header|PHP}}: Added Hand implementation. Refactored Deck::deal() to allow dealing into existing CardCollection (inspired by D language implementation))
Line 2,320:
}
 
public function deal( $amount = 1, CardCollection $cardCollection = null )
{
if( !is_int( $amount ) || $amount < 1 )
Line 2,332:
}
 
return$cards new CardCollection(= array_splice( $this->cards, 0, $amount ) );
 
$cardCollection = $cardCollection ?: new CardCollection;
 
foreach( $cards as $card )
{
$cardCollection->addCard( $card );
}
 
return $cardCollection;
}
 
Line 2,338 ⟶ 2,347:
{
shuffle( $this->cards );
}
}
 
class Hand
extends CardCollection
{
// override CardCollection __constructor
// to allow public instantiation
// but disallow instantiation with cards
public function __construct() {}
 
public function play( $position )
{
if( !isset( $this->cards[ $position ] ) )
{
throw new OutOfBoundsException( 'Invalid position; position is not present in this hand' );
}
 
$result = array_splice( $this->cards, $position, 1 );
return $result[ 0 ];
}
}</lang>
Line 2,371 ⟶ 2,400:
echo PHP_EOL . count( $deck ) . ' cards in the new deck, shuffled: ' . PHP_EOL . $deck . PHP_EOL . PHP_EOL;
 
// dealintialize four cards per player, to four playershands
$players = array(
for( $p = 1; $p <= 4; $p++ )
new Hand,
new Hand,
new Hand,
new Hand
);
 
// three deal rounds, with amounts: 2, 2, 3
foreach( array( 2, 2, 3 ) as $amount )
{
// deal this rounds amount to player
foreach( $players as $hand )
{
$deck->deal( $amount, $hand );
}
}
 
foreach( $players as $p => $hand )
{
// dealsort player cards, and sort them, using default sort
$playerCards = $deck->deal( 4 )hand->sort();
// show player cards
echo 'Player ' . ( $p + 1 ) . ' got dealt the following ' . count( $playerCardshand ) . ' cards (sorted): ' . $playerCardshand . PHP_EOL;
}
 
Line 2,394 ⟶ 2,440:
 
52 cards in the new deck, shuffled:
♥3♣7 ♣2♠Q ♥6♥9 ♦A♠7 ♠J ♦K♠9 ♥T♣J ♦3♣4 ♦6♠8 ♠6♥Q ♥J♥2 ♥5♦J ♣J♠T ♥4♣Q ♥K♦A ♠4♠K ♦T♠2 ♠5♣K ♠8♦K ♠T♦Q ♠3 ♣6♣5 ♣9♥5 ♦5♠4 ♣T♥3 ♣3♦9 ♠K♣2 ♦8 ♣K ♣5 ♠2 ♥A ♥2♥6 ♥7♦2 ♥Q♦5 ♥8♥J ♠7♣T ♣7♦3 ♠Q♠5 ♠9♠A ♦J♣8 ♣Q♥8 ♠A♥7 ♦Q♥K ♦9♦7 ♣4♥T ♦2♦6 ♣8♦T ♥9♣6 ♣3 ♣A ♦4 ♦7♥4 ♠6 ♣9
 
Player 1 got dealt the following 47 cards (sorted): ♣2♠2 ♣7 ♠8 ♠Q ♥3♥Q ♥6♦K ♦A♣K
Player 2 got dealt the following 47 cards (sorted): ♦3♥2 ♠3 ♣5 ♠7 ♥T♥9 ♠J♦J ♦K♦Q
Player 3 got dealt the following 47 cards (sorted): ♥3 ♠4 ♥5 ♠6♠9 ♠T ♦6♠J ♥J♣Q
Player 4 got dealt the following 47 cards (sorted): ♠4♣2 ♥4♣4 ♦8 ♦9 ♣J ♥K♠K ♦A
 
3624 cards remaining in the deck:
♥A ♥6 ♦2 ♦5 ♥J ♣T ♦3 ♠5 ♠A ♣8 ♥8 ♥7 ♥K ♦7 ♥T ♦6 ♦T ♣6 ♣3 ♣A ♦4 ♥4 ♠6 ♣9</pre>
♦T ♠5 ♠8 ♠T ♠3 ♣6 ♣9 ♦5 ♣T ♣3 ♠K ♦8 ♣K ♣5 ♠2 ♥A ♥2 ♥7 ♥Q ♥8 ♠7 ♣7 ♠Q ♠9 ♦J ♣Q ♠A ♦Q ♦9 ♣4 ♦2 ♣8 ♥9 ♣A ♦4 ♦7</pre>
 
=={{header|PicoLisp}}==