我目前正在编写一个BlackJack游戏,现在我在Sprint进行HAND操作,而事实是该手具有HandVALUE和HandRATING。该值或等级表示Hands属性或字段。问题是(在代码中将有更多说明),当我在Console.NetFramework(UI)中创建新的HAND并键入Console.WriteLine(Console.WriteLine(“ hand的值为:” + hand时)。 HandValue);或Console.WriteLine(“ hand的等级为:” + hand.HandRating);它们始终为0且无。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);
hand类和最后一个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,
}
为了使代码正常工作,需要更改几件事:
在Hand
类中,HandValue
和HandRating
都具有不执行任何操作的setter,应将其重构为:
public int HandValue { get; private set; }
public HandRating HandRating { get; private set; }
同时,不必要地删除_handValue
和_handRating
字段,并替换为上面的属性。
在CalculateHandValue()
中,有一个引入IndexOutOfRange
异常的错误(for循环中的<=应该为CalculateHandValue()
可以重构以利用foreach循环,如下所示:
private void CalculateHandValue()
{
foreach (var card in _cardsInHand)
{
ReturnValue(card);
}
}
ReturnValue()
不能涵盖所有卡的值(例如,控制台中的用例为7。而且,增量操作不正确(应为+ = 2而不是= +2)。如果我们添加新的用例并修复增量,它将看起来像:
private void ReturnValue(ICard card)
{
if (card.Value == CardValue.Two)
{
HandValue += 2;
}
else if (card.Value == CardValue.Three)
{
HandValue += 3;
}
else if (card.Value == CardValue.Four)
{
HandValue += 4;
}
else if (card.Value == CardValue.Seven)
{
HandValue += 7;
}
}
[我将把它留给您作为练习来填充其余手值,并可能考虑将其转换为switch
语句。
创建一个新方法CalculateHandRating()
,它将使用HandRating
设置器中的大多数逻辑,但要进行修改以确保不发生IndexOutOfRangeException
:
private void CalculateHandRating()
{
//TripleSeven
if (_cardsInHand.Count == 3 &&
_cardsInHand[0].Value == CardValue.Seven &&
_cardsInHand[1].Value == CardValue.Seven &&
_cardsInHand[2].Value == CardValue.Seven)
{
HandRating = HandRating.TripleSeven;
}
//BlackJack
else if (_cardsInHand.Count == 2 &&
_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;
}
}
注意每个if块的_cardsInHand.Count
子句,以确保我们在评估前手头有正确数量的卡。
最后,在AddCard
中,如下添加:
public void AddCard(ICard card)
{
_cardsInHand.Add(card);
CalculateHandValue();
CalculateHandRating();
}
该类应如下所示:
public class Hand
{
private List<ICard> _cardsInHand = new List<ICard>();
public int HandValue { get; private set; }
public HandRating HandRating { get; private set; }
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);
CalculateHandValue();
CalculateHandRating();
}
public void ClearHand()
{
_cardsInHand.Clear();
HandValue = 0;
HandRating = HandRating.None;
}
private void CalculateHandValue()
{
foreach (var card in _cardsInHand)
{
ReturnValue(card);
}
}
private void CalculateHandRating()
{
//TripleSeven
if (_cardsInHand.Count == 3 &&
_cardsInHand[0].Value == CardValue.Seven &&
_cardsInHand[1].Value == CardValue.Seven &&
_cardsInHand[2].Value == CardValue.Seven)
{
HandRating = HandRating.TripleSeven;
}
//BlackJack
else if (_cardsInHand.Count == 2 &&
_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;
}
}
private void ReturnValue(ICard card)
{
if (card.Value == CardValue.Two)
{
HandValue += 2;
}
else if (card.Value == CardValue.Three)
{
HandValue += 3;
}
else if (card.Value == CardValue.Four)
{
HandValue += 4;
}
else if (card.Value == CardValue.Seven)
{
HandValue += 7;
}
}
}