usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceCh10CardLib{publicenumRank{Ace=1,Deuce,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King,}pub...
using System;
using System.Collections.Generic;
using System.Text;
namespace Ch10CardLib
{ public enum Rank
{
Ace = 1,
Deuce,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Jack,
Queen,
King,
}
public enum Suit
{
Club,
Diamond,
Heart,
Spade,
}
public class Card
{
public readonly Ch10CardLib.Suit suit;
public readonly Ch10CardLib.Rank rank;
public Card(Suit newSuit,Rank newRank)
{
suit = newSuit;
rank = newRank;
}
private Card()
{
}
public override string ToString()
{
return "The" + rank + "of" + suit + "s";
}
}
public class Deck
{
private Ch10CardLib.Card[] cards;//这里面的cards是数组吗?如果是那Ch10CardLib.Card[] 是什么类型的?不是数组又表示什么呢?
public Deck()
{
cards = new Card[52];
for (int suitVal = 0; suitVal < 4; suitVal++)
{
for (int rankVal = 1; rankVal < 14; rankVal++)
{
cards[suitVal * 13 + rankVal - 1] = new Card((Suit)suitVal, (Rank)rankVal);//"suitVal * 13 + rankVal - 1"这个表达式是什么意思?如果cards[]是数组那么cards[]=new Card( ,)这个呢?
}
}
}
public Ch10CardLib.Card GetCard(int cardNum)
{
if (cardNum >= 0 && cardNum <= 51)
return cards[cardNum];
else
throw (new System.ArgumentOutOfRangeException("cardNum", cardNum, "Value must be between 0 and 51."));
}
public void Shuffle()
{
Card[] newDeck = new Card[52];
bool[] assigned = new bool[52];
Random sourceGen = new Random();
for (int i = 0; i < 52; i++)
{
int destCard = 0;
bool foundCard = false;
while (foundCard == false)
{
destCard = sourceGen.Next(52);
if (assigned[destCard] == false)
foundCard = true;
}
assigned[destCard] = true;
newDeck[destCard] = cards[i];
}
newDeck.CopyTo(cards, 0);
}
}
}
这是书上面的一个例题有些部分看不明白,请帮忙看看!谢谢!
展开