Skip to content

Instantly share code, notes, and snippets.

@DanielCoulbourne
Created June 17, 2023 12:44
Show Gist options
  • Save DanielCoulbourne/1ca5da858d0d5d025a5d0dae0abdbdd2 to your computer and use it in GitHub Desktop.
Save DanielCoulbourne/1ca5da858d0d5d025a5d0dae0abdbdd2 to your computer and use it in GitHub Desktop.
DealCardsTest
<?php
namespace Tests\Feature;
use Illuminate\Support\Collection;
use Tests\TestCase;
class DealCardsTest extends TestCase
{
protected function genrateShuffledDeck($include_jokers = false)
{
$suits = collect(['♠️', '♥️', '♦️', '♣️']);
$values = collect(range(2, 10))->merge(['J', 'Q', 'K', 'A']);
return $suits->crossJoin($values)
->map(fn ($card) => "$card[0] $card[1]")
->when($include_jokers, fn ($deck) => $deck->merge(['Joker', 'Joker']))
->shuffle();
}
public function deal(Collection $deck, int $hands, ?int $hand_limit = null)
{
$remainder = $hand_limit
? $deck->splice($hand_limit * $hands)
: collect();
$hands = collect(range(0, $hands - 1))
->map(fn ($hand_number) => $deck->nth($hands, $hand_number));
return [$hands, $remainder];
}
public function dealHoldEm(int $player_count)
{
$deck = $this->genrateShuffledDeck();
[$hands, $deck] = $this->deal($deck, $player_count, 2);
[[$flop], $deck] = $this->deal($deck, 1, 3);
[[$turn], $deck] = $this->deal($deck, 1, 1);
[[$river], $deck] = $this->deal($deck, 1, 1);
return [
$hands,
$flop,
$turn,
$river,
];
}
public function test_six_max_holdem()
{
[$hands, $flop, $turn, $river] = $this->dealHoldEm(6);
dump('Hands: ', $hands->toArray());
dump('Flop: ', $flop->toArray());
dump('Turn: ', $turn->toArray());
dump('River: ', $river->toArray());
$this->assertCount(6, $hands);
$this->assertCount(3, $flop);
$this->assertCount(1, $turn);
$this->assertCount(1, $river);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment