Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在牌组中获得牌的得分值的最佳方法是什么_C# - Fatal编程技术网

C# 在牌组中获得牌的得分值的最佳方法是什么

C# 在牌组中获得牌的得分值的最佳方法是什么,c#,C#,我想知道最好的方法是创建一副有分数的牌,还是计算分数?正如你将在下面看到的,我有一个计算类来处理卡片分数,检查卡片是什么,给它一个值,并计算赢家 我正在创建一个21点游戏,我已经得到了随机生成的卡,但现在在每个卡的分数有麻烦 我只想给你们看一下我目前的课程,这样你们就可以了解我在做什么,我在哪里 我知道这是一个很大的代码一个是没有生病的人谁会尝试和帮助,但请尝试和忍受我 卡片类 public static class Card { // Should be enumes eg.

我想知道最好的方法是创建一副有分数的牌,还是计算分数?正如你将在下面看到的,我有一个计算类来处理卡片分数,检查卡片是什么,给它一个值,并计算赢家

我正在创建一个21点游戏,我已经得到了随机生成的卡,但现在在每个卡的分数有麻烦

我只想给你们看一下我目前的课程,这样你们就可以了解我在做什么,我在哪里

我知道这是一个很大的代码一个是没有生病的人谁会尝试和帮助,但请尝试和忍受我

卡片类

     public static class Card
{
    // Should be enumes eg. Privat enume suite{}
    private static string[] Suite = new string[4] {"Clubs", "Hearts", "Spades", "Diamonds" };
    private static string[] FaceValue = new string[13] {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };


    public static List<string> CreateDeck()
    {
        List<string> deckOfCards = new List<string>();

        for (int s = 0; s < 4; s++ )
        {
            string sut = Suite[s];

            for (int fV = 0; fV < 13; fV++)
            {
                string value = FaceValue[fV];

                deckOfCards.Add(sut + value);
            }
        }
        // End of For loop.
        deckOfCards.Shuffle();

        return deckOfCards;
    }

    public static void Shuffle<T>(this IList<T> list)
    {
        Random rng = new Random();
        int n = list.Count;
        while (n > 1)
        {
            n--;
            int k = rng.Next(n + 1);
            T value = list[k];
            list[k] = list[n];
            list[n] = value;
        }
    }
}
计算等级(这是我用来计算每张卡的赢家和分数的方法,这是我的问题,因为这不是最好的方法)


提前谢谢。

也许是这样的

    public struct Card
    {
        public string suite;
        public string value;
    }

    private static string[] Suite = new string[4] { "Clubs", "Hearts", "Spades", "Diamonds" };
    private static string[] FaceValue = new string[13] { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };

    public static List<Card> CreateDeck()
    {
        List<Card> deckOfCards = new List<Card>();

        for (int s = 0; s < Suite.Length; s++)
        {
            string sut = Suite[s];

            for (int fV = 0; fV < FaceValue.Length; fV++)
            {
                Card myCard;

                myCard.suite = sut;
                myCard.value = FaceValue[fV];

                deckOfCards.Add(myCard);
            }
        }
        // End of For loop.
        deckOfCards.Shuffle();

        return deckOfCards;
    }
