It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Visual Basic Forum

Card Game Solution

Moderator: dafunkymunky

Card Game Solution

Postby samlemx18 on Thu May 20, 2010 8:50 pm

Hi all,
I need help. The end of my semester is coming up and i have alot of VB assignments left to do. I did all of this Card Game Assignment once but now the book says that I have to change my code to make the card game shuffle the cards only after the first 12 cards have been displayed. I was trying to use a loop to keep it going until all the cards (1-12) were displayed. I have 6 assignments left to do in a week and dont have much time to spend on just one assignment so any help would be so greatly appreciated. Heres my code:
Code: Select all
Option Explicit On
Option Strict On
Option Infer Off

Public Class MainForm

    Private cardDeck As New DeckOfCards
    Private Sub DetermineHighest(ByVal selection As String)
        'turns the cards face up, determines whether the plyer selected the highest card
        'displays the number of correct and incorrect selections

        'counter variables
        Static numCorrect As Integer
        Static numIncorrect As Integer

        Dim card1Value As Integer
        Dim card2Value As Integer
        Dim highest As String = String.Empty

        'disable the picture boxes
        card1PictureBox.Enabled = False
        card2PictureBox.Enabled = False

        'displays cards face up
        card1PictureBox.Image = _
        Image.FromFile(cardDeck.Cards(0).Front)
        card2PictureBox.Image = _
        Image.FromFile(cardDeck.Cards(1).Front)

        'convert face values to numbers
        card1Value = GetNumber(cardDeck.Cards(0).FaceValue)
        card2Value = GetNumber(cardDeck.Cards(1).FaceValue)

        'determine the highest card
        If card1Value > card2Value Then
            highest = "card1"
        Else
            highest = "card2"
        End If

        'determine whether the player's choice is correct
        If selection = highest Then
            msgLabel.Text = "Correct!"
            numCorrect = numCorrect + 1
        Else
            msgLabel.Text = "Sorry!"
            numIncorrect = numIncorrect + 1
        End If

        'displays counter
        correctLabel.Text = numCorrect.ToString
        incorrectLabel.Text = numIncorrect.ToString
    End Sub
    Private Function GetNumber(ByVal faceChar As String) As Integer
        ' returns the numeric value of the Card object

        Dim number As Integer

        Select Case faceChar
            Case "2", "3", "4", "5", "6", "7", "8", "9", "10"
                Integer.TryParse(faceChar, number)
            Case "J"
                number = 11
            Case "Q"
                number = 12
            Case "K"
                number = 13
            Case "A"
                number = 14
        End Select

        Return number
    End Function

    Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()
    End Sub

    Private Sub dealButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles dealButton.Click
        'shuffles the cards, then displays 2 cards

        cardDeck.ShuffleCards()

        'display the back of the first 2 cards
        card1PictureBox.Image = _
        Image.FromFile(cardDeck.Cards(0).Back)
        card2PictureBox.Image = _
        Image.FromFile(cardDeck.Cards(1).Back)

        'enable the picture boxes
        card1PictureBox.Enabled = True
        card2PictureBox.Enabled = True

        'clear the msg label
        msgLabel.Text = String.Empty
    End Sub

    Private Sub card1PictureBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles card1PictureBox.Click
        Call DetermineHighest("card1")
    End Sub

    Private Sub card2PictureBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles card2PictureBox.Click
        Call DetermineHighest("card2")
    End Sub
End Class


and my code that is in a different class where the cards are actually shuffled:
Code: Select all
Public Class DeckOfCards
    Private _cards(12) As Card

    Public ReadOnly Property Cards(ByVal subscript As Integer) As Card
        Get
            Return _cards(subscript)
        End Get
    End Property

    Public Sub New()
        _cards(0) = New Card("AH.tif", "cardback.tif", "A", "H")
        _cards(1) = New Card("KH.tif", "cardback.tif", "K", "H")
        _cards(2) = New Card("QH.tif", "cardback.tif", "Q", "H")
        _cards(3) = New Card("JH.tif", "cardback.tif", "J", "H")
        _cards(4) = New Card("10H.tif", "cardback.tif", "10", "H")
        _cards(5) = New Card("9H.tif", "cardback.tif", "9", "H")
        _cards(6) = New Card("8H.tif", "cardback.tif", "8", "H")
        _cards(7) = New Card("7H.tif", "cardback.tif", "7", "H")
        _cards(8) = New Card("6H.tif", "cardback.tif", "6", "H")
        _cards(9) = New Card("5H.tif", "cardback.tif", "5", "H")
        _cards(10) = New Card("4H.tif", "cardback.tif", "4", "H")
        _cards(11) = New Card("3H.tif", "cardback.tif", "3", "H")
        _cards(12) = New Card("2H.tif", "cardback.tif", "2", "H")
    End Sub

    Public Sub ShuffleCards()
        Dim randomGenerator As New Random
        Dim randNum1 As Integer
        Dim randNum2 As Integer
        Dim temp As New Card


        For x As Integer = 1 To 25
            randNum1 = randomGenerator.Next(0, 13)
            randNum2 = randomGenerator.Next(0, 13)
            temp = Cards(randNum1)
            _cards(randNum1) = _cards(randNum2)
            _cards(randNum2) = temp
        Next x
    End Sub
End Class


again thanks so much.

Sam
samlemx18
 
Posts: 7
Joined: Sat Mar 20, 2010 11:02 pm

Who is online

Users browsing this forum: No registered users and 2 guests