从数组中取出一个字符串并存储到list中



这是主程序,我有两个类Deck和Card。我需要的是允许用户从牌组中取出一张牌并将其放入用户手中也就是列表userHand

class Program
{
static void Main(string[] args)
{

List<Card> userHand = new List<Card>();
// Where I am suppose to enter this **Card drawnCard = deck1.GetCard();**

Deck deck1 = new Deck();
for(int i = 0; i < 52; i++)
{
Console.WriteLine("{0,-19}", deck1.GetCard());
if ((i + 1) % 4 == 0)
Console.WriteLine();
}
Console.ReadLine();
}
}

这是Card Classes,我只是声明了一个oververide字符串

class Card
{
private string face;
private string suit;
public Card(string cardFace, string cardSuit)
{
face = cardFace;
suit = cardSuit;
}
public override string ToString()
{
return face + " of " + suit;
}
}

最后是Deck类,这里是牌面和花色的数组,这里还有洗牌方法

class Deck
{
private Card[] deck;
private int currentCard;
private const int numberofCards = 52;
private Random ranNum;
public Deck()
{
string[] faces = { "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10","Jack", "Queen", "King"};
string[] suits = { "Clubs", "Hearts", "Spades", "Diamonds" };
deck = new Card[numberofCards];
currentCard = 0;
ranNum = new Random();
for (int count = 0; count < deck.Length; count++)
deck[count] = new Card(faces[count % 13], suits[count / 13]);
}
public void Shuffle()
{
currentCard = 0;
for (int i = 0; i < deck.Length; i++)
{
int j = ranNum.Next(numberofCards);
Card temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
public Card DealCard()
{
if (currentCard < deck.Length)
return deck[currentCard++];
else
return null;
}
}
}

我想这会有帮助…我增加了一种方法,从Deck中随机抽取GetCard,然后显示userHand和剩余的卡。

class Program
{
private static int userCards = 0;
private static string[] userhand;
private static Deck deck1 = new Deck();
static void Main(string[] args)
{
string[] faces = { "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10","Jack", "Queen", "King"};
for (int i = 0; i < 52; i++)
{
deck1.DealCard();
Console.WriteLine(deck1.deck[i]);
if ((i + 1) % 13 == 0)
Console.WriteLine();
}
deck1.Shuffle();
Console.WriteLine("----------------Shuffle cards----------------");
for (int i = 0; i < 52; i++)
{
Console.WriteLine(deck1.deck[i]);
}
Console.WriteLine("Enter how many cards you want to pick");
userCards = Convert.ToInt32(Console.ReadLine());
userhand = new string[userCards];
for (int i = 0; i < userCards; i++)
{
GetCard(i);
}
Console.WriteLine("----------------User Hand----------------");
for (int i = 0; i < userhand.Length; i++)
{
Console.WriteLine(userhand[i]);
}
deck1.Sort();
Console.WriteLine("--------------Remaining Cards-------------");
for (int i = 0; i < 52 - userhand.Length; i++)
{
Console.WriteLine(deck1.sortedlist[i]);
}
Console.ReadLine();
}
public static void GetCard(int i)
{
userhand[i] = deck1.deck[0].ToString();
deck1.deck = deck1.deck.Where((source, index) => index != 0).ToArray();
}
}

卡片类将是这个…

class Card
{
private string face;
private string suit;
public Card(string cardFace, string cardSuit)
{
face = cardFace;
suit = cardSuit;
}
public override string ToString()
{
return face + " of " + suit;
}
}

甲板类…

class Deck
{
public Card[] deck;
public List<string> sortedlist;
private int currentCard;
private const int numberofCards = 52;
private Random ranNum;
public Deck()
{
string[] faces = { "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10","Jack", "Queen", "King"};
string[] suits = { "Clubs", "Hearts", "Spades", "Diamonds" };
deck = new Card[numberofCards];
currentCard = 0;
ranNum = new Random();
for (int count = 0; count < deck.Length; count++)
deck[count] = new Card(faces[count % 13], suits[count / 13]);
}
public void Sort()
{
sortedlist = deck.Select(i => i.ToString()).ToList();
sortedlist.Sort();
}
public void Shuffle()
{
for (int i = deck.Length - 1; i > 0; i--)
{
int j = ranNum.Next(i + 1);
Object temp = deck[i];
deck[i] = deck[j];
deck[j] = (Card)temp;
}
}
public Card DealCard()
{
if (currentCard < deck.Length)
return deck[currentCard++];
else
return null;
}
}

List<string> userHand = new List<string>();声明为List<Card> userHand = new List<Card>();,然后像这样重新定义deck类:

class Deck
{
private Card[] deck;
private const int nCards = 52;
private Random rnd;
public Deck()
{
string[] faces = { "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10","Jack", "Queen", "King"};
string[] suits = { "Clubs", "Hearts", "Spades", "Diamonds" };

deck = new Card[numberofCards];

rnd = new Random();

for (int count = 0; count < deck.Length; count++)
deck[count] = new Card(faces[count % 13], suits[count / 13]);
}

public Card GetCard()
{
int i = -1;
Card selectedCard = null;

while(selectedCard == null)
{
i = rnd.Next(deck.Length);
selectedCard = deck[i];
}

deck[i] = null;
return selectedCard;
}
}

现在简单的deck.GetCard()。如果deck是空的(一切都被处理),你会有问题,所以它会在GetCard()中的while循环中卡住,所以你需要检查数组内的所有内容是否为空,如果是空甲板抛出异常。

更好的方法是将数组转换为List<Card>(),因为它更容易操作(有Count()Remove()方法)