一副纸牌

一副纸牌

我最近一直在与 verbs 和 livewire 合作,并认为尝试创建一些我喜欢玩的纸牌游戏是一个有趣的实验。

为了促进这一点,我需要定义一副卡片,我可以在之后从事的任何项目中使用它。

一副牌需要包含 card、deck 和 cardcollection 类。一张牌应有花色和数值,一副牌应由 52 张牌组成。因为花色和数值都是为一副牌定义的,所以我可以使用枚举来表示牌的属性。

cardcollection 类允许我以 verbs 状态安全地存储卡片集合。

<?php // cards/enums/suit.php  declare(strict_types=1);  namespace cardsenums;  enum suit: string {     case clubs = 'clubs';     case diamonds = 'diamonds';     case hearts = 'hearts';     case spades = 'spades'; } 
登录后复制
<?php // cards/enums/value.php  declare(strict_types=1);  namespace cardsenums;  enum value: string {     case two = 'two';     case three = 'three';     case four = 'four';     case five = 'five';     case six = 'six';     case seven = 'seven';     case eight = 'eight';     case nine = 'nine';     case ten = 'ten';     case jack = 'jack';     case queen = 'queen';     case king = 'king';     case ace = 'ace'; } 
登录后复制
<?php // cards/card.php  declare(strict_types=1);  namespace cards;  use cardsenumssuit; use cardsenumsvalue;  final readonly class card {     public function __construct(         public suit $suit,         public value $value,     ) {} } 
登录后复制
<?php // cards/cardcollection.php  declare(strict_types=1);  namespace cards;  use illuminatesupportcollection; use symfonycomponentserializernormalizerdenormalizerinterface; use symfonycomponentserializernormalizernormalizerinterface; use thunkverbsserializedbyverbs;  class cardcollection extends collection implements serializedbyverbs {     public static function deserializeforverbs(mixed $data, denormalizerinterface $denormalizer): static     {         return static::make($data)             ->map(fn($serialized) =&gt; card::deserializeforverbs($serialized, $denormalizer));     }      public function serializeforverbs(normalizerinterface $normalizer): string|array     {         return $this-&gt;map(fn(card $card) =&gt; $card-&gt;serializeforverbs($normalizer))-&gt;tojson();     } } 
登录后复制
<?php // Cards/Deck.php  declare(strict_types=1);  namespace Cards;  use CardsEnumsSuit; use CardsEnumsValue;  final class Deck {     public CardCollection $cards;      public function __construct()     {         $this->cards = CardCollection::make([]);          collect(CardSuit::cases())             -&gt;each(function (CardSuit $suit): void {                 collect(CardValue::cases())                     -&gt;each(function (CardValue $value) use ($suit): void {                         $this-&gt;cards-&gt;push(new Card($suit, $value));                     });             });          $this-&gt;shuffle();     }      public function shuffle(): void     {         $this-&gt;cards = $this-&gt;cards             -&gt;shuffle()             -&gt;reverse();     }      public function deal(): ?Card     {         if (0 === $this-&gt;cards-&gt;count()) {             return null;         }          return $this-&gt;cards-&gt;pop();     }      public function remAIningCards(): int     {         return $this-&gt;cards-&gt;count();     } } 
登录后复制

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容