'How do I create a winner in a game of BlackJack?
In my c# we are building a blackjack game with classes. I have card.cs hand.cs and deck.cs which I'm posting with this. I am trying to put the game together and I cannot figure out the getWinner() method or how to code and show a winner. My code for my hand looks correct as far as I can tell- except when I first run the game it does not add and show the total of the two cards so I am starting there, then working on the winner- Can I create a winner in the Hand class? I was thinking it would be better on the Form.cs?
class Hand
{
private List<Card> listOfCards = new List<Card>();
public int cardCount()
{
return listOfCards.Count();
}
public void addCard(Card card)
{
listOfCards.Add(card);
}
private void clearHand()
{
listOfCards.Clear();
}
public Card getCardAtIndex(int index)
{
Card card = new Card();
if(index < listOfCards.Count)
{
card = listOfCards[index];
}
return card;
}
public int getAllCardsTotal()
{
int total = 0;
int aceCount = 0;
foreach(Card card in listOfCards)
{
if (card.getBlackJackValue() == 1)
aceCount++;
total = total + card.getBlackJackValue();
}
//See if we need to use the Ace as a 1 or 11
if(aceCount > 0)
{
//Dealer stays 17-21 or can be player
if (total >= 7 && total <= 11)
total = total + 10;
}
return total;
}
}
}
class Deck
{
private List<Card> listOfCards = new List<Card>();
public Deck()
{
listOfCards.Clear();
loadDeck();
}
public int cardCount()
{
return listOfCards.Count();
}
public Card drawCard()
{
Card card = new Card();
//get random card from deck
int cardCount = listOfCards.Count;
Random random = new Random(Guid.NewGuid().GetHashCode());
int cardIndex = random.Next(0, cardCount);
card = listOfCards[cardIndex]; //Get a copy of the card
listOfCards.RemoveAt(cardIndex); // This removes the card
return card;
}
private void loadDeck()
{
//load in order A-KClubs- Diamonds - Hearts - Spades
int imageIndex = 0;
for(int rank = 1; rank <=13; rank++)
{
Card card = new Card(rank, Card.SuitType.Clubs, imageIndex);
listOfCards.Add(card);
imageIndex++; //increment each time we add a card
card = new Card(rank, Card.SuitType.Diamonds, imageIndex);
listOfCards.Add(card);
imageIndex++; //increment each time we add a card
card = new Card(rank, Card.SuitType.Hearts, imageIndex);
listOfCards.Add(card);
imageIndex++; //increment each time we add a card
card = new Card(rank, Card.SuitType.Spades, imageIndex);
listOfCards.Add(card);
imageIndex++; //increment each time we add a card
}
}
}
}
class Card
{
public enum SuitType { Clubs, Diamonds, Hearts, Spades}
private int rank;
private SuitType suit;
private int blackJackValue;
private int imageIndex;
public SuitType Suit { get => suit; set => suit = value; }
public int ImageIndex { get => imageIndex; set => imageIndex = value; }
public Card(int rank, SuitType suit, int blackJackValue, int imageIndex)
{
this.rank = rank;
this.suit = suit;
this.imageIndex = imageIndex;
}
public Card()
{
}
public Card(int rank, SuitType suit, int imageIndex)
{
this.rank = rank;
this.Suit = suit;
this.ImageIndex = imageIndex;
}
public int getBlackJackValue()
{
int blackJackValue = 0;
if (rank > 9)
blackJackValue = 10;
else
blackJackValue = rank;
return blackJackValue;
}
public override string ToString()
{
String cardName = "";
string cardRank = "";
//Display a string of ace of clubs or 3 of diamonds
if (rank == 13)
cardRank = "King";
else if (rank == 12)
cardRank = "Queen";
else if (rank == 11)
cardRank = "Jack";
else if (rank == 1)
cardRank = "Ace";
cardName = cardRank + "of" + suit.ToString();
return cardName;
}
}
}
And the form1.cs so far is:
public partial class BlackJack : Form
{
public BlackJack()
{
InitializeComponent();
}
//Global variables
Deck gameDeck = new Deck();
Hand playerHand = new Hand();
Hand dealerHand = new Hand();
//To increment the games?
int playerWins = 0;
int dealerWins = 0;
int ties = 0;
int totalGames = 0;
List<PictureBox> listPBPlayer = new List<PictureBox>();
List<PictureBox> listPBDealer = new List<PictureBox>();
private void BlackJack_Load(object sender, EventArgs e)
{
dealHands();
addPictureBoxesToLists();
}
private void dealHands()
{
gameDeck = new Deck();
playerHand = new Hand();
dealerHand = new Hand();
hidePictureBoxes();
Card card = new Card();
//Add players first card
card = gameDeck.drawCard();
playerHand.addCard(card);
pictureBoxP1.Image = imageListCards.Images[card.ImageIndex];
pictureBoxP1.Visible = true;
//Add dealers first card
card = gameDeck.drawCard();
dealerHand.addCard(card);
pictureBoxD1.Image = imageListCards.Images[card.ImageIndex];
pictureBoxD1.Visible = true;
//Add players 2nd card
card = gameDeck.drawCard();
dealerHand.addCard(card);
pictureBoxP2.Image = imageListCards.Images[card.ImageIndex];
pictureBoxP2.Visible = true;
//Add dealers 2nd card
card = gameDeck.drawCard();
dealerHand.addCard(card);
pictureBoxD2.Image = imageListCards.Images[52]; // card face back image
labelPlayerTotal.Text = playerHand.getAllCardsTotal().ToString();
labelDealerWins.Text = dealerHand.getCardAtIndex(0).getBlackJackValue().ToString();
labelWhoWon.Text = "";
buttonDEAL.Enabled = true;
buttonHIT.Enabled = true;
buttonSTAND.Enabled = true;
}
private void addPictureBoxesToLists()
{
listPBPlayer.Clear();
listPBPlayer.Add(pictureBoxP1);
listPBPlayer.Add(pictureBoxP2);
listPBPlayer.Add(pictureBoxP3);
listPBPlayer.Add(pictureBoxP4);
listPBPlayer.Add(pictureBoxP5);
listPBPlayer.Add(pictureBoxP6);
listPBPlayer.Add(pictureBoxP7);
listPBDealer.Clear();
listPBDealer.Add(pictureBoxD1);
listPBDealer.Add(pictureBoxD2);
listPBDealer.Add(pictureBoxD3);
listPBDealer.Add(pictureBoxD4);
listPBDealer.Add(pictureBoxD5);
listPBDealer.Add(pictureBoxD6);
listPBDealer.Add(pictureBoxD7);
}
private void newRound()
{
dealHands();
}
private void hidePictureBoxes()
{
listPBPlayer.Clear();
listPBDealer.Clear();
foreach (PictureBox pb in listPBPlayer)
{
//pb.Image = imageListCards.Images[52];
pb.Visible = false;
}
foreach (PictureBox pb in listPBDealer)
{
//pb.Image = imageListCards.Images[52]; - never got to see all the cards on the screen?
pb.Visible = false;
}
}
private void buttonDEAL_Click(object sender, EventArgs e)
{
dealHands();
}
private void buttonHIT_Click(object sender, EventArgs e)
{
Card card = new Card();
card = gameDeck.drawCard();
playerHand.addCard(card);
// textBoxPlayerTotal.Text = playerHand.getAllCardsTotal.ToString();
//Show the new card
listPBPlayer[playerHand.cardCount() - 1].Image = imageListCards.Images[card.ImageIndex];
listPBDealer[playerHand.cardCount() - 1].Visible = true;
if (playerHand.getAllCardsTotal() > 21)
{
//Busted show the dealer cards and display second card
listPBDealer[1].Image = imageListCards.Images[dealerHand.getCardAtIndex(1).ImageIndex];
listPBDealer[1].Visible = true;
// textBoxDealerTotal.Text = dealerHand.getHandCardTotal.ToString();
// textBoxWhoWon.Text = getWinner();
buttonDEAL.Enabled = true;
buttonHIT.Enabled = false;
buttonSTAND.Enabled = false;
}
}
private void buttonSTAND_Click(object sender, EventArgs e)
{
//Stand - have the dealer try to bust
}
private void getWinner()
{
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
