Click here to Skip to main content
15,507,266 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey everyone, I was hoping that someone would be able to help me, I have found a way to randomly display cards but I have no idea how to give the cards a score value to calculate the players score, I tried using a select case, but to no avail. This is my entire code so far...

VB
Option Strict Off

Public Class FrmBlackjack
    ' These are the variable declarations for my program..
    Public playerScore As Integer = 0
    Public dealerScore As Integer = 0
    Public wins As Integer = 0
    Public losses As Integer = 0
    Public ties As Integer = 0

    ' Here i am declaring my array to hold the deck of 52 cards and under that is the declaration for the random class.
    Public playingCards(51) As String
    Dim ranCard As Random = New Random()

    ' This is the form load subrootine.
    Private Sub FrmBlackjack_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' When the game is loaded for the first time, only the "New game" and the "Quit" buttons will be
        ' visible.
        BtnDeal.Visible = False
        BtnHit.Visible = False
        BtnStay.Visible = False
        GroupBox1.Visible = False

    End Sub

    ' When the user selects the "New game" button, the relevent picturBoxex will be loaded with images
    ' form the "RandomCards()" method.
    Private Sub BtnNewGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNewGame.Click

        ' Here i am gaing to set the enabled of the "New game" button to false so that the player continue
        ' with the game and only select "New game" once the sessions are over.
        BtnNewGame.Enabled = False
        BtnDeal.Visible = True
        BtnHit.Visible = True
        BtnStay.Visible = True
        GroupBox1.Visible = True

        ' Here i am setting the players score, wins, losses and ties to their respecive results.
        LblPlayerScore.Text = "Your current score is : " & playerScore
        LblPlayerWins.Text = "Your current total wins are : " & wins
        LblPlayerLosses.Text = "Your current total losses are : " & losses
        LblPlayerTies.Text = "Your current total ties are : " & ties

        ' These are the players starting cards being loaded into the picturboxes from the "RandomeCards()" method.
        PicBoxPlayerCard1.Image = Image.FromFile("../../Images/" & RandomCards() & ".gif")
        PicBoxPlayerCard2.Image = Image.FromFile("../../Images/" & RandomCards() & ".gif")

        ' These are the dealers cards being loaded into the dealers picturboxes from the "RandomeCards()" method.
        PicBoxDealerCard1.Image = Image.FromFile("../../Images/backOfCard.gif")
        PicBoxDealerCard2.Image = Image.FromFile("../../Images/" & RandomCards() & ".gif")

    End Sub

    Private Sub BtnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnQuit.Click
        Application.Exit()
    End Sub

    Private Sub BtnDeal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDeal.Click

    End Sub

    Private Sub BtnHit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnHit.Click

    End Sub

    Private Sub BtnStay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStay.Click

    End Sub

    Public Function RandomCards()
        ' Here i am declaring a variable to return the random card from the array.
        Dim card As String

        ' This is the array contaning all the cards.
        playingCards(0) = "c2"
        playingCards(1) = "c3"
        playingCards(2) = "c4"
        playingCards(3) = "c5"
        playingCards(4) = "c6"
        playingCards(5) = "c7"
        playingCards(6) = "c8"
        playingCards(7) = "c9"
        playingCards(8) = "ct"
        playingCards(9) = "cj"
        playingCards(10) = "cq"
        playingCards(11) = "ck"
        playingCards(12) = "ca"
        playingCards(13) = "d2"
        playingCards(14) = "d3"
        playingCards(15) = "d4"
        playingCards(16) = "d5"
        playingCards(17) = "d6"
        playingCards(18) = "d7"
        playingCards(19) = "d8"
        playingCards(20) = "d9"
        playingCards(21) = "dt"
        playingCards(22) = "dj"
        playingCards(23) = "dq"
        playingCards(24) = "dk"
        playingCards(25) = "da"
        playingCards(26) = "h2"
        playingCards(27) = "h3"
        playingCards(28) = "h4"
        playingCards(29) = "h5"
        playingCards(30) = "h6"
        playingCards(31) = "h7"
        playingCards(32) = "h8"
        playingCards(33) = "h9"
        playingCards(34) = "ht"
        playingCards(35) = "hj"
        playingCards(36) = "hq"
        playingCards(37) = "hk"
        playingCards(38) = "ha"
        playingCards(39) = "s2"
        playingCards(40) = "s3"
        playingCards(41) = "s4"
        playingCards(42) = "s5"
        playingCards(43) = "s6"
        playingCards(44) = "s7"
        playingCards(45) = "s8"
        playingCards(46) = "s9"
        playingCards(47) = "st"
        playingCards(48) = "sj"
        playingCards(49) = "sq"
        playingCards(50) = "sk"
        playingCards(51) = "sa"

        ' Here i am loading the randomly seleccted card into the "card" variable.
        card = playingCards(ranCard.Next(playingCards.Length))

        ' This is the select...case to try and load the playing score acording to the randome card.
        Select Case card
            Case "c2, d2, h2, s2"
                playerScore += 2
            Case "c3, d3, h3, s3"
                playerScore += 3
            Case "c4, d4, h4, s4"
                playerScore += 4
            Case "c5, d5, h5, s5"
                playerScore += 5
            Case "c6, d6, h6, s6"
                playerScore += 6
            Case "c7, d7, h7, s7"
                playerScore += 7
            Case "c8, d8, h8, s8"
                playerScore += 8
            Case "c9, d9, h9, s9"
                playerScore += 9
            Case "ct, cj, cq, ck, dt, dj, dq, dk, ht, hj, hq, hk, st"
                playerScore += 10
        End Select

        ' Returning th card variable.
        Return card

    End Function

    Private Function CalculateScore()

    End Function
