Matching pairs (cards) game array

jackandjones120

New member
Joined
Apr 14, 2012
Messages
2
Programming Experience
Beginner
Basically i need to create a card matching game which two players who take turn to play.

Each time they match a pair this adds 1 point to their score.

The cards start off showing a red picture card from file C:\Users\Programming\Pairs_Ass2\Cards\Red.png

In this folder there are 52 cards and a red and blue one.

i need to create a 2 dimensional array which randomises the cards from folder into picture boxes.

the code which i have so far is:

VB.NET:
Public Class Frm_Pairs

Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
Frm_About.Show()
End Sub

Private Sub NewGameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewGameToolStripMenuItem.Click
Dim Player1 As String
Dim Player2 As String
If Txt_Player1Name.Text = "" And Txt_Player2Name.Text = "" Then
Player1 = InputBox("What is your name Player 1?")
Txt_Player1Name.Text = Player1
Player2 = InputBox("What is your name Player 2?")
Txt_Player2Name.Text = Player2
ElseIf Txt_Player1Name.Text = "" Then
Player1 = InputBox("What is your name Player 1?")
Txt_Player1Name.Text = Player1
ElseIf Txt_Player2Name.Text = "" Then
Player2 = InputBox("What is your name Player 2?")
Txt_Player2Name.Text = Player2
Else
MessageBox.Show("Well done")
End If
End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
End
End Sub
End Class


at the line: MessageBox.Show("Well done") my code should put create a 2 dimensional array which randomises the cards from folder into picture boxes as i explained.


Please help, i am really stuck!


if you need any more information please ask


Thanks
 
Use a random shuffle algorithm

First, import the data from your file into 2 identical arrays. Then shuffle each array randomly, using the algorithm below. In this demo, I used integer arrays and assigned sequential values to each of them. In your case, you will use the text values to populate the arrays with identical string values. After shuffling each array, the demo displays the results in 2 listboxes to show how each deck was shuffled randomly. Using the array indexes, you can call up the 2 array elements to see whether or not they match.

The following code uses 2 decks. If you only need 1 deck, then use only setA, mixA and tempA with a single listbox. Your players can then take turns selecting the next index from the shuffled array deck of cards.

VB.NET:
    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x, mixA, mixB, tempA, tempB As Integer  'if string arrays used, tempA and TempB should be As String
        Dim num As Integer = 54
        Dim setA(num) As Integer    'This could be a String instead
        Dim setB(num) As Integer
        Dim randnum As Random = New Random()

        'Assign sequential values to array  'This could be imported from a text file instead
        For x = 1 To num
            setA(x) = x
            setB(x) = x
        Next x

        'Swap the array indexes randomly
        For x = num To 1 Step -1
            mixA = randnum.Next(1, x)
            mixB = randnum.Next(1, x)
            tempA = setA(mixA)
            tempB = setB(mixB)
            setA(mixA) = setA(x)
            setB(mixB) = setB(x)
            setA(x) = tempA
            setB(x) = tempB
        Next x

        'Display all the rearranged array values
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()
        For x = 1 To num
            ListBox1.Items.Add(setA(x))
            ListBox2.Items.Add(setB(x))
        Next x
    End Sub
 
Last edited:
First, import the data from your file into 2 identical arrays. Then shuffle each array randomly, using the algorithm below. In this demo, I used integer arrays and assigned sequential values to each of them. In your case, you will use the text values to populate the arrays with identical string values. After shuffling each array, the demo displays the results in 2 listboxes to show how each deck was shuffled randomly. Using the array indexes, you can call up the 2 array elements to see whether or not they match.

The following code uses 2 decks. If you only need 1 deck, then use only setA, mixA and tempA with a single listbox. Your players can then take turns selecting the next index from the shuffled array deck of cards.

VB.NET:
    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x, mixA, mixB, tempA, tempB As Integer  'if string arrays used, tempA and TempB should be As String
        Dim num As Integer = 54
        Dim setA(num) As Integer    'This could be a String instead
        Dim setB(num) As Integer
        Dim randnum As Random = New Random()

        'Assign sequential values to array  'This could be imported from a text file instead
        For x = 1 To num
            setA(x) = x
            setB(x) = x
        Next x

        'Swap the array indexes randomly
        For x = num To 1 Step -1
            mixA = randnum.Next(1, x)
            mixB = randnum.Next(1, x)
            tempA = setA(mixA)
            tempB = setB(mixB)
            setA(mixA) = setA(x)
            setB(mixB) = setB(x)
            setA(x) = tempA
            setB(x) = tempB
        Next x

        'Display all the rearranged array values
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()
        For x = 1 To num
            ListBox1.Items.Add(setA(x))
            ListBox2.Items.Add(setB(x))
        Next x
    End Sub


Hello, thanks for the replay it has really helped,

but i need to generate matching numbers (only once) (e.g. 1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9)

i need these upto a 6 by 6 grid of cards

they have to be pairs so that the user can match them together


thanks
 
I showed you how to create a random shuffled deck. I don't even understand what else you are trying to do. It's your game, and the rest is up to you to figure out the logic on how to make it work.
 
Back
Top