2014-09-22 91 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(); 
     } 
    } 
相關問題