如何验证值是否在数组中 [英] how to validate if the value is in the array

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

问题描述

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



任何帮助将不胜感激



这是我需要使用的表格

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

推荐答案

请注意,此处显示的代码使用自动属性支持-field创建从C#3.0(2007)开始实现。



通过将Deck定义为继承自List< Card>我们(基本上)创建一个自定义的卡片列表,我们可以用我们需要的任何东西扩展自定义列表的功能:在这种情况下,我们所做的就是添加一个'随机播放方法。



通过使用'枚举,您可以让您的代码表达您对卡片组建模的意图。



通过使用属性私有'setter,你可以确保只有Card实例的构造函数可以设置属性,但是public'getter允许从代码中任何地方访问Value,它引用了Deck的实例。
Note the code shown here uses "automatic" Property backing-field creation as implemented beginning in C# 3.0 (2007).

By defining a Deck as inheriting from a List<Card> we (essentially) create a custom List of Cards, and we can extend the functionality of that "custom List" with whatever we require: in this case, all we do is add a 'Shuffle method.

By use of 'Enums you can make your code express your intent to model a Deck of Cards.

By use of Properties with a private 'setter, you can ensure that only the constructor of an instance of a Card can set the property, but the public 'getter allows access to the Value from anywhere in your code that has a reference to the instance of the Deck.
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);
}


错误的问题,无需按照您的预期回答。不仅这样的索引搜索会影响性能,但也绝对不需要。



你的错误是字段 String _value; 你永远不需要它。此外,相关的问题是:将字段作为实例字段是不好的;它应该是静态只读字段。实际上,您不希望在所有实例中携带此数组对象,因为它是所有实例共享的对象。



修复你的类的设计,将值声明为byte。首先,这种类型作为整数类型,便于所有计算。其次,它将使您免于毫无意义的搜索。你唯一的问题是找到卡的字符串值(让我称之为名称,或者你可以使用名称和短名称,这是一个简单的数组中的索引。所以,它应该是这样的:

Wrong question, no need to answer it the way you expect. Not only such search of the index would compromise performance, but it's also absolutely not needed.

Your mistake is having the field String _value; you never need it. Besides, related problem is: it's bad to have the field Values as the instance field; it should really be static read-only field. Indeed, you don't want to carry this array object with all of your instances, because it is the object to be shared by all your instances.

To fix the design of you class, declare value as byte. First of all, this type, as an integer type, is convenient for all calculations. Second of all, it will save you from the pointless search. Your only problem is finding the string value of the card (let me call it "Name", or you could have "Name" and "ShortName", and this is a simple as the index in the array Values. So, it should be something like that:
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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