From cc8fbf33eedf25d94eb7fe94bcfc5897be54cdd4 Mon Sep 17 00:00:00 2001 From: CrystalMoogle Date: Thu, 20 Jul 2023 19:59:45 +0100 Subject: [PATCH] Start working on a unified card class --- BlackjackGUI/GameWindow.vb | 19 ++++++--------- BlackjackGUI/Utilities.vb | 11 +++++++-- BlackjackGUI/card.vb | 50 ++++++++++++++++++++++++++++++++++++++ BlackjackGUI/player.vb | 2 +- 4 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 BlackjackGUI/card.vb diff --git a/BlackjackGUI/GameWindow.vb b/BlackjackGUI/GameWindow.vb index 2a7c19b..f8aefc4 100644 --- a/BlackjackGUI/GameWindow.vb +++ b/BlackjackGUI/GameWindow.vb @@ -30,17 +30,12 @@ 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 - pictureBox = CType(Me.Controls.Find("PlayerCard" + cardNumber, True).First(), PictureBox) - If hide Then - pictureBox.Image = My.Resources.blue - Else - pictureBox.Image = Utilities.GetCardImage(card) - End If - Catch ex As Exception - EndGame("Error: " + ex.Message) - End Try + pictureBox = CType(Me.Controls.Find("PlayerCard" + cardNumber, True).First(), PictureBox) + If hide Then + pictureBox.Image = My.Resources.blue + Else + pictureBox.Image = Utilities.GetCardImage(card) + End If End Sub Private Sub SetTotalLabels(playerTotalAmount, dealerTotalAmount) Handles game.SetTotalLabels @@ -69,4 +64,4 @@ StandButton.Enabled = False StartGame.Enabled = True End Sub -End Class \ No newline at end of file +End Class diff --git a/BlackjackGUI/Utilities.vb b/BlackjackGUI/Utilities.vb index 29ca6ab..faa9f8e 100644 --- a/BlackjackGUI/Utilities.vb +++ b/BlackjackGUI/Utilities.vb @@ -11,7 +11,10 @@ Return list End Function - Public Shared Function GetCardImage(card As Tuple(Of String, String)) As Image + Public Shared Function GetCardImage(card As Tuple(Of String, String), Optional hidden As Boolean = False) As Image + If hidden Then + Return My.Resources.blue + End If Dim suit As String = card.Item1 Dim num As String = card.Item2 Select Case num @@ -26,6 +29,10 @@ End Select Dim img As String = suit + "_" + num - Return My.Resources.ResourceManager.GetObject(img.ToLower()) + Try + Return My.Resources.ResourceManager.GetObject(img.ToLower()) + Catch + Return My.Resources.blue + End Try End Function End Class \ No newline at end of file diff --git a/BlackjackGUI/card.vb b/BlackjackGUI/card.vb new file mode 100644 index 0000000..03f5c45 --- /dev/null +++ b/BlackjackGUI/card.vb @@ -0,0 +1,50 @@ +Public Class Card + Private Shared idCounter As Integer = 1 + Private id As Integer + Private boxId As PictureBox + Private cardImage As Image + Private cardName As Tuple(Of String, String) + Private cardLocation As Point + Private cardHidden As Boolean + + Public Sub New(name As Tuple(Of String, String), Optional hidden As Boolean = False) + id = idCounter + idCounter += 1 + boxId = New PictureBox() + cardName = name + cardHidden = hidden + cardImage = Utilities.GetCardImage(name, hidden) + End Sub + + Public Sub SetLocation(location As point) + cardLocation = location + End Sub + + Public Sub SetName(name As Tuple(Of String, String)) + cardName = name + End Sub + + Public Sub SetHidden(hidden As Boolean) + cardHidden = hidden + End Sub + + Public Function GetId() + Return id + End Function + + Public Function GetBoxId() + Return boxId + End Function + + Public Function GetName() + Return cardName + End Function + + Public Function GetLocation() + Return cardLocation + End Function + + Public Function IsHidden() + Return cardHidden + End Function +End Class \ No newline at end of file diff --git a/BlackjackGUI/player.vb b/BlackjackGUI/player.vb index 302f5e9..7b270db 100644 --- a/BlackjackGUI/player.vb +++ b/BlackjackGUI/player.vb @@ -4,7 +4,7 @@ Public Property ingame As Boolean Public Property winType As WinCondition - Sub Start() + Public Sub Start() hand.Clear() total = 0 ingame = True