VB.Net OOP BlackJack
Overview
BlackJack is a card game for two or more players, but in this version, it's just the user versus the computer.
Game Core
The core of the game consists of the BlackJack (game) class, Player class, Deck class, Card class, and a Count utility class.
- BlackJack class: A new instance of this is instantiated in the GUI Form_Shown event after the user has entered their name. This class contains NewGame, hit, and stick methods with Public Access, as these are the methods called from the code behind the GUI. The class also contains a Private Access play method. The hit, stick, and play methods work together, recursively prompting the user to hit or stick while the user holds a valid hand (i.e. below or equal to 21). Sticking or busting, when the user has first move passes play to the computer, which must keep hitting while its hand' sum is below 17. When both players have stuck or bust, the computer's hand is revealed. Occasionally when the computer has the first move, it can bust and the user wins by default. If the user has first move and busts, the computer wins by default.
- Player class: The user and the computer are both represented by instances of the Player class which has name and hand Properties as well as a hasStuck Property and a getHandAsString Function that returns a textual description of the hand.
- Deck class: This holds the List(Of Card) that represents the deck of cards. It has createDeck, shuffleDeck, and deal methods.
- Card class: Each card in the deck is represented by an instance of this class. It has suit, facevalue, and image Properties that describe and depict the card.
- Count class: This is a utility class that contains five Shared Functions used in scoring and determining the winning hand.
The GUI
The GUI consists of a Form containing a Label for textual output, two handViewer UserControls that display the hands pictorially, and three Buttons used in gameplay. In this game I've reused my InputDialog class which requests the user's name.
- handViewer UserControl: This is a simple control that displays the player's name and shows their hand pictorially. There are two of these controls, one for each player.
handViewer code
Public Class handViewer
Public Sub setName(name As String)
Label1.Text = name
End Sub
Public Sub setImage(Optional hand As List(Of Card) = Nothing)
Dim img As Bitmap = Nothing
If Not hand Is Nothing Then
img = New Bitmap(105 + If(hand.Count > 2, (hand.Count - 2) * 15, 0), 120)
Dim gr As Graphics = Graphics.FromImage(img)
Dim m As New Drawing2D.Matrix
m.RotateAt(-20, Point.Empty)
gr.Transform = m
gr.DrawImage(hand(0).getImage, New Point(-8, 22))
gr.ResetTransform()
Dim startX As Integer = 33
For x As Integer = 1 To hand.Count - 1
gr.DrawImage(hand(x).getImage, New Point(startX, 17))
startX += 15
Next
End If
PictureBox1.Image = img
End Sub
Private Sub PictureBox1_Resize(sender As Object, e As EventArgs) Handles PictureBox1.Resize
Me.Width = Math.Max(PictureBox1.Width, Label1.Width) + 6
End Sub
End Class
The Form code
Public Class Form1
Private WithEvents game As BlackJack
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
Dim d As New InputDialog
'this shows a modal Dialog with a prompt, and a title, with no DefaultResponse
If d.ShowDialog("Enter your name", "OOP BlackJack") = DialogResult.OK Then
Dim playerName As String = d.returnedValue
HandViewer1.setName(playerName)
HandViewer2.setName( "Computer" )
game = New BlackJack("Computer", playerName)
btnNewGame.PerformClick()
Else
Application. Exit ()
End If
End Sub
Private Sub btnNewGame_Click(sender As Object, e As EventArgs) Handles btnNewGame.Click
HandViewer1.setImage()
HandViewer2.setImage()
btnHit.Enabled = True
btnStick.Enabled = True
btnNewGame.Enabled = False
game.NewGame()
End Sub
Private Sub btnStick_Click(sender As Object, e As EventArgs) Handles btnStick.Click
game.stick()
End Sub
Private Sub btnHit_Click(sender As Object, e As EventArgs) Handles btnHit.Click
game.hit()
End Sub
Private Sub game_displayHand(hand As List(Of Card), player As Integer) Handles game.displayHand
If player = 1 Then
HandViewer1.setImage(hand)
Else
HandViewer2.setImage(hand)
End If
End Sub
Private Sub game_displayText(text As String) Handles game.displayText
Label1.Text &= text & Environment.NewLine & Environment.NewLine
End Sub
Private Sub game_clearText() Handles game.clearText
Label1.Text = ""
End Sub
Private Sub game_isOver() Handles game.isOver
btnHit.Enabled = False
btnStick.Enabled = False
btnNewGame.Enabled = True
End Sub
Private Sub HandViewer1_Resize(sender As Object, e As EventArgs) Handles HandViewer1.Resize
HandViewer2.Left = HandViewer1.Right + 6
End Sub
End Class
Downloads
Resources
Articles related to game programming
VB.Net - WordSearch
VB.Net - Vertex
VB.Net - Perspective
VB.Net - MasterMind
VB.Net - Numbers Game
VB.Net - HangMan
Console BlackJack - VB.Net | C#
TicTacToe - VB.Net | C#
OOP Conway's Game of Life - VB.Net | C#
OOP Sudoku - VB.Net | C#
OctoWords VB.Net | C#
OOP Buttons Guessing Game VB.Net | C#
OOP Tangram Shapes Game VB.Net | C#
VB.Net - Three-card Monte
VB.Net - Split Decisions
VB.Net - Pascal's Pyramid
VB.Net - Random Maze Games
(Office) Wordsearch Creator
VB.Net - Event Driven Programming - LockWords Game
C# - Crack the Lock
VB.Net - Totris