BlackjackGUI/BlackjackGUI/game.vb

193 lines
6.2 KiB
VB.net

Public Class Game
Public Event ResetUI()
Public Event CardDealt(card, cardNumber, hide)
Public Event SetTotalLabels(playerTotal, dealerTotal)
Public Event ShowDealerCard(card)
Public Event EndGame(winMessage)
Private player As Player
Private dealer As Dealer
Private deck As List(Of Tuple(Of String, String))
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 As Integer = 0
Dim aceTotal As Integer = 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 SetTotalLabels(player.total, dealer.total)
RaiseEvent ShowDealerCard(dealer.hidden)
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
ElseIf dealer.total > 21 Then
dealer.winType = WinCondition.Bust
player.ingame = False
ElseIf dealer.total < dealer.limit Then
DealCard(dealer)
DealerTurn()
Else
dealer.winType = WinCondition.NormalWin
End If
End If
CheckGame()
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()
If dealer.winType = WinCondition.Blackjack Then
If player.winType = WinCondition.Blackjack Then
Return "Player ties with Blackjack!"
Else
Return "Dealer wins with Blackjack!"
End If
End If
If player.winType = WinCondition.Blackjack Then
Return "Player wins with Blackjack!"
End If
If player.winType = WinCondition.FiveCard Then
Return "Player wins with Five Card Charlie!"
End If
If player.winType = WinCondition.Bust Then
Return "Player busts!"
End If
If dealer.winType = WinCondition.Bust Then
Return "Dealer busts!"
End If
If player.total > dealer.total Then
Return "Player wins with total of " + player.total.ToString() + "!"
End If
If player.total = dealer.total Then
Return "Tie!"
End If
If player.total < dealer.total Then
Return "Dealer wins with total of " + dealer.total.ToString() + "!"
End If
Return "error"
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"}
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