別の古典的なカード/ポーカー ゲームの問題。ランダムなカード (ユーザー、コンピューター) で 2 つのハンドを埋めています。今のところ、2 つのハンドを比較し、ハンドの enum/rank 値を合計して比較しています。正しく表示されており、ロジックは正しいと思いますが、特に列挙型に関しては、キャストをよく理解していません。ランクは列挙型 (デュース = 2 など) で確立されます。SuperCard 親クラスは、cardRank としてランクの設定プロパティを取得しています
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
それぞれを循環させ、int にキャスト (列挙型item.cardsRank
の SuperClass で設定されたプロパティを取得) し、その値を現在の合計 ( 、) に追加するには、ループが必要だと考えています。次に、プログラムで bool を「won」と呼んでいるので、それらを戻り値と比較します。ヒントやガイダンスをいただければ幸いです。Rank
userTotal
compTotal