Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

passing a value with cards for blackjack game

$
0
0
Hi, I used an Adamspeight2008 tutorial dealing with playing cards on a forum tutorial located here to generate cards, a deck, and hands. I modified the code to deal 2 cards with 2 hands to make a blackjack game. everything has ran smoothly up until i need to keep a running total of what each card is worth. Any suggestions would be great. Thanks!

Original tutorial code located here : http://www.dreamincode.net/forums/topic/74005-playing-cards/

My code:

Public Class frmGame
    Protected mHand As List(Of PlayingCard)
    Dim TheDeck As DeckOfCards
    Dim Hands(NumberOfHands - 1) As HandOfCards
    Const NumberOfHands As Integer = 2


    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        If TheDeck IsNot Nothing Then
            For i = 0 To NumberOfHands - 1
                Hands(i).DrawHand(e.Graphics, 423, 250 + i * 65)
            Next
        End If


    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Console.WriteLine("--------")
        DealerCover.Visible = True
        btnHit.Visible = True
        btnStay.Visible = True


        TheDeck = New DeckOfCards
        TheDeck.ShuffleCards()
        TheDeck.ShuffleCards()
        'TheDeck.ShuffleCards()
        'TheDeck.ShuffleCards()
        For i = 0 To NumberOfHands - 1
            Hands(i) = New HandOfCards
            For c As Integer = 0 To 1
                Hands(i).AddCard(TheDeck.TakeCard)
            Next
        Next
        Me.Refresh()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub



    Private Sub btnHit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHit.Click

        Console.WriteLine("--------")
        Hands(1).AddCard(TheDeck.TakeCard)
        Me.Refresh()

    End Sub

End Class

Public Class HandOfCards
    Dim Handvalue As Integer = 0

    Protected mHand As List(Of PlayingCard)

    Public Sub AddCard(ByVal card As PlayingCard)
        mHand.Add(card)

    End Sub

    Public Function Card(ByRef index As Integer) As PlayingCard
        If index < 0 Or index > 4 Then Return Nothing
        Return mHand(index)
    End Function

    Public Sub New()
        mHand = New List(Of PlayingCard)

    End Sub

    Public Sub DrawHand(ByRef g As Graphics, ByRef x As Integer, ByRef y As Integer, Optional ByRef t As Boolean = True)
        For i As Integer = 0 To mHand.Count - 1
            mHand(i).DrawCard(g, 400 + 25 * i, y)
        Next
    End Sub

End Class

Public Class DeckOfCards

    Dim r As New Random(Now.Millisecond)
    Dim mDeck As New List(Of PlayingCard)
    Dim Suits() As String = {"S", "D", "C", "H"}
    Dim Faces() As String = {"2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"}


    Public Sub New()
        For Each Suit As String In Suits
            For Each Face As String In Faces.Reverse
                mDeck.Add(New PlayingCard(Suit, Face))
            Next
        Next
    End Sub

    Public Sub ShuffleCards()
        Dim rc As PlayingCard
        Dim rn As Integer
        For i As Integer = 0 To 52
            rn = r.Next(0, 51) Mod mDeck.Count
            rc = mDeck(rn)
            mDeck.RemoveAt(rn)
            mDeck.Add(rc)
        Next
    End Sub

    Public ReadOnly Property TakeCard() As PlayingCard
        Get

            Dim RemovedCard As PlayingCard = mDeck(0)
            mDeck.RemoveAt(0)
            Return RemovedCard
        End Get
    End Property
End Class

Public Class PlayingCard
    Dim CardPoints As Integer = 0
    Dim Suits() As String = {"S", "D", "C", "H"}
    Dim Faces() As String = {"2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"}
    Private mFace As String
    Private mSuit As String
    Protected cv As Integer = 0
    Protected sv As Integer = 0

    Public Sub New(ByVal tSuit As String, ByVal tFaceValue As String)
        mFace = tFaceValue
        mSuit = tSuit
        cv = Array.IndexOf(Of String)(Faces, mFace) '+ 1
        sv = Array.IndexOf(Of String)(Suits, mSuit) ' + 1
    End Sub

    Public ReadOnly Property Suit() As String
        Get
            Return mSuit
        End Get
    End Property

    Public ReadOnly Property Face() As String
        Get
            Return mFace
        End Get
    End Property


    Public Overrides Function ToString() As String

        Return mSuit & mFace
    End Function

    Public Sub DrawCard(ByRef g As Graphics, ByRef x As Integer, ByRef y As Integer)
        g.CompositingQuality = Drawing2D.CompositingQuality.GammaCorrected
        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
        g.DrawImage(New System.Drawing.Bitmap(System.Windows.Forms.Application.StartupPath & "\cards_png\" & Me.ToString & ".png"), x, y, 46, 64) '71, 96)
    End Sub

    Public ReadOnly Property CardValue() As Integer
        Get
            Return cv
        End Get
    End Property

    Public ReadOnly Property SuitValue() As Integer
        Get
            Return sv
        End Get
    End Property

End Class

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>