簡體   English   中英

C#將值分配給數組

[英]C# assigning value to array

我正在嘗試編寫一個程序,從王牌到王牌將13張撲克牌洗牌。 分發2張卡,然后將分配給每張卡的值相加。 ace = 11,國王= 10,傑克= 10,女王= 10,十= 10,九= 9,八個= 8等等...有點像二十一點。

到目前為止,我只能隨機洗牌並隨機打印出兩張卡,但是我不知道如何為每張卡分配值,將它們加起來並打印出來。 例如,如果我的兩張隨機卡分別是King和8張卡,那么我希望程序打印出來。

18歲

這就是我得到的...

        static void Main(string[] args)
    {
        string[] Cards = new string[] {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "jack", "Queen", "King", "Ace"};

        for (int x = 0; x < 100; x++) // looping the shuffle 100 times to maximize the randomness
        {
            for (int i = Cards.Length; i > 0; i--) //shuffle
            {
                string temp;
                Random random = new Random();
                int r = random.Next(i);
                temp = Cards[r];
                Cards[r] = Cards[i-1];
                Cards[i-1] = temp;
            }  
        }
        Console.WriteLine(Cards[0]); //first random card
        Console.WriteLine(Cards[1]); //second random card
        Console.ReadKey();
    }

有關編程紙牌游戲的絕佳示例,請在Google上從《 Beginning Visual C#2012》一書中搜索KarliCards(您可以輕松找到完整的PDF,但由於不確定其合法性,所以我不會發布鏈接) 。 它有很多東西,例如如何在類或結構之類的東西上使用常規運算符(+)。

無論如何,您正在尋找的是一個枚舉。 這與Rob建議的Dictionary(string)(int)非常相似(我不確定如何編寫三角括號)。 這是它如何工作的示例:

    enum CardValue
    {
        One = 1,
        Two = 2,
        Three = 3,
        Four = 4
    }

    static void Main(string[] args)
    {
        int myInt = (int)CardValue.One;
        Console.WriteLine("output should be 1: " + myInt);

        int mySum = (int)CardValue.One + (int)CardValue.Three;
        Console.WriteLine("output should be 4: " + mySum);
        Console.ReadKey();
    }

我的第一語言是Perl,所以我傾向於將所有內容視為結構而不是類。 總是有不止一種方法來做...。

    public enum CardSuits
    {
        Clubs,
        Spades,
        Hearts,
        Diamonds
    }

    public enum CardValues
    {
        Ace = 1,
        Two = 2,
        Three = 3,
        Four = 4
    }

    public struct Card
    {
        public CardValues Value; // Card.Value = CardValues.Ace
        public CardSuits Suit; // Card.Suit = CardSuits.Spades

        public override string ToString()
        {
            // Card.ToString() == "Ace of Spades"
            return String.Format(Value + " of " + Suit); 
        }

        public string ToStringAsInteger()
        {
            // Card.ToStringAsInteger() == "1 of Spades"
            return String.Format((int)Value + " of " + Suit); 
        }
    }

    static void Main(string[] args)
    {
        Card AceOfSpades = new Card();
        AceOfSpades.Value = CardValues.Ace;
        AceOfSpades.Suit = CardSuits.Spades;

        Card TwoOfClubs = new Card();
        TwoOfClubs.Value = CardValues.Two;
        TwoOfClubs.Suit = CardSuits.Clubs;

        int mySum = (int)AceOfSpades.Value + (int)TwoOfClubs.Value;
        Console.WriteLine("Sum of Ace (1) and Two (2) is: " + mySum); // should be 3
        Console.WriteLine("output of AceOfSpades.ToString() is: " + AceOfSpades.ToString());
        Console.WriteLine("output of AceOfSpades.ToStringAsInteger is: " + AceOfSpades.ToStringAsInteger());

        Console.ReadKey();
    }

這是我的處理方式:

var cards = new Dictionary<string, int>()
{
    { "Two", 2 }, { "Three", 3 }, { "Four", 4 }, { "Five", 5 }, { "Six", 6 },
    { "Seven", 7 }, { "Eight", 8 }, { "Nine", 9 }, { "Ten", 10 }, { "Jack", 10 },
    { "Queen", 10 }, { "King", 10 }, { "Ace", 11 },
};

var random = new Random();

var selected = cards.OrderBy(c => random.Next()).Take(2).ToArray();

foreach (var card in selected)
{
    Console.WriteLine(card.Key);
}

Console.WriteLine(selected.Sum(c => c.Value));

運行它,我得到(例如):

Two
Ten
12

現在,有關您現有代碼的更多信息。

調用Random random = new Random(); 循環中的結果將導致許多(如果不是全部)隨機數相同。 您應該只使用一個Random實例。

無需for for (int x = 0; x < 100; x++)循環,因為for (int i = Cards.Length; i > 0; i--)循環單次通過就足以使卡隨機化。

暫無
暫無

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

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