End Class
Posted
Comments
Sergey Alexandrovich Kryukov 9-Apr-14 16:05pm    
I'm sorry, there is nothing to discuss here. Programming questions start when there is some programming. This is not. You just manually assign values to playingCards(x), just repeating the same statement with different index and value. This is opposite to programming.
—SA

1 solution

You're going about this the wrong way ...
Think about a deck of cards ... there are suites ...
Dim Suites As String() = New String() {"Spade", "Heart", "Club", "Diamond"}

and there are the face values
Dim CardNames() As String = New String() {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}

This lends itself to KeyValuePairs - see dotnetperls.com - keyvaluepairs[^]
Then you have the deck as a whole ... think of this as a list of the cards, which we have already discovered are "keyvaluepairs" (see the same link above)
Dim Cards As List(Of KeyValuePair(Of String, Integer)) = New List(Of KeyValuePair(Of String, Integer))

Because each card is a keyvaluepair of it's "name" = Suite + face title, plus it's nominal value
Dim ACard As KeyValuePair(Of String, Integer)
we can fill our deck of cards like this
VB
Dim ACard As KeyValuePair(Of String, Integer)
For s = 0 To 3
    Dim suite As String = Suites(s)    'I've declared Suites, Cardnames and Cards at the Form level

    'in BlackJack, and all picture cards are worth 10 so ...
    For i As Integer = 1 To 13
        ACard = New KeyValuePair(Of String, Integer)(suite + CardNames(i - 1), IIf(i >= 10, 10, i))

        'you might want to change the "value" of an Ace to 11 ...
        'it can be either 1 or 11  but most card players hope for that A+10 combo
        'or you might want to deal with that situation later ... you are going to know you have an Ace
        If i = 1 Then ACard = New KeyValuePair(Of String, Integer)(suite + CardNames(i - 1), 11)
        Cards.Add(ACard)
    Next
Next

'We now have a "deck" of cards - each has a unique identifier, a "name" and a value
'This output shows the cards ...
For i As Integer = 0 To Cards.Count - 1
    Debug.Print(i.ToString() + " : " + Cards(i).Key + " : " + Cards(i).Value.ToString())
Next

(If you are not familiar with the ternary operator iif here is a reference[^])

That Debug.Print bit at the end is going to produce
0 : SpadeAce : 11
1 : Spade2 : 2
2 : Spade3 : 3
3 : Spade4 : 4
4 : Spade5 : 5
5 : Spade6 : 6
...
46 : Diamond8 : 8
47 : Diamond9 : 9
48 : Diamond10 : 10
49 : DiamondJack : 10
50 : DiamondQueen : 10
51 : DiamondKing : 10

So hopefully you can see that we have a "deck of cards", each card has a unique identifier (the first number), a name (e.g. SpadeAce) and a Value (11,2,3 etc)

Now when I get a random card out of the deck I'm going to use this function
VB
Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
    'The Static bit here means that you will get new numbers each time you hit
    'the button. If you have a new System.Random each time you will get either
    'the same number or numbers that are not random (Random is based on the system clock)
    Static Generator As System.Random = New System.Random()
    Return Generator.Next(Min, Max)
End Function

[NOTE - I found this some time ago via Google but now can't find who to credit it to - happy to update this solution if anyone knows who deserves the credit]

So my button
VB
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    'This bit is going to pick a card at random
    Dim ranCard As Integer = GetRandom(0, Cards.Count - 1)
    Debug.Print("Card is : " + Cards(ranCard).Key + " : Value = " + Cards(ranCard).Value.ToString())
End Sub

produces results like this ...
Card is : Club4 : Value = 4
Card is : Club7 : Value = 7
Card is : DiamondKing : Value = 10
Card is : Diamond9 : Value = 9
Card is : Diamond2 : Value = 2
Card is : Heart6 : Value = 6


Now that I've given you a solution I do need to justify my comment "You're going about this the wrong way"

Firstly ... in forums like this, only post the relevant code ... you posted a lot of empty functions ... that just gets in the way of people trying to understand what your problem is and they are less likely to try and help (human nature!)

Next have a good look at your function RandomCards() ... when you have hard-coded values like that you want to think about moving them out of a function that can be called many times and into some sort of initialisation routine.

Lastly, you went for a single-dimension array ... that can only hold one type of information - hence your problem with assigning values. Research some of the collection types available in .NET ... Lists and Dictionaries (as you can see above) can be very powerful
 
Share this answer
 
v2
Comments
[no name] 9-Apr-14 19:48pm    
Very good.
CHill60 9-Apr-14 19:50pm    
Thank you ... better than my 'C' eh? :-)
JLSto 11-Apr-14 7:45am    
Thanks a lot man, this really helps me a lot, there is a bit more I would like to ask, but first I just want to thank you for helping me and giving me good criticism and not just turning away.
so here goes my questions...

1. how would I link my different images to each card (eg. 0 : SpadeAce : 11) how would I link the ace card image to this ?

If you could help it would be great, maby evan an email ?
CHill60 12-Apr-14 5:20am    
At this stage I would be thinking to myself ... Hmm, I need to store more stuff than I realised to represent each card ... so I'll write a Class to represent a Playing Card ...
Let's call it ACard and it will have properties for key, Suite, FaceValue and Value (notice how we can split the suite away from the rest of the "name" now) and also a link to an appropriate image.

Then the collection that forms the Deck becomes Dim Cards As List(Of ACard))
JLSto 11-Apr-14 11:24am    
Thanks that was very helpful, and I enjoyed your explanations.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900