• (2 Pages)
  • +
  • 1
  • 2

Playing Cards How to create a set of playing cards. (Net 3.5+) Rate Topic: ***** 3 Votes

#16 IdeAzRight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-December 12

Posted 14 December 2012 - 07:03 PM

I referenced the code provided and am trying to create a BlackJack Game. Everything has ran smoothing up until trying to assign each card added to your hand, a value. I think it's being difficult because it's just an image passed from 'public playing card class' and not actually the Cards string? any suggestions would be greatly appreciated, thanks. This is where i'm at so far, it's a little sketchy because i haven't really organized it yet.

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


Button1 displays 2 hands with 2 cards, Index 0 is the dealer and index 1 is the player (you) when you press the hit button it adds a card to the players hand. This is why i need each cards value to see if it busts or not. Then i'll use an if statement to see if a card should be added to the dealer if it's not passed 17 after you decide to stay. Everythings worked fine up until this point. Thanks again!
Was This Post Helpful? 0
  • +
  • -

#17 JohnF84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-April 13

Posted 30 April 2013 - 12:08 PM

[quote name='AdamSpeight2008' date='28 November 2008 - 02:50 PM' timestamp='1227909048' post='475888']
This tutorial is on how to create your pack of cards, which you can use a basis for other card games.
It'll touch on the aspects of Object Orientated Design (O.O.D.)
You'll also be using LINQ, RegEx (Regular Expressions) and creating custom sorts.

Awesome code. How could this be used for the game of WAR?
Was This Post Helpful? 0
  • +
  • -

#18 Khilborn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-January 14

Posted 20 January 2014 - 07:39 AM

Can someone explain in better detail the NameOfHand function? Specifically what this line is

Case System.Text.RegularExp<b></b>ressions.Regex.Match(f, "0*10*10*10*10*10*").Success


Thanks
Was This Post Helpful? 0
  • +
  • -

#19 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Posted 20 January 2014 - 09:39 AM

If you have 5 different cards of the same suit is a Flush
Was This Post Helpful? 0
  • +
  • -

#20 Khilborn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-January 14

Posted 27 January 2014 - 12:57 PM

What is Dim a As New List(Of String) and Dim b As New List(Of String)

mod: removed excessive quote..

edit:
(post #3)

This post has been edited by modi123_1: 27 January 2014 - 01:23 PM
Reason for edit:: removed excessive quote

Was This Post Helpful? 0
  • +
  • -

#21 Khilborn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-January 14

Posted 27 January 2014 - 01:13 PM

View PostKhilborn, on 27 January 2014 - 12:57 PM, said:

What is Dim a As New List(Of String) and Dim b As New List(Of String)

mod: removed excessive quote..


The mod removed the quoted code so it made my original question meaningless. The two lines are from the Texas Holdem like play snippets.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2