另一个经典的纸牌/扑克游戏问题;我正在用随机卡(用户,计算机)填充两只手。现在我们只是比较两只手,并将手的枚举/排名值相加并进行比较。我让它正确显示,我认为我的逻辑是正确的,但我只是不太了解铸造,特别是在涉及枚举时。排名在枚举中建立(deuce = 2 等)。SuperCard 父类已将等级的设置属性设置为 cardsRank
继承人 SuperCard 类:
public abstract class SuperCard
{
#region Properties
public Rank cardsRank { get; set; }
public abstract Suit cardSuit { get; }
#endregion
public abstract void Draw();
}
这是主程序:
CardLibrary.CardSet myDeck = new CardLibrary.CardSet(); // create deck of 52 cards
int howManyCards = 5;
int balance = 10;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("Welcome to NewPoker! \nYou get $10 to start. Each bet is $1"); // intro and rules
while (balance != 0)
{
SuperCard[] computerHand = myDeck.GetCards(howManyCards); // create two hands, user/comp
SuperCard[] myHand = myDeck.GetCards(howManyCards);
DrawHands(computerHand, myHand); // call hands
Console.ReadLine();
bool won = CompareHands(computerHand, myHand);
if (won)
{
Console.WriteLine("You win this hand! Your balance is {0}", balance.ToString("C"));
balance++;
Console.WriteLine("Press enter to play the next hand");
Console.ReadLine();
}
if (!won)
{
Console.WriteLine("The dealer wins this hand! Your balance is {0}", balance.ToString("C"));
balance--;
if (balance > 0)
{
Console.WriteLine("Press enter to play the next hand");
Console.ReadLine();
}
}
}
这是 CompareHands 方法:
public bool CompareHands(SuperCard[] compHand, SuperCard[] userHand)
{
int compTotal = 0, userTotal = 0;
foreach (var item in compHand)
{
//cast enum rank to int, add?
}
foreach (var item in userHand)
{
//cast enum rank to int, add?
}
if (userTotal > compTotal)
{
return true;
}
else
return false;
}
在我看来,我认为需要一个foreach
循环来循环每个循环,将item.cardsRank
(在 SuperClass 中为Rank
枚举设置属性)转换为 int(我不确定如何),然后将该值添加到运行总计(userTotal
, compTotal
),然后比较它们的返回值,因为我们在程序“won”中调用 bool。非常感谢任何提示或指导!