enums and collections - Organizing your data - Sort, HTML, row, col
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.11(Long Exercise)の解答を求めてみる。
コード
Index.razor
@page "/"
<dv class="container">
<div class="row">
<div class="col-5">
<div class="row">
<label for="deck1" 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 class="col-1" />
<div class="col-5">
<div class="row">
<label for="deck2" accesskey="w">
<strong>Deck T<span style="text-decoration: underline">w</span>o</strong>
</label>
<select @bind="twoDecks.RightCardSelected"
class="custom-select" size="10" id="deck2">
@for (int i = 0; i < twoDecks.RightDeckCount; i++)
{
<option value="@i">@twoDecks.RightDeckCardName(i)</option>
}
</select>
</div>
<div class="row">
<button class="btn btn-primary col-5 mt-2" accesskey="c"
@onclick="twoDecks.Clear">
<span style="text-decoration: underline">C</span>lear
</button>
</div>
<div class="row">
<button class="btn btn-primary col-5 mt-2" accesskey="t"
@onclick="twoDecks.Sort">
Sor<span style="text-decoration: underline">t</span>
</button>
</div>
<div class="row">
<h2>@twoDecks.RightDeckCardName(twoDecks.RightCardSelected)</h2>
</div>
</div>
</div>
</dv>
@code {
TwoDecks twoDecks = new TwoDecks();
}
コード
TwoDecks.cs
using System;
namespace TwoDecksBlazor
{
class TwoDecks
{
private Deck leftDeck = new Deck();
private Deck rightDeck = new Deck();
public int RightDeckCount
{
get { return rightDeck.Count; }
}
public int RightCardSelected { get; set; }
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();
}
public string RightDeckCardName(int index)
{
return rightDeck[index].ToString();
}
public void Sort()
{
rightDeck.Sort(new CardComparerByValue());
}
public void Clear()
{
rightDeck.Shuffle();
}
}
}
コード
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;
}
}
}