我想知道用分数创建一副纸牌或计算分数的最佳方法是什么?正如你将在下面看到的,我有一个计算课程来处理卡片分数,检查卡片是什么,给它一个值并计算胜利者。
我正在创建一个二十一点游戏,我已经获得了随机生成的卡片,但现在每张卡片的分数都有问题......
我只会向你展示我到目前为止的课程,这样你就可以了解我在做什么以及在哪里。
我知道这很多代码并不会给那些试图帮助的人带来痛苦,但请耐心等待我...
卡类
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;
}
}
}
提前致谢。
答案 0 :(得分:1)
不要使用简单的字符串。将它们放入枚举并创建一个不可变的类Card
,它将采用三个属性Suite
,Value
,Score
:
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();
}
}
答案 1 :(得分: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;
}
答案 2 :(得分:0)
也许最好为卡片值使用整数值,以便您可以直接使用它们进行评分。虽然对于ace,您必须在评分逻辑中将其定义为1或11。
然后使用ToString()将值1,1,11,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<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();
}
}