该代码随机抽取52张卡,在4个文本箱中各排13张,每个点击。 我谈一下它如何发挥作用的一般想法,但我需要帮助消除一些混乱或了解它是如何运作的。 我对我不敢肯定的话提出了大胆的评论。
namespace Cards
{
enum Value { Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace}
}
namespace Cards
{
enum Suit { Clubs, Diamonds, Hearts, Spades }
}
namespace Cards
{
class PlayingCard
{
private readonly Suit suit;
private readonly Value value;
public PlayingCard(Suit s, Value v)
{
this.suit = s;
this.value = v;
}
public override string ToString()
{
string result = string.Format("{0} of {1}", this.value, this.suit);
return result;
}
public Suit CardSuit() // **Not sure why this is here. Maybe for future use?**
{
return this.suit;
}
public Value CardValue() // **Same as above**
{
return this.value;
}
}
}
namespace Cards
{
using System;
using System.Collections;
class Pack // **Data Access Layer?**
{
public const int NumSuits = 4;
public const int CardsPerSuit = 13;
private PlayingCard[,] cardPack;
private Random randomCardSelector = new Random();
public Pack() //**Storing all elements**
{
this.cardPack = new PlayingCard[NumSuits, CardsPerSuit]; //
for (Suit suit = Suit.Clubs; suit <= Suit.Spades; suit++) //
{
for (Value value = Value.Two; value <= Value.Ace; value++)
{
this.cardPack[(int)suit, (int)value] = new PlayingCard(suit, value);
}
}
}
public PlayingCard DealCardFromPack() // **Purpose: To dealing unique cards**
{
Suit suit = (Suit)randomCardSelector.Next(NumSuits); // **picks random 0-3 from Suit**
while (this.IsSuitEmpty(suit)) // **Purpose: Checks if empty but don t know how it works**
{
suit = (Suit)randomCardSelector.Next(NumSuits);
}
Value value = (Value)randomCardSelector.Next(CardsPerSuit);
while (this.IsCardAlreadyDealt(suit, value)) // ?
{
value = (Value)randomCardSelector.Next(CardsPerSuit);
}
PlayingCard card = this.cardPack[(int)suit, (int)value];
this.cardPack[(int)suit, (int)value] = null; // **sets the current element to null so it isn t reused.**
return card;
}
private bool IsSuitEmpty(Suit suit) // **checks if empty**
{
bool result = true;
for (Value value = Value.Two; value <= Value.Ace; value++) //**Checks Null or not**
{
if(!IsCardAlreadyDealt(suit, value))
{
result = false;
break;
}
}
return result;
}
private bool IsCardAlreadyDealt(Suit suit, Value value) //**returns current element null?**
{
return (this.cardPack[(int)suit, (int)value] == null);
}
}
}
namespace Cards
{
using System;
using System.Collections;
class Hand // **Business layer?**
{
public const int HandSize = 13;
private PlayingCard[] cards = new PlayingCard[HandSize]; // **single array? Thought it had to be 2 because of 2 parameters?**
private int playingCardCount = 0;
public void AddCardToHand(PlayingCard cardDealt)//**Confusion when class used as variable**
{
if (this.playingCardCount >= HandSize)
{
throw new ArgumentException("Too many cards");
}
this.cards[this.playingCardCount] = cardDealt; //**Confused...cardDealt value going in card[]? How does that work..**
this.playingCardCount++;//**incrementing, how is this helpful**
}
public override string ToString() //to show all 13 cards in hand
{
string result = "";
foreach (PlayingCard card in this.cards)
{
result += card.ToString() + "
";
}
return result;
}
}
}
namespace Cards
{
public partial class Game : Window
{
public const int NumHands = 4;
private Pack pack = null;
private Hand[] hands = {new Hand(), new Hand(), new Hand(), new Hand()}; //**Creates 4 different list all single array?**
public Game()
{
InitializeComponent();
}
private void dealClick(object sender, RoutedEventArgs e)
{
try
{
pack = new Pack();
for (int handNum = 0; handNum < NumHands; handNum++) //
{
hands[handNum] = new Hand();
for (int numCards = 0; numCards < Hand.HandSize; numCards++)
{
PlayingCard cardDealt = pack.DealCardFromPack(); //**Deals 13 random cards into each array. Don t understand how it worked though.**
hands[handNum].AddCardToHand(cardDealt);
}
}
north.Text = hands[0].ToString();
south.Text = hands[1].ToString();
east.Text = hands[2].ToString();
west.Text = hands[3].ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}