Compare commits

...

31 Commits

Author SHA1 Message Date
CrystalMoogle 9877b71b07 Test 2024-10-22 20:21:58 +01:00
CrystalMoogle 14787c3753 Add description to README 2023-08-12 11:28:17 +01:00
zana a1274d7335 Add text to README 2023-08-12 10:22:50 +00:00
zana 4647fb8288 test mirror 2023-08-12 10:21:39 +00:00
zana de6a9a0645 Add readme.md 2023-08-12 10:21:04 +00:00
CrystalMoogle 98d7967726 Make slight changes to layout, add deck for future animation, add a size attribute 2023-07-22 19:45:15 +01:00
CrystalMoogle 959b3cd71d Change how suit/number is handled in the card class, change the layout of the card tuples. 2023-07-21 21:24:37 +01:00
CrystalMoogle f381a1806e Delete extra file 2023-07-21 18:22:09 +01:00
CrystalMoogle 9581591605 Fix 2023-07-21 18:20:29 +01:00
CrystalMoogle a368d0af41 Convert more functions to use card class 2023-07-21 18:20:03 +01:00
CrystalMoogle c286b08521 Rename for consistency 2023-07-20 20:11:35 +01:00
CrystalMoogle cc8fbf33ee Start working on a unified card class 2023-07-20 19:59:45 +01:00
CrystalMoogle 2455812fb2 Add a message for dealer's turn, also an event to add any message if needed in the future 2023-07-16 17:40:28 +01:00
CrystalMoogle 20f702b537 Disable buttons when player turn ended 2023-07-16 17:34:38 +01:00
CrystalMoogle de4dda9b25 Merge branch 'master' of https://github.com/CrystalMoogle/BlackjackGUI 2023-07-16 17:18:15 +01:00
CrystalMoogle ceef90be2e More formatting 2023-07-16 17:17:03 +01:00
Zana 0047e56ec3
Update dotnet.yml 2023-07-16 17:07:14 +01:00
Zana b090e3f83c
Create dotnet.yml 2023-07-16 17:04:19 +01:00
CrystalMoogle ef55b4bc2e Formatting 2023-07-16 16:50:52 +01:00
CrystalMoogle 3ae3a26e2b Add a delay to dealer drawing of cards 2023-07-16 16:34:20 +01:00
CrystalMoogle 12b6bb7358 Move this here, otherwise dealer can win with 5 cards even if bust 2023-07-16 16:14:04 +01:00
CrystalMoogle d4eff0d774 Replace this with a switch statement, make it easier to read 2023-07-16 16:10:48 +01:00
CrystalMoogle 2860c9c622 Remove testing code 2023-07-16 16:07:57 +01:00
CrystalMoogle 73ace5bcca Condense code a little 2023-07-16 16:07:13 +01:00
CrystalMoogle d4511f6ffa Specify data types for parameters 2023-07-16 15:54:53 +01:00
CrystalMoogle eb37ba7c41 Convert cards to tuple 2023-07-16 15:37:10 +01:00
CrystalMoogle ca2dd78e06 Remove unused file 2023-07-16 15:21:04 +01:00
CrystalMoogle 1e0a9a71b2 Remove unneeded code 2023-07-16 15:15:29 +01:00
CrystalMoogle 6880589562 Refactor code to make Game its own Class to split from UI dedicated functions. 2023-07-16 15:13:43 +01:00
CrystalMoogle 1295bdbc09 Hide second Dealer card/total from Player unless Dealer has Blackjack. Fix possible crash cause for dealer drawing too many cards. 2023-07-15 23:22:31 +01:00
CrystalMoogle 0c5c9e115b Fix a crash until cause is found, slightly change position of UI element 2023-07-15 22:22:53 +01:00
13 changed files with 500 additions and 318 deletions

28
.github/workflows/dotnet.yml vendored 100644
View File

@ -0,0 +1,28 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net
name: .NET
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal

View File

