Write a program that deals a five-card poker hand. The write functions to accomp
ID: 3626786 • Letter: W
Question
Write a program that deals a five-card poker hand. The write functions to accomplish each of the following:Determine whether the hand contains a pair.
Determine whether the hand contains two pairs.
Determine whether the hand contains three of a kind (e.g., three jacks).
Determine whether the hand contains four of a kind (e.g., four aces).
The code must be in C#
This is what I have so far:
using System;
using System.Windows.Forms;
namespace DeckOfCards
{
public partial class DeckOfCardsForm : Form
{
private Card[] deck = new Card[ 52 ]; // deck of 52 cards
private int currentCard; // count which card was just dealt
// default constructor
public DeckOfCardsForm()
{
// Required for Windows Form Designer support
InitializeComponent();
} // end constructor
// handles form at load time
private void DeckForm_Load( object sender, EventArgs e )
{
string[] faces = { "Ace", "Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen",
"King" };
string[] suits = { "Hearts", "Diamonds", "Clubs", "Spades" };
currentCard = -1; // no cards have been dealt
// initialize deck
for ( int i = 0; i < deck.Length; i++ )
deck[ i ] = new Card( faces[ i % 13 ], suits[ i / 13 ] );
} // end method DeckForm_Load
// handles dealButton Click
private void dealButton_Click( object sender, EventArgs e )
{
Card dealt = DealCard();
// if dealt card is null, then no cards left
// player must shuffle cards
if ( dealt != null )
{
displayLabel.Text = dealt.ToString();
statusLabel.Text = "Card #: " + currentCard;
} // end if
else
{
displayLabel.Text = "NO MORE CARDS TO DEAL";
statusLabel.Text = "Shuffle cards to continue";
} // end else
} // end method dealButton_Click
// shuffle cards
private void Shuffle()
{
Random randomNumber = new Random();
Card temporaryValue;
currentCard = -1;
// swap each card with randomly selected card (0-51)
for ( int i = 0; i < deck.Length; i++ )
{
int j = randomNumber.Next( 52 );
// swap cards
temporaryValue = deck[ i ];
deck[ i ] = deck[ j ];
deck[ j ] = temporaryValue;
} // end for
dealButton.Enabled = true; // shuffled deck can now deal cards
} // end method Shuffle
// deal a card if the deck is not empty
private Card DealCard()
{
// if there is a card to deal then deal it
// otherwise signal that cards need to be shuffled by
// disabling dealButton and returning null
if ( currentCard + 1 < deck.Length )
{
currentCard++; // increment count
return deck[ currentCard ]; // return new card
} // end if
else
{
dealButton.Enabled = false; // empty deck cannot deal cards
return null; // do not return a card
} // end else
} // end method DealCard
// handles shuffleButton Click
private void shuffleButton_Click( object sender, EventArgs e )
{
displayLabel.Text = "SHUFFLING...";
Shuffle();
displayLabel.Text = "DECK IS SHUFFLED";
} // end method shuffleButton_Click
} // end class DeckForm
}
The above code is part of a GUI that is required for this program.
What I am unsure about is how to determine a pair, two of a kind, three of a kind, and four of a kind.
Thanks.
Explanation / Answer
Once you have a hand, use a sorting algorithm to order the 5 cards by their face ("Ace" or "Deuce" or "Three," etc) Then create variables prevFace=nul, sameCount=1, and bool onePair = false. Then, for the sorted hand, pop the last card from the hand, store its face as prevFace, and set sameCount = 1. Do so again. If that face is the same as prevFace, do sameCount++. If sameCount>=2, you've got at least one pair. In that case, set onePair=true, and continue. If you get another pair, and onePair is already true, then you've got two pair! Similarly, if you ever find that sameCount >=3, you've got three of a kind. Same thing goes for sameCount>=4. You'll need to do some housekeeping, like treating the case when prevFace is null before you get the first card, and resetting sameCount to 1 when the card is different from the previous one.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.