Public Class Game Public Event ResetUI() Public Event CardDealt(card, cardNumber, hide) Public Event SetTotalLabels(playerTotal, dealerTotal) Public Event ShowDealerCard(card) Public Event EndPlayerTurn() Public Event EndGame(winMessage) Public Event MessageLabel(message) Private player As Player Private dealer As Dealer Private deck As List(Of Tuple(Of String, String)) Private WithEvents dealerDelay As Timer Public Sub Start() Init() player.Start() dealer.Start() DealCard(player) DealCard(dealer) DealCard(player) DealCard(dealer, hide:=True) CheckPlayer(player) CheckPlayer(dealer, True) End Sub Sub Init() player = New Player() dealer = New Dealer() deck = CreateDeck() RaiseEvent ResetUI() End Sub Sub DealCard(playerDealt As Object, Optional hide As Boolean = False) Dim card As Tuple(Of String, String) = deck(0) playerDealt.hand.Add(card) deck.RemoveAt(0) GetTotal(playerDealt, hide) If hide Then playerDealt.hidden = card End If Dim cardNumber As String = If(TypeOf playerDealt Is Dealer, (playerDealt.hand.Count + 5).ToString(), playerDealt.hand.Count) RaiseEvent CardDealt(card, cardNumber, hide) RaiseEvent SetTotalLabels(player.total, dealer.total) End Sub Sub GetTotal(playerToCheck As Object, Optional hide As Boolean = False) Dim cards As List(Of Tuple(Of String, String)) = playerToCheck.hand If cards.Count = 0 Then playerToCheck.total = 0 Return End If Dim total = 0 Dim aceTotal = 0 For Each card In cards Dim num As String = card.Item2 If num = "K" Or num = "Q" Or num = "J" Then num = 10 End If If num = "A" Then num = 11 aceTotal += 1 End If num = Integer.Parse(num) total += num Next While (aceTotal > 0 And total > 21) aceTotal -= 1 total -= 10 End While If hide Then playerToCheck.trueTotal = total Else playerToCheck.total = total End If End Sub Sub CheckPlayer(playerToCheck As Object, Optional peek As Boolean = False) Dim total As Integer = playerToCheck.total If peek Then total = playerToCheck.trueTotal End If If total = 21 And playerToCheck.hand.Count = 2 Then playerToCheck.winType = WinCondition.Blackjack playerToCheck.ingame = False If peek Then player.ingame = False Else dealer.ingame = False End If DealerTurn() ElseIf total > 21 Then playerToCheck.winType = WinCondition.Bust playerToCheck.ingame = False DealerTurn() ElseIf total < 22 And playerToCheck.hand.Count = 5 Then playerToCheck.winType = WinCondition.FiveCard playerToCheck.ingame = False dealer.ingame = False DealerTurn() End If If total = 21 Then playerToCheck.ingame = False DealerTurn() End If End Sub Sub DealerTurn() GetTotal(dealer) RaiseEvent MessageLabel("Dealer's turn...") RaiseEvent EndPlayerTurn() RaiseEvent SetTotalLabels(player.total, dealer.total) RaiseEvent ShowDealerCard(dealer.hidden) If player.winType = WinCondition.Bust Then CheckGame() Exit Sub End If If dealer.ingame Then If dealer.total = 21 And dealer.hand.Count = 2 Then dealer.winType = WinCondition.Blackjack dealer.ingame = False ElseIf dealer.total > 21 Then dealer.winType = WinCondition.Bust player.ingame = False ElseIf dealer.total < dealer.limit And dealer.hand.Count < 5 Then dealerDelay = New Timer With { .Interval = 1000 } dealerDelay.Start() Exit Sub Else dealer.winType = WinCondition.NormalWin End If End If CheckGame() End Sub Sub OnTickDealerDelay(sender As Object, e As EventArgs) Handles dealerDelay.Tick dealerDelay.Stop() dealerDelay.Dispose() DealCard(dealer) DealerTurn() End Sub Sub Hit() If player.hand.Count < 5 And player.ingame Then DealCard(player) CheckPlayer(player) End If End Sub Sub Stand() If player.ingame Then player.ingame = False DealerTurn() End If End Sub Sub CheckGame() Dim winMessage As String = GetResults() RaiseEvent EndGame(winMessage) End Sub Function GetResults() Select Case True Case dealer.winType = WinCondition.Blackjack If player.winType = WinCondition.Blackjack Then Return "Player ties with Blackjack!" Else Return "Dealer wins with Blackjack!" End If Case player.winType = WinCondition.Blackjack Return "Player wins with Blackjack!" Case player.winType = WinCondition.FiveCard Return "Player wins with Five Card Charlie!" Case player.winType = WinCondition.Bust Return "Player busts!" Case dealer.winType = WinCondition.Bust Return "Dealer busts!" Case player.total > dealer.total Return "Player wins with a total of " & player.total.ToString() & "!" Case player.total = dealer.total Return "Tie!" Case Else Return "Dealer wins with a total of " & dealer.total.ToString() & "!" End Select End Function Shared Function CreateDeck() As List(Of Tuple(Of String, String)) Dim response As New List(Of Tuple(Of String, String)) Dim nums As String() = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"} Dim suits As String() = {"Hearts", "Diamonds", "Spades", "Clubs"} For Each suit As String In suits For Each num As String In nums response.Add(Tuple.Create(suit, num)) Next Next Return Utilities.Shuffle(response) End Function End Class Public Enum WinCondition Bust NormalWin FiveCard Blackjack End Enum