簡體   English   中英

c# 從數組中取出一個字符串並將其存儲到列表中

[英]c# Take a string from array and store it to list

這是主程序,我有 2 個類,即 Deck 和 Card。 所以我需要的是使用戶能夠從牌組中取出一張卡片並將其放入用戶手中,即 List 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();
    }
}

所以這是卡片類,我只是在這里聲明了一個 ovveride 字符串。

 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 類,這里有 Faces 和suits 的數組,這里還有shuffle 方法。

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;
    }
    }

}

代替List<string> userHand = new List<string>(); 將其聲明為List<Card> userHand = new List<Card>(); 然后讓我們像這樣重新定義甲板類:

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() 如果卡組為空(所有內容都已處理),您將GetCard()問題,因此它將卡在GetCard()內的while循環中,因此您需要檢查數組中的所有內容是否為空,如果是,則為空卡組拋出異常。

更好的方法是將數組轉換為List<Card>()因為它更容易為您操作(有Count()Remove()方法)

我認為這將有助於...我添加了一個方法,它隨機GetCardDeck然后顯示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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM