2014-09-22 95 views
0

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

林在具有比分为每张卡的麻烦创建二十一点游戏和我得到的牌随机生成,但现在......

我只是要告诉你我有到目前为止类你可以感受到我在做什么和在什么地方。

我知道,这是很多代码是不生病的人谁都会尽量帮助,但请尽量和我一起承担......

卡类

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

Player类

中心类
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; 
     } 
    } 
} 

谢谢。

+0

也许创建一个结构,并创建一个结构类型的平台?公共结构卡 { string suite; int value; } – Pavenhimself 2014-09-22 07:37:35

+0

我想知道 - 因为你只有52个值,可能明确定义卡片和分数可能最终导致更多的灵活性和性能? – kidshaw 2014-09-22 07:51:25

+0

我不会经常玩二十一点,但是这张卡的套装是否与其分数有关? (我认为这只是数值),所以我没有看到你从使用switch语句获得什么,只需添加卡片值 – Sayse 2014-09-22 07:53:08

回答

0

也许这样的事情

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

这个东西在手,你可以用个人得分创造一切需要的牌。

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

也许它能够更好地使用整数值的卡值以便您可以直接使用它们进行评分。尽管对于ace来说,你必须在你的得分逻辑中将它定义为1或11。

然后使用toString()来显示的值1,11,12,13度Acc,千斤顶,后,王

卡的结构:

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<Card>有这样一个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(); 
     } 
    } 
相关问题