【发布时间】:2020-08-12 21:19:37
【问题描述】:
我目前正在编写 BlackJack 游戏,现在我在 HAND 的 Sprint 中,问题是 Hand 有 HandVALUE 和 HandRATING。此值或等级代表 Handsproperties 或字段。问题是(您将在代码中更多地了解它)当我在 Console.NetFramework(UI)中创建一个新 HAND 并键入 Console.WriteLine(Console.WriteLine("The Value of the hand is: " + hand. HandValue); 或 Console.WriteLine("The Ratings of the hand is: " + hand.HandRating); 他们总是 0 和 None。0 = HandValue 和 None 是 HandRating。但他们应该有其他的值和评级,因为 (正如您将在代码中看到的那样)有 3 张牌,每张牌的价值为 7 = HANDVALUE : 21,HandRating 应该是 TripleSeven(因为我们有 3 张 7 值牌)。 应该使用属性并且应该设置 HandValue 和 HandRating。 (代码在这里显示得非常简短。如果我留下一些东西,没有人会理解问题和代码结构)
Console.WriteLine("HAND - TESTS");
Hand hand = new Hand();
Console.WriteLine("First, output of the cards in the hand at the beginning. If their is nothing to see, the test" +
"is positive.");
ICard card3 = new Card(CardValue.Seven, CardSuit.Heart);
ICard card4 = new Card(CardValue.Seven, CardSuit.Spade);
ICard card5 = new Card(CardValue.Seven, CardSuit.Diamond);
Console.WriteLine("");
Console.WriteLine("Press <ENTER> to add one card to the hand:");
Console.ReadLine();
hand.AddCard(card3);
Console.WriteLine("If there is only one card ---> then this hand function is working.");
hand.CardsInHandConsole();
Console.WriteLine("Press <ENTER> to add the next card:");
Console.ReadLine();
hand.AddCard(card4);
hand.AddCard(card5);
Console.WriteLine("If there are 3 cards ---> then this hand function is working.");
hand.CardsInHandConsole();
Console.WriteLine("Press <ENTER> to continue:");
Console.ReadLine();
Console.WriteLine("The Value of the hand is: " + hand.HandValue);
Console.WriteLine("The Rating of the hand is: " + hand.HandRating);
手牌类和最后向下的ENUM HandRating:
public class Hand
{
private List<ICard> _cardsInHand = new List<ICard>();
private int _handValue;
private HandRating _handRating;
public int HandValue
{
get
{
return _handValue;
}
set
{
CalculateHandValue();
}
}
public HandRating HandRating
{
get
{
return _handRating;
}
set
{
//TripleSeven
if (_cardsInHand[0].Value == CardValue.Seven &&
_cardsInHand[1].Value == CardValue.Seven &&
_cardsInHand[2].Value == CardValue.Seven)
{
_handRating = HandRating.TripleSeven;
}
//BlackJack
else if (_cardsInHand[0].Value == CardValue.Jack &&
_cardsInHand[1].Value == CardValue.Ace)
{
_handRating = HandRating.BlackJack;
}
//Busted
else if (_handValue > 21)
{
_handRating = HandRating.Busted;
}
//None
else {
_handRating = HandRating.None;
}
}
}
public IEnumerable<ICard> CardsInHand
{
get
{
return _cardsInHand;
}
}
public void CardsInHandConsole()
{
foreach (ICard card in _cardsInHand)
{
Console.WriteLine(card.ToString());
}
}
public void AddCard(ICard card)
{
_cardsInHand.Add(card);
}
public void ClearHand()
{
_cardsInHand.Clear();
_handValue = 0;
_handRating = HandRating.None;
}
private void CalculateHandValue()
{
for (int i = 0; i <= _cardsInHand.Count(); i++)
{
ReturnValue(i);
}
}
public void ReturnValue(int index)
{
if (_cardsInHand[index].Value == CardValue.Two)
{
_handValue =+2;
}
else if (_cardsInHand[index].Value == CardValue.Three)
{
_handValue =+3;
}
else if (_cardsInHand[index].Value == CardValue.Four)
{
_handValue =+4;
}
else if (_cardsInHand[index].Value == CardValue.Five)
{
_handValue =+5;
}
else if (_cardsInHand[index].Value == CardValue.Six)
{
_handValue =+6;
}
else if (_cardsInHand[index].Value == CardValue.Seven)
{
_handValue =+7;
}
else if (_cardsInHand[index].Value == CardValue.Eight)
{
_handValue =+8;
}
else if (_cardsInHand[index].Value == CardValue.Nine)
{
_handValue =+9;
}
else if (_cardsInHand[index].Value == CardValue.Queen)
{
_handValue =+10;
}
else if (_cardsInHand[index].Value == CardValue.King)
{
_handValue =+10;
}
else if (_cardsInHand[index].Value == CardValue.Jack)
{
_handValue =+10;
}
else if (_cardsInHand[index].Value == CardValue.Ace)
{
if ((_handValue + 11) > 21)
{
_handValue =+1;
}
else
{
_handValue =+11;
}
}
}
#endregion
}
public enum HandRating
{
BlackJack,
TripleSeven,
Busted,
None,
}
【问题讨论】:
-
确实显示的代码不使用设置器...您能否澄清为什么您认为显示的代码(理想情况下应该清理为minimal reproducible example)应该调用设置器?确保 edit 发布该信息(旁注:忽略值的设置器非常可疑......很可能您实际上想要始终计算值的 get-only 属性)
-
嘿 :-),所以当 hand.HandValue 被调用时,它们应该得到正确的 HandValue 但它始终为 0。在 HandRating 上它始终为 None(none 是枚举中的第一个参数)。我目前不知道如何构建我的代码以使 HandValue 和 HandRating - PROPerties 工作。我想将 HandValue 和 HandRating 放在 UI 的控制台上。
标签: c# user-interface blackjack