公共结构卡
{
公共字符串组;
公共字符串值;
}
私有静态字符串[]套件=新字符串[4]{“梅花”、“红心”、“黑桃”、“钻石”};
私有静态字符串[]FaceValue=新字符串[13]{“Ace”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”、“Jack”、“Queen”、“King”};
公共静态列表CreateDeck()
{
List DECKOFCARD=新列表();
对于(int s=0;s
不要使用简单的字符串。将它们放入枚举中,并创建一个不可变的类
,该类将包含三个属性
套件
分数

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

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

public class Card
{
    public Card(Suite suite, Value value, decimal score)
    {
        Suite = suite;
        Value = value;
        Score = score;
    }

    public Suite Suite { get; private set; }

    public Value Value { get; private set; }

    public decimal Score { get; private set; }
}
有了这些东西,你就可以用一个单独的分数创建所有想要的卡片

也许分数甚至不是卡本身的属性,但只适用于持有所有卡的玩家。如果一张牌的分数可以根据玩家持有的其他牌的不同而变化(例如,黑杰克中的王牌可以是一张或十一张,但如果你有两张,那么它就等于21张),这是有意义的。因此,在最后一种情况下,您可能应该为您喜欢的游戏创建一个静态类,该类能够为您提供所需的一切:

public static class BlackJack
{
    public static IEnumerable<Card> CreateNewDeck()
    {
        var suits = Enum.GetValues(typeof(Suite)).Cast<Suite>();
        var values = Enum.GetValues(typeof(Value)).Cast<Value>();

        // Create a new deck that contains all cards as often as needed.
        var simpleDeck = suits.SelecMany(suit => values.Select(value => new Card(suit, value)));
        // E.g. in one BlackJack deck you'll find all cards four times (IMHO).
        var deck = Enumerable.Repeat(simpleDeck, 4).SelectMany(x => x);
                             .ToList();

        deck.Shuffle();
        return deck;
    }

    public static decimal ComputeScore(IEnumerable<Card> playerCards)
    {
        // Iterate through all cards the player is currently holding
        // and tell the current player score.
        throw new NotImplementException();
    }
}
公共静态类21点
{
公共静态IEnumerable CreateNewDeck()
{
var=Enum.GetValues(typeof(Suite)).Cast();
var values=Enum.GetValues(typeof(Value)).Cast();
//根据需要创建一个包含所有卡片的新牌组。
var simpleDeck=suits.SelecMany(suit=>values.Select(value=>newcard(suit,value));
//例如,在一个21点牌组中,你会发现所有牌四次(IMHO)。
var deck=Enumerable.Repeat(simpleDeck,4)。选择many(x=>x);
.ToList();
洗牌();
返回甲板;
}
公共静态十进制计算核心(IEnumerable playerCards)
{
//迭代玩家当前持有的所有牌
//并告诉当前玩家的分数。
抛出新的NotImplementException();
}
}

也许对卡片值使用整数值更好,这样您就可以直接使用它们进行评分。虽然对于王牌,你必须在得分逻辑中将其定义为1或11

然后使用ToString()将值1、11、12、13显示为Ace、jack、Queen、King

卡片结构:

public struct Card
    {
        public int value;
        public string suite;

        public Card(int value, string suite)
        {
            this.value = value;
            this.suite = suite;
        }

        public override string ToString()
        {
            switch (value)
            {
                case 1:
                    return "Ace of " + suite;
                case 11:
                    return "Jack of " + suite;
                case 12:
                    return "Queen of " + suite;
                case 13:
                    return "King of " + suite;
                default:
                    return value + " of " + suite;
            }
        }

    }
还使用indexer创建了一个CardDeck类来访问您的卡,如下所示:

var deck = new CardDeck();
var aCard = deck[0];
Foreach(Card card in Deck)
{

}
IEnumerable
与类似的Foreach一起使用:

var deck = new CardDeck();
var aCard = deck[0];
Foreach(Card card in Deck)
{

}
CardDeck级别:

    public class CardDeck : IEnumerable<Card>
    {
        private List<Card> _cards;

        public Card this[int index] { get { return _cards[index]; } private set; }

        public CardDeck()
        {
            string[] suits = new string[4] {"Clubs", "Hearts", "Spades", "Diamonds" };
            int[] values = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };

            _cards = new List<Card>();

            // Populate deck
            for (int i = 0; i < values.Length; i++)
            {
                int value = values[i];

                for (int x = 0; i < suits.Length; x++)
                {
                    _cards.Add(new Card(value, suits[x]));
                }
            }
        }

        public void RemoveAt(int index)
        {
            _cards.RemoveAt(index);             
        }

        public void Shuffle()
        {
            // shuffle based on http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
            Random rng = new Random();
            int n = _cards.Count;
            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                var value = _cards[k];
                _cards[k] = _cards[n];
                _cards[n] = value;
            }  
        }

        public IEnumerator<Card> GetEnumerator()
        {
            for (int index = 0; index < _cards.Count; index++)
            {
                yield return _cards[index];
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
公共类卡片组:IEnumerable
{
私人名单卡;
公共卡此[int索引]{get{return}卡[index];}私有集;}
公共卡片组()
{
字符串[]套装=新字符串[4]{“梅花”、“红心”、“黑桃”、“钻石”};
int[]值=新的int[]{1,2,3,4,5,6,7,8,9,10,11,12,13};
_卡片=新列表();
//填充甲板
for(int i=0;i1)
{
n--;
int k=下一个(n+1);
var值=_卡[k];
_卡片[k]=_卡片[n];
_卡片[n]=价值;
}  
}
公共IEnumerator GetEnumerator()
{
对于(int index=0;index<\u cards.Count;index++)
{
收益率回报卡[指数];
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
}

是否可以创建一个结构并创建该结构类型的数据组?public struct card{string suite;int value;}我想知道-既然你只有52个值,那么明确定义卡片和分数可能最终会带来更大的灵活性和性能?我不经常玩21点,但卡片的花色是否与其分数相关?(我以为这只是数值)所以我看不出使用开关st有什么好处
    public class CardDeck : IEnumerable<Card>
    {
        private List<Card> _cards;

        public Card this[int index] { get { return _cards[index]; } private set; }

        public CardDeck()
        {
            string[] suits = new string[4] {"Clubs", "Hearts", "Spades", "Diamonds" };
            int[] values = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };

            _cards = new List<Card>();

            // Populate deck
            for (int i = 0; i < values.Length; i++)
            {
                int value = values[i];

                for (int x = 0; i < suits.Length; x++)
                {
                    _cards.Add(new Card(value, suits[x]));
                }
            }
        }

        public void RemoveAt(int index)
        {
            _cards.RemoveAt(index);             
        }

        public void Shuffle()
        {
            // shuffle based on http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
            Random rng = new Random();
            int n = _cards.Count;
            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                var value = _cards[k];
                _cards[k] = _cards[n];
                _cards[n] = value;
            }  
        }

        public IEnumerator<Card> GetEnumerator()
        {
            for (int index = 0; index < _cards.Count; index++)
            {
                yield return _cards[index];
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }