首页 >后端开发 >php教程 >一副纸牌

一副纸牌

Linda Hamilton
Linda Hamilton原创
2024-11-03 21:52:03654浏览

A deck of cards

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

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

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

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

<?php
// Cards/Enums/Suit.php

declare(strict_types=1);

namespace Cards\Enums;

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 Cards\Enums;

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 Cards\Enums\Suit;
use Cards\Enums\Value;

final readonly class Card
{
    public function __construct(
        public Suit $suit,
        public Value $value,
    ) {}
}
<?php
// Cards/CardCollection.php

declare(strict_types=1);

namespace Cards;

use Illuminate\Support\Collection;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Thunk\Verbs\SerializedByVerbs;

class CardCollection extends Collection implements SerializedByVerbs
{
    public static function deserializeForVerbs(mixed $data, DenormalizerInterface $denormalizer): static
    {
        return static::make($data)
            ->map(fn($serialized) => Card::deserializeForVerbs($serialized, $denormalizer));
    }

    public function serializeForVerbs(NormalizerInterface $normalizer): string|array
    {
        return $this->map(fn(Card $card) => $card->serializeForVerbs($normalizer))->toJson();
    }
}
<?php
// Cards/Deck.php

declare(strict_types=1);

namespace Cards;

use Cards\Enums\Suit;
use Cards\Enums\Value;

final class Deck
{
    public CardCollection $cards;

    public function __construct()
    {
        $this->cards = CardCollection::make([]);

        collect(CardSuit::cases())
            ->each(function (CardSuit $suit): void {
                collect(CardValue::cases())
                    ->each(function (CardValue $value) use ($suit): void {
                        $this->cards->push(new Card($suit, $value));
                    });
            });

        $this->shuffle();
    }

    public function shuffle(): void
    {
        $this->cards = $this->cards
            ->shuffle()
            ->reverse();
    }

    public function deal(): ?Card
    {
        if (0 === $this->cards->count()) {
            return null;
        }

        return $this->cards->pop();
    }

    public function remainingCards(): int
    {
        return $this->cards->count();
    }
}

以上是一副纸牌的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn