【问题标题】:What is the best way to have a score value for cards in a deck为一副牌中的卡片获得分值的最佳方法是什么
【发布时间】:2014-09-22 07:24:34
【问题描述】:

我想知道创建带有分数的一副牌或计算分数的最佳方法是什么?正如您将在下面看到的那样,我有一个计算类来处理卡片分数,检查卡片是什么并给它一个值并计算获胜者。

我正在创建一个二十一点游戏,我已经让卡片随机生成,但现在每张卡片的分数都有问题......

我只会向您展示我目前所上的课程,以便您了解我在做什么以及我在哪里。

我知道这是很多代码,对那些愿意尝试提供帮助但请尝试并忍受我的人来说并不好......

卡类

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

发牌员级别(这是发牌员将获得、洗牌和发牌的中心级别)

    class Dealer
{
    private List<string> randomisedCards;

    public Dealer()
    {
        randomisedCards = Card.CreateDeck();
    }


    public string dealCard()
    {
        string randCard = randomisedCards[0];
        randomisedCards.RemoveAt(0);


        // Creating the object.
        Calculate c = new Calculate(randCard);

        return randCard;
    }
}

播放器类

    class Player
{
    // Member variables.
    private int newPlayerScore;
    private int newPlayerWin;
    private int newPlayerLosses;
    private int newPlayerTies;


    // Defalut constructor used to set the member variables to their default values and will be used to reset
    // the players details.
    public Player()
    {
        newPlayerScore = 0;
        newPlayerWin = 0;
        newPlayerLosses = 0;
        newPlayerTies = 0;
    }

    // Propertie used to get and set the players score.
    public int calcPlayerScore
    {
        get
        {
            return newPlayerScore;
        }
        set
        {
            newPlayerScore = value;
        }
    }


    public void getPlayerDetails()
    {
        Calculate c = new Calculate(newPlayerScore);

    }
}

计算类(这是我用来计算每张牌的获胜者和得分的方法,这是我的问题,因为这不是最好的方法)

    class Calculate
{
    Player p = new Player();

    // Member variable.
    private string newCard;
    private int pScore;
    private int dScore;


    // Overloaded constructor.
    public Calculate(string card)
    {
        newCard = card;
    }

    public Calculate(int playerScore)
    {
        pScore = playerScore;
    }

    public void calculateScore()
    {
        switch (newCard)
        {
            case "Spades2":
            case "Hearts2":
            case "Diamonds2":
            case "Clubs2":
                pScore = 2;
                p.calcPlayerScore = pScore;
                break;

            case "Spades3":
            case "Hearts3":
            case "Diamonds3":
            case "Clubs3":
                pScore = 3;
                p.calcPlayerScore = pScore;
                break;

            case "Spades4":
            case "Hearts4":
            case "Diamonds4":
            case "Clubs4":
                pScore = 4;
                p.calcPlayerScore = pScore;
                break;

            case "Spades5":
            case "Hearts5":
            case "Diamonds5":
            case "Clubs5":
                pScore = 5;
                p.calcPlayerScore = pScore;
                break;

            case "Spades6":
            case "Hearts6":
            case "Diamonds6":
            case "Clubs6":
                pScore = 6;
                p.calcPlayerScore = pScore;
                break;

            case "Spades7":
            case "Hearts7":
            case "Diamonds7":
            case "Clubs7":
                pScore = 7;
                p.calcPlayerScore = pScore;
                break;

            case "Spades8":
            case "Hearts8":
            case "Diamonds8":
            case "Clubs8":
                pScore = 8;
                p.calcPlayerScore = pScore;
                break;

            case "Spades9":
            case "Hearts9":
            case "Diamonds9":
            case "Clubs9":
                pScore = 9;
                p.calcPlayerScore = pScore;
                break;

            default:
                pScore = 10;
                p.calcPlayerScore = pScore;
                break;
        }
    }
}

提前致谢。

【问题讨论】:

  • 也许创建一个结构并创建该结构类型的甲板?公共结构卡{字符串套件;整数值; }
  • 我想知道 - 由于您只有 52 个值,也许明确定义卡片和分数最终可能会带来更大的灵活性和性能?
  • 我不经常玩二十一点,但这张牌的花色与它的分数有什么关系吗? (我认为这只是数值)所以当你可以添加卡片值时,我看不到你从使用 switch 语句中获得了什么

标签: c#


【解决方案1】:

不要使用简单的字符串。将它们放入枚举中并创建一个不可变类Card,它将采用三个属性SuiteValueScore

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

有了这些东西,您可以创建所有想要的带有个人分数的卡片。

也许分数甚至不是卡片本身的属性,而是仅适用于持有所有卡片的玩家。如果一张牌的分数可以根据玩家持有的其他牌而改变(例如,黑杰克中的 A 可以是 1 或 11,但如果你有两张而不是 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();
    }
}

【讨论】:

    【解决方案2】:

    可能是这样的

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

    【讨论】:

      【解决方案3】:

      也许最好使用整数值作为卡片值,以​​便您可以直接使用它们进行评分。虽然对于 ace,您必须在得分逻辑中将其定义为 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;
                  }
              }
      
          }
      

      还制作了一个带有索引器的 CardDeck 类来访问您的卡片,如下所示:

      var deck = new CardDeck();
      var aCard = deck[0];
      

      还有IEnumerable&lt;Card&gt; 与这样的 Foreach 一起使用:

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

      【讨论】:

        猜你喜欢
        • 2017-06-17
        • 2023-03-20
        • 1970-01-01
        • 2016-05-21
        • 2010-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多