简体   繁体   中英

C# assigning value to array

I'm trying to write a program that would shuffle 13 playing cards from ace to king. Deal 2 cards out and add up the value that's assigned to each card. ace = 11, king = 10, jack = 10, queen = 10, ten = 10, nine = 9, eight = 8 and so on... sort of like blackjack.

so far I'm only able to shuffle the cards and print out two cards randomly but i don't know how to assign a value to each card, add them up and print it out. example, if my two random cards is King and Eight then i would want the program to print out..

King

Eight

18

here's what i got...

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

For a great example of programming a card game, do a google search for KarliCards from the book Beginning Visual C# 2012 (you can easily find a full PDF, but I won't post a link because I'm not sure if it's legal). It has lots of stuff like how to use regular operators (+) on things like classes or structs.

Anyway, what you're looking for is an enum. It's very similar to the Dictionary(string)(int) suggested by Rob (I'm not sure how to write triangle brackets). Here's an example of how it works:

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

My first language was Perl, so I tend to see everything as a struct instead of a class. There's always more than one way to do it....

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

Here's how I would do this:

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

Running this, I get (for example):

Two
Ten
12

Now, just for a bit more info on your existing code.

Calling Random random = new Random(); within a loop will result in many (if not all) of the random numbers being the same. You should use a single instance of Random only.

There is no need for the for (int x = 0; x < 100; x++) loop as a single pass of the for (int i = Cards.Length; i > 0; i--) loop is sufficient to randomize the cards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM