diff --git a/BlackjackGUI/GameWindow.vb b/BlackjackGUI/GameWindow.vb index f6b8d3b..fcb6c18 100644 --- a/BlackjackGUI/GameWindow.vb +++ b/BlackjackGUI/GameWindow.vb @@ -1,9 +1,11 @@ Public Class GameWindow Private WithEvents game As Game + Private Sub StartGame_Click(sender As Object, e As EventArgs) Handles StartGame.Click game = New Game() game.Start() End Sub + Private Sub HitButton_Click(sender As Object, e As EventArgs) Handles HitButton.Click game.Hit() End Sub @@ -11,8 +13,11 @@ Private Sub StandButton_Click(sender As Object, e As EventArgs) Handles StandButton.Click game.Stand() End Sub + Private Sub OnResetUI() Handles game.ResetUI - Dim pictureBoxes() As PictureBox = {PlayerCard1, PlayerCard2, PlayerCard3, PlayerCard4, PlayerCard5, PlayerCard6, PlayerCard7, PlayerCard8, PlayerCard9, PlayerCard10} + Dim pictureBoxes() As PictureBox = + {PlayerCard1, PlayerCard2, PlayerCard3, PlayerCard4, PlayerCard5, PlayerCard6, PlayerCard7, PlayerCard8, + PlayerCard9, PlayerCard10} For Each box In pictureBoxes box.Image = Nothing Next @@ -22,7 +27,8 @@ StartGame.Enabled = False End Sub - Private Sub OnCardDealt(card As Tuple(Of String, String), cardNumber As String, hide As Boolean) Handles game.CardDealt + Private Sub OnCardDealt(card As Tuple(Of String, String), cardNumber As String, hide As Boolean) _ + Handles game.CardDealt Dim pictureBox As PictureBox Debug.Print(cardNumber) Try @@ -47,12 +53,11 @@ Private Sub ShowDealerCard(card) Handles game.ShowDealerCard PlayerCard7.Image = Utilities.GetCardImage(card) End Sub + Private Sub EndGame(message) Handles game.EndGame WinMessage.Text = message HitButton.Enabled = False StandButton.Enabled = False StartGame.Enabled = True End Sub - - End Class \ No newline at end of file diff --git a/BlackjackGUI/Utilities.vb b/BlackjackGUI/Utilities.vb index 1240495..4974ae3 100644 --- a/BlackjackGUI/Utilities.vb +++ b/BlackjackGUI/Utilities.vb @@ -10,6 +10,7 @@ Next Return list End Function + Public Shared Function GetCardImage(card As Tuple(Of String, String)) As Image Dim suit As String = card.Item1 Dim num As String = card.Item2 diff --git a/BlackjackGUI/game.vb b/BlackjackGUI/game.vb index beb40a7..a6908be 100644 --- a/BlackjackGUI/game.vb +++ b/BlackjackGUI/game.vb @@ -9,6 +9,7 @@ Private dealer As Dealer Private deck As List(Of Tuple(Of String, String)) Private WithEvents dealerDelay As Timer + Public Sub Start() Init() player.Start() @@ -20,12 +21,14 @@ 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) @@ -34,11 +37,13 @@ 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) + 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 @@ -68,8 +73,8 @@ 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 @@ -98,8 +103,8 @@ playerToCheck.ingame = False DealerTurn() End If - End Sub + Sub DealerTurn() GetTotal(dealer) RaiseEvent SetTotalLabels(player.total, dealer.total) @@ -118,7 +123,7 @@ ElseIf dealer.total < dealer.limit And dealer.hand.Count < 5 Then dealerDelay = New Timer With { .Interval = 1000 - } + } dealerDelay.Start() Exit Sub Else @@ -127,6 +132,7 @@ End If CheckGame() End Sub + Sub OnTickDealerDelay(sender As Object, e As EventArgs) Handles dealerDelay.Tick dealerDelay.Stop() dealerDelay.Dispose() @@ -140,16 +146,19 @@ 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 @@ -174,6 +183,7 @@ Return "Dealer wins with a total of " & dealer.total.ToString() & "!" End Select End Function + 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"} @@ -185,7 +195,6 @@ Next Return Utilities.Shuffle(response) End Function - End Class Public Enum WinCondition diff --git a/BlackjackGUI/player.vb b/BlackjackGUI/player.vb index e575b93..302f5e9 100644 --- a/BlackjackGUI/player.vb +++ b/BlackjackGUI/player.vb @@ -9,9 +9,9 @@ total = 0 ingame = True winType = WinCondition.NormalWin - End Sub End Class + Public Class Dealer Inherits Player