@ -1,45 +1,45 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<StartupObject>Sub Main</StartupObject>
<UseWindowsForms>true</UseWindowsForms>
<MyType>WindowsForms</MyType>
</PropertyGroup>
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<StartupObject>Sub Main</StartupObject>
<UseWindowsForms>true</UseWindowsForms>
<MyType>WindowsForms</MyType>
</PropertyGroup>
<ItemGroup>
<Import Include="System.Data" />
<Import Include="System.Drawing" />
<Import Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<Import Include="System.Data" />
<Import Include="System.Drawing" />
<Import Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<Compile Update="My Project\Application.Designer.vb">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Application.myapp</DependentUpon>
</Compile>
<Compile Update="My Project\Resources.Designer.vb">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Update="My Project\Application.Designer.vb">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Application.myapp</DependentUpon>
</Compile>
<Compile Update="My Project\Resources.Designer.vb">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="My Project\Resources.resx">
<CustomToolNamespace>My.Resources</CustomToolNamespace>
<Generator>VbMyResourcesResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.vb</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="My Project\Resources.resx">
<CustomToolNamespace>My.Resources</CustomToolNamespace>
<Generator>VbMyResourcesResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.vb</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Update="My Project\Application.myapp">
<Generator>MyApplicationCodeGenerator</Generator>
<LastGenOutput>Application.Designer.vb</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<None Update="My Project\Application.myapp">
<Generator>MyApplicationCodeGenerator</Generator>
<LastGenOutput>Application.Designer.vb</LastGenOutput>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,59 @@
Public Class Card
Private Shared idCounter As Integer = 1
Private id As Integer
Private boxId As PictureBox
Private cardImage As Image
Private card As (suit As String, number As String)
Private cardLocation As Point
Private cardHidden As Boolean
Private size As Size
Public Sub New(name As (String, String), Optional hidden As Boolean = False)
id = idCounter
idCounter += 1
boxId = New PictureBox()
card = name
cardHidden = hidden
cardImage = Utilities.GetCardImage(name, hidden)
size = New Size(125,175)
End Sub
Public Sub SetLocation(location As point)
cardLocation = location
End Sub
Public Sub SetName(name As (String, String))
card = 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 card
End Function
Public Function GetSuit() As String
Return card.suit
End Function
Public Function GetNumber() As String
Return card.number
End Function
Public Function GetLocation()
Return cardLocation
End Function
Public Function IsHidden()
Return cardHidden
End Function
End Class

View File

