From 1295bdbc090ada10c75b22e7f1c01b3867f39f98 Mon Sep 17 00:00:00 2001 From: CrystalMoogle Date: Sat, 15 Jul 2023 23:22:31 +0100 Subject: [PATCH] Hide second Dealer card/total from Player unless Dealer has Blackjack. Fix possible crash cause for dealer drawing too many cards. --- BlackjackGUI/Form1.vb | 81 ++++++++++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 27 deletions(-) diff --git a/BlackjackGUI/Form1.vb b/BlackjackGUI/Form1.vb index 6f917b4..32a65b5 100644 --- a/BlackjackGUI/Form1.vb +++ b/BlackjackGUI/Form1.vb @@ -43,7 +43,9 @@ DealCard(player) DealCard(dealer, isDealer:=True) DealCard(player) - DealCard(dealer, isDealer:=True) + DealCard(dealer, isDealer:=True, hide:=True) + GetTotal(dealer) + CheckPlayer(dealer, True) CheckPlayer(player) End Sub Sub CreateDeck() @@ -67,24 +69,31 @@ deck(rand) = temp Next End Sub - Sub DealCard(player, Optional suppress = False, Optional isDealer = False) + Sub DealCard(player, Optional suppress = False, Optional isDealer = False, Optional hide = False) Dim card = deck(0) player.hand.Add(card) deck.RemoveAt(0) - GetTotal(player) + GetTotal(player, hide) Dim cardNumber As String = If(isDealer, (player.hand.count + 5).ToString(), player.hand.count) Dim pictureBox As PictureBox Try pictureBox = CType(Me.Controls.Find("PlayerCard" + cardNumber, True).First(), PictureBox) + If hide Then + pictureBox.Image = My.Resources.blue + player.hidden = card + Else + pictureBox.Image = GetCardImage(card) + End If + SetTotalLabels() Catch ex As Exception WinMessage.Text = "Error: " + ex.Message EndGame() End Try - - pictureBox.Image = GetCardImage(card) - SetTotalLabels() End Sub - Sub GetTotal(player) + Sub GetTotal(player, Optional hide = False) + If hide Then + Exit Sub + End If Dim cards As List(Of String) = player.hand If cards.Count = 0 Then player.total = 0 @@ -118,29 +127,46 @@ PlayerTotal.Text = playerLabelTotal + ": " + player.total.ToString() End Sub - Sub CheckPlayer(player) - If player.total = 21 And player.hand.count = 2 Then - player.winType = WinCondition.Blackjack - player.ingame = False - dealer.ingame = False - DealerTurn() - ElseIf player.total > 21 Then - player.winType = WinCondition.Bust - player.ingame = False - DealerTurn() - ElseIf player.total < 22 And player.hand.count = 5 Then - player.winType = WinCondition.FiveCard - player.ingame = False - dealer.ingame = False - DealerTurn() - End If - If player.total = 21 Then - player.ingame = False - DealerTurn() + Sub CheckPlayer(playerToCheck, Optional peek = False) + If Not peek Then + If playerToCheck.total = 21 And playerToCheck.hand.count = 2 Then + playerToCheck.winType = WinCondition.Blackjack + playerToCheck.ingame = False + dealer.ingame = False + DealerTurn() + ElseIf playerToCheck.total > 21 Then + playerToCheck.winType = WinCondition.Bust + playerToCheck.ingame = False + DealerTurn() + ElseIf playerToCheck.total < 22 And playerToCheck.hand.count = 5 Then + playerToCheck.winType = WinCondition.FiveCard + playerToCheck.ingame = False + dealer.ingame = False + DealerTurn() + End If + If playerToCheck.total = 21 Then + playerToCheck.ingame = False + DealerTurn() + End If + Else + If playerToCheck.total = 21 And playerToCheck.hand.count = 2 Then + playerToCheck.winType = WinCondition.Blackjack + playerToCheck.ingame = False + player.ingame = False + DealerTurn() + End If End If + End Sub Sub DealerTurn() - If dealer.ingame Then + PlayerCard7.Image = GetCardImage(dealer.hidden) + GetTotal(dealer) + SetTotalLabels() + If player.winType = WinCondition.Bust Then + CheckGame() + Exit Sub + End If + If dealer.ingame And dealer.hand.Count < 5 Then If dealer.total = 21 And dealer.hand.Count = 2 Then dealer.winType = WinCondition.Blackjack dealer.ingame = False @@ -257,6 +283,7 @@ Public Class Dealer Inherits Player Public Property limit As Integer = 17 + Public Property hidden As String End Class Enum WinCondition