本文介绍了如何验证值是否在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用控制台应用程序制作一个二十一点游戏,我开始制作我的卡类,但我不确定如何验证设置到卡的值是否在我的阵列中



任何帮助将不胜感激



这是我需要使用的表格

I am making a blackjack game using console application and I started to make my card class but I am not sure on how to validate if the value set to the card is in my array

Any help would be appreciated

Here is the table I need to use

String[] values = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};





这是我到目前为止的卡类





Here is my card class so far

class Card
    {

        public enum SUIT { HEARTS, SPADES, DIAMONDS, CLUBS };

        private SUIT _suit;
        private String _value;

        public Card(SUIT suit, String value)
        {

            _suit = suit;
            _value = value;
        }
        String[] values = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
        public SUIT Suit
        {
            get
            {
                //Return member variable value
                return _suit;
            }
            set
            {
                _suit = value;
            }
        }
       
        public String Value 
        {
            get
            {
                //Return member variable value
                return _value;
            }
            set
            {
                int index = Array.IndexOf(values, Convert.ToInt32(_value));
                if (index > 1)
                    _value = value;
            }
        }
    }

推荐答案

public enum SuitType
{
    Hearts, Diamonds, Clubs, Spades, Jokers
}

// note the Enum value 'Joker here: this is not used in this example
// but is there to enable another type of Deck definition
// ... the "US deck," with Jokers, for example

public enum CardValueType
{
    Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Joker
}

public class Card
{
    // note the private 'set
    public SuitType Suit { private set; get; }
    public CardValueType Value { private set; get; }

    public Card(SuitType suit, CardValueType value)
    {
        Suit = suit;
        Value = value;
    }
}

// note inheritance from List<Card>
public class Deck : List<Card>
{
    public void ShuffleDeck()
    {
        var rnd = new Random(DateTime.Now.Millisecond);
        
        this.OrderBy(order => rnd.Next());
    }
}

// "Classic" or "French" Deck
// four Suits of 13 cards each.

// because we inherit from 'Deck:
// this class also will behave as a List<Card>
public class ClassicDeck : Deck
{
    public ClassicDeck()
    {
        for (int i = 0; i < 4; i++)
        {
            var suit = (SuitType) i;

            for (int j = 0; j < 13; j++)
            {
               this.Add(new Card(suit, (CardValueType)j));
            }
        }
    }
}

// test: put these in a Form's code

// requires Two TextBoxes on the Form, 'textBox1, 'textBox2
// requires a Button on the Form, 'btnTestDeck with its 'Click EventHandler
// connected to the EventHandler shown here

private ClassicDeck TheCurrentDeck;

private void TestDeck(Deck theDeck)
{
    foreach (Card card in theDeck)
    {
        textBox1.AppendText(string.Format("{0}\t{1}\n", card.Value, card.Suit));
    }

    TheCurrentDeck.ShuffleDeck();

    foreach (Card card in theDeck)
    {
        textBox2.AppendText(string.Format("{0}\t{1}\n", card.Value, card.Suit));
    }
}

// run the app, click the button
private void btnTestDeck_Click(object sender, EventArgs e)
{
    textBox1.Clear();
    textBox2.Clear();
    
    TheCurrentDeck = new ClassicDeck();
    
    TestDeck(TheCurrentDeck);
}


class Card {

    public string Name { get { return names[cardValue]; } }

    // ...

    static readonly string[] names = new string[] {
        "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", };
    byte cardValue;

    // ...

}



我相信你永远不需要一个接受卡片名称(短或完整)的构造函数,你只需要它的数值。这些字符串值仅用于在屏幕上显示游戏,因此仅在计算输出时需要。



我必须注意你显示的很常见但是这些天许多初学者的悲惨谬误:尝试使用代表数据而不是实际数据的字符串。我会建议你摆脱这种习惯。 :-)



-SA


这篇关于如何验证值是否在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 12:08