計算機科学のブログ

enums and collections - Organizing your data - List, Add, Clear, Shufle, Random

Head First C#: A Learner’s Guide to Real-World Programming with C# and .NET Core (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)のChapter 8(enums and collections - Organizing your data)、p.6(Long Exercise)の解答を求めてみる。

コード

Index.razor

@page "/"

<dv class="container">
    <div class="row">
        <div class="col-5">
            <div class="row">
                <label for="deck" accesskey="o">
                    <strong>Deck <span style="text-decoration: underline">O</span>ne</strong>
                </label>
                <select @bind="twoDecks.LeftCardSelected"
                        class="custom-select" size="10" id="deck1">
                    @for (int i = 0; i < twoDecks.LeftDeckCount; i++)
                    {
                        <option value="@i">@twoDecks.LeftDeckCardName(i)</option>
                    }
                </select>
            </div>
            <div class="row">
                <button class="btn btn-primary col-5 mt-2" accesskey="s"
                        @onclick="twoDecks.Shuffle">
                    <span style="text-decoration: underline">S</span>huffle
                </button>
            </div>
            <div class="row">
                <button class="btn btn-primary col-5 mt-2" accesskey="r"
                        @onclick="twoDecks.Reset">
                    <span style="text-decoration: underline">R</span>eset
                </button>
            </div>
            <div class="row">
                <h2>@twoDecks.LeftDeckCardName(twoDecks.LeftCardSelected)</h2>
            </div>
        </div>
    </div>
</dv>

@code {
    TwoDecks twoDecks = new TwoDecks();
}

コード

TwoDecks.cs

using System;
namespace TwoDecksBlazor
{
    class TwoDecks
    {
        private Deck leftDeck = new Deck();
        public int LeftDeckCount
        {
            get { return leftDeck.Count; }
        }
        public int LeftCardSelected { get; set; }
        public TwoDecks()
        {
        }
        public string LeftDeckCardName(int i)
        {
            return leftDeck[i].ToString();
        }
        public void Shuffle()
        {
            leftDeck.Shuffle();
        }
        public void Reset()
        {
            leftDeck = new Deck();
        }
    }
}

コード

Deck.cs

using System;
using System.Collections.Generic;

namespace TwoDecksBlazor
{
    class Deck: List<Card>
    {
        private static Random random = new Random();
        public Deck()
        {
            Reset();
        }
        public void Reset()
        {
            Clear();
            for (int suit = 0; suit < 4; suit++)
            {
                for (int value = 1; value < 14; value++)
                {
                    Add(new Card((Values)value, (Suits)suit));
                }
            }
        }
        public void Shuffle()
        {
            List<Card> cards = new List<Card>(this);
            Clear();
            while (cards.Count > 0)
            {
                int i = random.Next(cards.Count);
                Add(cards[i]);
                cards.RemoveAt(i);
            }
        }
        public Card Deal(int index)
        {
            Card card = base[index];
            RemoveAt(index);
            return card;
        }
    }
}