Playing cards: Difference between revisions

Content added Content deleted
(→‎{{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: Line 2,320:
}
}


public function deal( $amount = 1 )
public function deal( $amount = 1, CardCollection $cardCollection = null )
{
{
if( !is_int( $amount ) || $amount < 1 )
if( !is_int( $amount ) || $amount < 1 )
Line 2,332: Line 2,332:
}
}


return new CardCollection( array_splice( $this->cards, 0, $amount ) );
$cards = array_splice( $this->cards, 0, $amount );

$cardCollection = $cardCollection ?: new CardCollection;

foreach( $cards as $card )
{
$cardCollection->addCard( $card );
}

return $cardCollection;
}
}


Line 2,338: Line 2,347:
{
{
shuffle( $this->cards );
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>
}</lang>
Line 2,371: Line 2,400:
echo PHP_EOL . count( $deck ) . ' cards in the new deck, shuffled: ' . PHP_EOL . $deck . PHP_EOL . PHP_EOL;
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
// intialize four player hands
$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 )
{
{
// deal player cards, and sort them, using default sort
// sort player cards, default sort
$playerCards = $deck->deal( 4 )->sort();
$hand->sort();
// show player cards
// show player cards
echo 'Player ' . $p . ' got dealt the following ' . count( $playerCards ) . ' cards: ' . $playerCards . PHP_EOL;
echo 'Player ' . ( $p + 1 ) . ' got dealt the following ' . count( $hand ) . ' cards (sorted): ' . $hand . PHP_EOL;
}
}


Line 2,394: Line 2,440:


52 cards in the new deck, shuffled:
52 cards in the new deck, shuffled:
♥3 ♣2 ♥6 ♦A ♠J ♦K ♥T ♦3 ♦6 ♠6 ♥J ♥5 ♣J ♥4 ♥K ♠4 ♦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
♣7 ♠Q ♥9 ♠7 ♠J ♠9 ♣J ♣4 ♠8 ♥Q ♥2 ♦J ♠T ♣Q ♦A ♠K ♠2 ♣K ♦K ♦Q ♠3 ♣5 ♥5 ♠4 ♥3 ♦9 ♣2 ♦8 ♥A ♥6 ♦2 ♦5 ♥J ♣T ♦3 ♠5 ♠A ♣8 ♥8 ♥7 ♥K ♦7 ♥T ♦6 ♦T ♣6 ♣3 ♣A ♦4 ♥4 ♠6 ♣9


Player 1 got dealt the following 4 cards: ♣2 ♥3 ♥6 ♦A
Player 1 got dealt the following 7 cards (sorted): ♠2 ♣7 ♠8 ♠Q ♥Q ♦K ♣K
Player 2 got dealt the following 4 cards: ♦3 ♥T ♠J ♦K
Player 2 got dealt the following 7 cards (sorted): ♥2 ♠3 ♣5 ♠7 ♥9 ♦J ♦Q
Player 3 got dealt the following 4 cards: ♥5 ♠6 ♦6 ♥J
Player 3 got dealt the following 7 cards (sorted): ♥3 ♠4 ♥5 ♠9 ♠T ♠J ♣Q
Player 4 got dealt the following 4 cards: ♠4 ♥4 ♣J ♥K
Player 4 got dealt the following 7 cards (sorted): ♣2 ♣4 ♦8 ♦9 ♣J ♠K ♦A


36 cards remaining in the deck:
24 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}}==
=={{header|PicoLisp}}==