@ -1,258 +0,0 @@
Public Class Form1
Inherits Form
Dim player As New Player()
Dim dealer As New Dealer()
Dim deck As New List(Of String)
Dim gameRunning As Boolean = True
Private Sub StartGame_Click(sender As Object, e As EventArgs) Handles StartGame.Click
PlayGame()
End Sub
Private Sub HitButton_Click(sender As Object, e As EventArgs) Handles HitButton.Click
If gameRunning And player.hand.Count < 5 And player.ingame Then
DealCard(player)
CheckPlayer(player)
End If
End Sub
Private Sub StandButton_Click(sender As Object, e As EventArgs) Handles StandButton.Click
If gameRunning And player.ingame Then
player.ingame = False
DealerTurn()
End If
End Sub
Private Sub ResetCards()
Dim pictureBoxes() As PictureBox = {PlayerCard1, PlayerCard2, PlayerCard3, PlayerCard4, PlayerCard5, PlayerCard6, PlayerCard7, PlayerCard8, PlayerCard9, PlayerCard10}
For Each box In pictureBoxes
box.Image = Nothing
Next
End Sub
Sub PlayGame()
InitGame()
End Sub
Sub InitGame()
ResetCards()
WinMessage.ResetText()
HitButton.Enabled = True
StandButton.Enabled = True
StartGame.Enabled = False
gameRunning = True
player.Start()
dealer.Start()
CreateDeck()
DealCard(player)
DealCard(dealer, isDealer:=True)
DealCard(player)
DealCard(dealer, isDealer:=True)
CheckPlayer(player)
End Sub
Sub CreateDeck()
deck.Clear()
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 s As String In suits
For Each n As String In nums
deck.Add(n + " of " + s)
Next
Next
Shuffle(deck)
End Sub
Sub Shuffle(deck)
Dim max As Integer = deck.Count - 1
Dim random As New Random()
For x As Integer = 0 To max
Dim rand As Integer = random.Next(0, max)
Dim temp As String = deck(x)
deck(x) = deck(rand)
deck(rand) = temp
Next
End Sub
Sub DealCard(player, Optional suppress = False, Optional isDealer = False)
Dim card = deck(0)
player.hand.Add(card)
deck.RemoveAt(0)
GetTotal(player)
Dim cardNumber As String = If(isDealer, (player.hand.count + 5).ToString(), player.hand.count)
Dim pictureBox As PictureBox = CType(Me.Controls.Find("PlayerCard" + cardNumber, True).First(), PictureBox)
pictureBox.Image = GetCardImage(card)
SetTotalLabels()
End Sub
Sub GetTotal(player)
Dim cards As List(Of String) = player.hand
If cards.Count = 0 Then
player.total = 0
Return
End If
Dim t As Integer = 0
Dim a As Integer = 0
For Each x As String In cards
Dim card As String = x
Dim num As String = card.Split(" "c)(0)
If num = "K" Or num = "Q" Or num = "J" Then
num = 10
End If
If num = "A" Then
num = 11
a += 1
End If
num = Integer.Parse(num)
t += num
Next
While (a > 0 And t > 21)
a -= 1
t = t - 10
End While
player.total = t
End Sub
Sub SetTotalLabels()
Dim dealerLabelTotal As String = DealerTotal.Text.Split(": ")(0)
Dim playerLabelTotal As String = PlayerTotal.Text.Split(": ")(0)
DealerTotal.Text = dealerLabelTotal + ": " + dealer.total.ToString()
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()
End If
End Sub
Sub DealerTurn()
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 Then
DealCard(dealer, isDealer:=True)
DealerTurn()
Else
dealer.winType = WinCondition.NormalWin
End If
End If
CheckGame()
End Sub
Sub CheckGame()
WinMessage.Text = GetResults()
HitButton.Enabled = False
StandButton.Enabled = False
StartGame.Enabled = True
End Sub
Function GetResults()
If dealer.winType = WinCondition.Blackjack Then
If player.winType = WinCondition.Blackjack Then
PrintCurrentGame()
Return "Player ties with Blackjack!"
Else
PrintCurrentGame()
Return "Dealer wins with Blackjack!"
End If
End If
If player.winType = WinCondition.Blackjack Then
PrintCurrentGame()
Return "Player wins with Blackjack!"
End If
If player.winType = WinCondition.FiveCard Then
PrintCurrentGame()
Return "Player wins with Five Card Charlie!"
End If
If player.winType = WinCondition.Bust Then
PrintCurrentGame()
Return "Player busts!"
End If
If dealer.winType = WinCondition.Bust Then
PrintCurrentGame()
Return "Dealer busts!"
End If
If player.total > dealer.total Then
PrintCurrentGame()
Return "Player wins with total of " + player.total.ToString() + "!"
End If
If player.total = dealer.total Then
PrintCurrentGame()
Return "Tie!"
End If
If player.total < dealer.total Then
PrintCurrentGame()
Return "Dealer wins with total of " + dealer.total.ToString() + "!"
End If
PrintCurrentGame()
Return "error"
End Function
Function GetCardImage(card) As Image
Dim num As String = card.split(" ")(0)
Dim suit As String = card.split(" ")(2)
Select Case num
Case "K"
num = "king"
Case "Q"
num = "queen"
Case "J"
num = "jack"
Case "A"
num = "ace"
End Select
Dim img As String = suit + "_" + num
Return My.Resources.ResourceManager.GetObject(img.ToLower())
End Function
Sub PrintCurrentGame()
Console.WriteLine("Dealer Hand: " + String.Join(", ", dealer.hand) + " Total: " + dealer.total.ToString())
Console.WriteLine("Player Hand: " + String.Join(", ", player.hand) + " Total: " + player.total.ToString())
End Sub
End Class
Public Class Player
Private _winCondition As WinCondition
Public Property hand As New List(Of String)
Public Property total As Integer
Public Property ingame As Boolean
Public Property winType
Get
Return _winCondition
End Get
Set
_winCondition = Value
End Set
End Property
Sub Start()
hand.Clear()
total = 0
ingame = True
winType = WinCondition.NormalWin
End Sub
End Class
Public Class Dealer
Inherits Player
Public Property limit As Integer = 17
End Class
Enum WinCondition
Bust
NormalWin
FiveCard
Blackjack
End Enum

View File

@ -0,0 +1,205 @@
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 (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 (String, String) = deck(0)
Dim playingCard = New Card(card, hide)
playerDealt.hand.Add(playingCard)
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(playingCard, 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 Card) = 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.GetNumber()
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 (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"}
Dim response As List(Of (String, String)) = (From suit In suits From num In nums Select (suit, num)).ToList()
Return Utilities.Shuffle(response)
End Function
End Class
Public Enum WinCondition
Bust
NormalWin
FiveCard
Blackjack
End Enum

View File

@ -1,5 +1,5 @@
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class Form1
Partial Class GameWindow
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
@ -38,6 +38,7 @@ Partial Class Form1
PlayerTotal = New Label()
DealerTotal = New Label()
WinMessage = New Label()
deckPictureBox = New PictureBox()
CType(PlayerCard1, ComponentModel.ISupportInitialize).BeginInit()
CType(PlayerCard2, ComponentModel.ISupportInitialize).BeginInit()
CType(PlayerCard3, ComponentModel.ISupportInitialize).BeginInit()
@ -48,11 +49,12 @@ Partial Class Form1
CType(PlayerCard8, ComponentModel.ISupportInitialize).BeginInit()
CType(PlayerCard9, ComponentModel.ISupportInitialize).BeginInit()
CType(PlayerCard10, ComponentModel.ISupportInitialize).BeginInit()
CType(deckPictureBox, ComponentModel.ISupportInitialize).BeginInit()
SuspendLayout()
'
' HitButton
'
HitButton.Anchor = AnchorStyles.Top Or AnchorStyles.Right
HitButton.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left
HitButton.Enabled = False
HitButton.Font = New Font("Segoe UI", 36.0F, FontStyle.Regular, GraphicsUnit.Point)
HitButton.Location = New Point(100, 490)
@ -64,7 +66,7 @@ Partial Class Form1
'
' StandButton
'
StandButton.Anchor = AnchorStyles.Top Or AnchorStyles.Right
StandButton.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
StandButton.Enabled = False
StandButton.Font = New Font("Segoe UI", 36.0F, FontStyle.Regular, GraphicsUnit.Point)
StandButton.Location = New Point(780, 490)
@ -76,11 +78,11 @@ Partial Class Form1
'
' PlayerCard1
'
PlayerCard1.Anchor = AnchorStyles.Top Or AnchorStyles.Right
PlayerCard1.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left
PlayerCard1.ErrorImage = My.Resources.Resources.blue2
PlayerCard1.Image = My.Resources.Resources.blue
PlayerCard1.InitialImage = My.Resources.Resources.blue2
PlayerCard1.Location = New Point(150, 284)
PlayerCard1.Location = New Point(150, 303)
PlayerCard1.Name = "PlayerCard1"
PlayerCard1.Size = New Size(125, 175)
PlayerCard1.TabIndex = 2
@ -88,11 +90,11 @@ Partial Class Form1
'
' PlayerCard2
'
PlayerCard2.Anchor = AnchorStyles.Top Or AnchorStyles.Right
PlayerCard2.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left
PlayerCard2.ErrorImage = My.Resources.Resources.blue2
PlayerCard2.Image = My.Resources.Resources.blue
PlayerCard2.InitialImage = My.Resources.Resources.blue2
PlayerCard2.Location = New Point(375, 284)
PlayerCard2.Location = New Point(364, 303)
PlayerCard2.Name = "PlayerCard2"
PlayerCard2.Size = New Size(125, 175)
PlayerCard2.TabIndex = 3
@ -100,10 +102,10 @@ Partial Class Form1
'
' PlayerCard3
'
PlayerCard3.Anchor = AnchorStyles.Top Or AnchorStyles.Right
PlayerCard3.Anchor = AnchorStyles.Bottom
PlayerCard3.ErrorImage = Nothing
PlayerCard3.InitialImage = Nothing
PlayerCard3.Location = New Point(578, 284)
PlayerCard3.Location = New Point(578, 303)
PlayerCard3.Name = "PlayerCard3"
PlayerCard3.Size = New Size(125, 175)
PlayerCard3.TabIndex = 4
@ -111,10 +113,10 @@ Partial Class Form1
'
' PlayerCard4
'
PlayerCard4.Anchor = AnchorStyles.Top Or AnchorStyles.Right
PlayerCard4.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
PlayerCard4.ErrorImage = Nothing
PlayerCard4.InitialImage = Nothing
PlayerCard4.Location = New Point(780, 284)
PlayerCard4.Location = New Point(792, 303)
PlayerCard4.Name = "PlayerCard4"
PlayerCard4.Size = New Size(125, 175)
PlayerCard4.TabIndex = 5
@ -122,10 +124,10 @@ Partial Class Form1
'
' PlayerCard5
'
PlayerCard5.Anchor = AnchorStyles.Top Or AnchorStyles.Right
PlayerCard5.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
PlayerCard5.ErrorImage = Nothing
PlayerCard5.InitialImage = Nothing
PlayerCard5.Location = New Point(1005, 284)
PlayerCard5.Location = New Point(1005, 303)
PlayerCard5.Name = "PlayerCard5"
PlayerCard5.Size = New Size(125, 175)
PlayerCard5.TabIndex = 6
@ -151,7 +153,7 @@ Partial Class Form1
PlayerCard7.ErrorImage = My.Resources.Resources.blue2
PlayerCard7.Image = My.Resources.Resources.blue
PlayerCard7.InitialImage = My.Resources.Resources.blue2
PlayerCard7.Location = New Point(780, 12)
PlayerCard7.Location = New Point(792, 12)
PlayerCard7.Name = "PlayerCard7"
PlayerCard7.Size = New Size(125, 175)
PlayerCard7.TabIndex = 10
@ -159,7 +161,7 @@ Partial Class Form1
'
' PlayerCard8
'
PlayerCard8.Anchor = AnchorStyles.Top Or AnchorStyles.Right
PlayerCard8.Anchor = AnchorStyles.Top
PlayerCard8.ErrorImage = Nothing
PlayerCard8.InitialImage = Nothing
PlayerCard8.Location = New Point(578, 12)
@ -170,10 +172,9 @@ Partial Class Form1
'
' PlayerCard9
'
PlayerCard9.Anchor = AnchorStyles.Top Or AnchorStyles.Right
PlayerCard9.ErrorImage = Nothing
PlayerCard9.InitialImage = Nothing
PlayerCard9.Location = New Point(375, 12)
PlayerCard9.Location = New Point(364, 12)
PlayerCard9.Name = "PlayerCard9"
PlayerCard9.Size = New Size(125, 175)
PlayerCard9.TabIndex = 8
@ -181,7 +182,6 @@ Partial Class Form1
'
' PlayerCard10
'
PlayerCard10.Anchor = AnchorStyles.Top Or AnchorStyles.Right
PlayerCard10.ErrorImage = Nothing
PlayerCard10.InitialImage = Nothing
PlayerCard10.Location = New Point(150, 12)
@ -192,7 +192,7 @@ Partial Class Form1
'
' StartGame
'
StartGame.Anchor = AnchorStyles.Top Or AnchorStyles.Right
StartGame.Anchor = AnchorStyles.Bottom
StartGame.Font = New Font("Segoe UI", 26.25F, FontStyle.Regular, GraphicsUnit.Point)
StartGame.Location = New Point(535, 490)
StartGame.Name = "StartGame"
@ -203,6 +203,7 @@ Partial Class Form1
'
' PlayerTotal
'
PlayerTotal.Anchor = AnchorStyles.Bottom
PlayerTotal.AutoSize = True
PlayerTotal.Font = New Font("Segoe UI", 15.75F, FontStyle.Regular, GraphicsUnit.Point)
PlayerTotal.Location = New Point(535, 561)
@ -213,6 +214,7 @@ Partial Class Form1
'
' DealerTotal
'
DealerTotal.Anchor = AnchorStyles.Bottom
DealerTotal.AutoSize = True
DealerTotal.Font = New Font("Segoe UI", 15.75F, FontStyle.Regular, GraphicsUnit.Point)
DealerTotal.Location = New Point(535, 610)
@ -225,15 +227,30 @@ Partial Class Form1
'
WinMessage.AutoSize = True
WinMessage.Font = New Font("Segoe UI", 15.75F, FontStyle.Regular, GraphicsUnit.Point)
WinMessage.Location = New Point(565, 221)
WinMessage.Location = New Point(535, 216)
WinMessage.Name = "WinMessage"
WinMessage.Size = New Size(0, 30)
WinMessage.TabIndex = 15
'
' Form1
' deckPictureBox
'
deckPictureBox.Anchor = AnchorStyles.Top Or AnchorStyles.Right
deckPictureBox.BackgroundImageLayout = ImageLayout.None
deckPictureBox.ErrorImage = My.Resources.Resources.blue2
deckPictureBox.Image = My.Resources.Resources.blue
deckPictureBox.InitialImage = My.Resources.Resources.blue2
deckPictureBox.Location = New Point(1136, 156)
deckPictureBox.Name = "deckPictureBox"
deckPictureBox.Size = New Size(125, 175)
deckPictureBox.SizeMode = PictureBoxSizeMode.Zoom
deckPictureBox.TabIndex = 16
deckPictureBox.TabStop = False
'
' GameWindow
'
AutoScaleMode = AutoScaleMode.Inherit
ClientSize = New Size(1264, 681)
Controls.Add(deckPictureBox)
Controls.Add(WinMessage)
Controls.Add(DealerTotal)
Controls.Add(PlayerTotal)
@ -251,7 +268,7 @@ Partial Class Form1
Controls.Add(StandButton)
Controls.Add(HitButton)
MinimumSize = New Size(1280, 720)
Name = "Form1"
Name = "GameWindow"
StartPosition = FormStartPosition.CenterScreen
Text = "Blackjack"
CType(PlayerCard1, ComponentModel.ISupportInitialize).EndInit()
@ -264,6 +281,7 @@ Partial Class Form1
CType(PlayerCard8, ComponentModel.ISupportInitialize).EndInit()
CType(PlayerCard9, ComponentModel.ISupportInitialize).EndInit()
CType(PlayerCard10, ComponentModel.ISupportInitialize).EndInit()
CType(deckPictureBox, ComponentModel.ISupportInitialize).EndInit()
ResumeLayout(False)
PerformLayout()
End Sub
@ -284,4 +302,5 @@ Partial Class Form1
Friend WithEvents PlayerTotal As Label
Friend WithEvents DealerTotal As Label
Friend WithEvents WinMessage As Label
Friend WithEvents deckPictureBox As PictureBox
End Class

View File

@ -0,0 +1,67 @@
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
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}
For Each box In pictureBoxes
box.Image = Nothing
Next
WinMessage.ResetText()
HitButton.Enabled = True
StandButton.Enabled = True
StartGame.Enabled = False
End Sub
Private Sub OnCardDealt(card As Card, cardNumber As String, hide As Boolean) _
Handles game.CardDealt
Dim pictureBox As PictureBox
pictureBox = CType(Me.Controls.Find("PlayerCard" + cardNumber, True).First(), PictureBox)
If hide Then
pictureBox.Image = My.Resources.blue
Else
pictureBox.Image = Utilities.GetCardImage((card.GetSuit(), card.GetNumber()))
End If
End Sub
Private Sub SetTotalLabels(playerTotalAmount, dealerTotalAmount) Handles game.SetTotalLabels
Dim dealerLabelTotal As String = DealerTotal.Text.Split(": ")(0)
Dim playerLabelTotal As String = PlayerTotal.Text.Split(": ")(0)
DealerTotal.Text = dealerLabelTotal + ": " + dealerTotalAmount.ToString()
PlayerTotal.Text = playerLabelTotal + ": " + playerTotalAmount.ToString()
End Sub
Private Sub ShowDealerCard(card) Handles game.ShowDealerCard
PlayerCard7.Image = Utilities.GetCardImage(card)
End Sub
Private Sub EndPlayerTurn() Handles game.EndPlayerTurn
HitButton.Enabled = False
StandButton.Enabled = False
End Sub
Private Sub MessageLabel(message) Handles game.MessageLabel
WinMessage.Text = message
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

View File

@ -31,7 +31,7 @@ Namespace My
<Global.System.Diagnostics.DebuggerStepThroughAttribute()>
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = Form1
Me.MainForm = GameWindow
End Sub
End Class
End Namespace

View File

@ -0,0 +1,21 @@
Public Class Player
Public Property hand As New List(Of Card)
Public Property total As Integer
Public Property ingame As Boolean
Public Property winType As WinCondition
Public Sub Start()
hand.Clear()
total = 0
ingame = True
winType = WinCondition.NormalWin
End Sub
End Class
Public Class Dealer
Inherits Player
Public Property limit As Integer = 17
Public Property hidden As (String, String)
Public Property trueTotal As Integer
End Class

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

View File

@ -0,0 +1,38 @@
Public Class Utilities
Public Shared Function Shuffle(list)
Dim max As Integer = list.Count - 1
Dim random As New Random()
For x = 0 To max
Dim rand As Integer = random.Next(0, max)
Dim temp = list(x)
list(x) = list(rand)
list(rand) = temp
Next
Return list
End Function
Public Shared Function GetCardImage(card As (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
Case "K"
num = "king"
Case "Q"
num = "queen"
Case "J"
num = "jack"
Case "A"
num = "ace"
End Select
Dim img As String = suit + "_" + num
Try
Return My.Resources.ResourceManager.GetObject(img.ToLower())
Catch
Return My.Resources.blue
End Try
End Function
End Class

3
README.md 100644
View File

@ -0,0 +1,3 @@
# BlackjackGUI
A simple Blackjack game to help learn VB.net and its GUI interactions.