Hi everyone!
Just playing around with VB and have made a program for playing "pairs". The program displays lots of face down cards, then the user can click on them to reveal their face and try to find pairs.
If the user finds a pair, the cards should stay face up, however if they do not match, they should be returned to facing down. This needs to be done by hiding or showing the "reverse of card" picture
I have tried to do this by usingin the "Else" block in the "comparecard" function, but it returns errors!Code:play.carddown(0).Visible = True
What do I need to do?
The pictures need to be loaded dynamically through an array...
The code I have so far is as follows...
Form1...
play.vb...Code:Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
play.Show()
Me.Close()
End Sub
End Class
module2.vb...Code:'Imports mysql.Data.MySqlClient
Public Class play
Public carddown(52) As System.Windows.Forms.PictureBox
Public cardup(52) As System.Windows.Forms.PictureBox
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i As Integer = 1 To 52
carddown(i) = New System.Windows.Forms.PictureBox()
carddown(i).Image = System.Drawing.Image.FromFile("N:\CS2TX6\spring\YamalaCard\0.png")
carddown(i).Height = 100
carddown(i).Width = 71
If i = 1 Then
carddown(i).Top = 100
carddown(i).Left = 29
ElseIf i > 1 And i < 14 Then
carddown(i).Top = 100
carddown(i).Left = carddown(i - 1).Left + carddown(i - 1).Width + 5
ElseIf i = 14 Then
carddown(i).Top = 211
carddown(i).Left = 29
ElseIf i > 14 And i < 27 Then
carddown(i).Top = 211
carddown(i).Left = carddown(i - 1).Left + carddown(i - 1).Width + 5
ElseIf i = 27 Then
carddown(i).Top = 317
carddown(i).Left = 29
ElseIf i > 27 And i < 40 Then
carddown(i).Top = 317
carddown(i).Left = carddown(i - 1).Left + carddown(i - 1).Width + 5
ElseIf i = 40 Then
carddown(i).Top = 423
carddown(i).Left = 29
ElseIf i > 40 And i < 53 Then
carddown(i).Top = 423
carddown(i).Left = carddown(i - 1).Left + carddown(i - 1).Width + 5
Else
End If
AddHandler carddown(i).Click, AddressOf Me.carddown_click
Me.Controls.Add(carddown(i))
Next
shuffle()
For n = 0 To 51
cardup(n) = New System.Windows.Forms.PictureBox()
cardup(n).Height = 100
cardup(n).Width = 71
cardup(n).Image = System.Drawing.Image.FromFile("N:\CS2TX6\spring\YamalaCard\cardface" & "\" & nArr(n) & ".jpg")
cardup(n).Name = "Card " + n.ToString()
If n = 0 Then
cardup(n).Top = 100
cardup(n).Left = 29
ElseIf n > 0 And n < 13 Then
cardup(n).Top = 100
cardup(n).Left = cardup(n - 1).Left + cardup(n - 1).Width + 5
ElseIf n = 13 Then
cardup(n).Top = 211
cardup(n).Left = 29
ElseIf n > 13 And n < 26 Then
cardup(n).Top = 211
cardup(n).Left = cardup(n - 1).Left + cardup(n - 1).Width + 5
ElseIf n = 26 Then
cardup(n).Top = 317
cardup(n).Left = 29
ElseIf n > 26 And n < 39 Then
cardup(n).Top = 317
cardup(n).Left = cardup(n - 1).Left + cardup(n - 1).Width + 5
ElseIf n = 39 Then
cardup(n).Top = 423
cardup(n).Left = 29
ElseIf n > 39 And n < 52 Then
cardup(n).Top = 423
cardup(n).Left = cardup(n - 1).Left + cardup(n - 1).Width + 5
Else
End If
cardup(n).Tag = n.ToString()
AddHandler cardup(n).Click, AddressOf Me.cardselect_click
Me.Controls.Add(cardup(n))
Next
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
AboutBox1.Show()
End Sub
Public Sub carddown_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim Parent = sender
parent.visible = False
End Sub
Public Sub cardselect_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim grandparent = sender
Dim TempInt = grandparent.Tag()
grandparent.visible = True
Dim arrayindex As VariantType
arrayindex = TempInt
MsgBox("Card " & TempInt & " clicked " & nArr(arrayindex))
MsgBox(comparecard(nArr(arrayindex), CardTwo()))
'If comparecard() = "same" Then
' Parent.visible = True
'Else
' Parent.visible = False
'End If
End Sub
End Class
Thanks for your help everyone!Code:Module Module2
Public secondcard As String
Public nArr(51) As Long
Function shuffle()
Dim n As Long
Dim nTemp As Long, nRand As Long
For n = 0 To 51
nArr(n) = n + 1
Next
Randomize()
For n = 52 To 1 Step -1
nRand = Int(Rnd() * n)
nTemp = nArr(nRand)
nArr(nRand) = nArr(n - 1)
nArr(n - 1) = nTemp
Next
End Function
Function CardTwo() As Boolean
Static firstcard
If firstcard Then
CardTwo = True
Else
CardTwo = False
End If
firstcard = Not (firstcard)
End Function
Public Function comparecard(ByRef nArr As String, ByVal Check As Boolean) As String
Static lastcard
Dim cardvalue
comparecard = "NA"
cardvalue = nArr
If Check Then
comparecard = IIf((cardvalue = lastcard + 26 Or cardvalue = lastcard - 26), "Same", "Different")
lastcard = cardvalue
'For i As Integer = 1 To 26
' form1.Label3.Text = ("", (i))
'Next
Else
lastcard = cardvalue
'play.carddown_click() = True
play.carddown(0).Visible = False
End If
End Function
'Public Function comparecard(ByRef nArr As String, ByVal Check As Boolean) As String
' Static lastcard
' comparecard = "NA"
' If nArr = lastcard + 26 Or nArr = lastcard - 26 Then
' MsgBox("Match")
' carddown(firstcard).Visible = False
' cardup(firstcard).Visible = True
' carddown(secondcard).Visible = False
' cardup(secondcard).Visible = True
' Else
' MsgBox("Different")
' carddown(firstcard).Visible = False
' carddown(secondcard).Visible = True
' End If
'End Function
'Public Function Divide(ByVal n1 As Integer, ByVal n2 As Integer) As Object
' If n2 = 0 Then
' MsgBox("Cannot divide by 0")
' Else
' MsgBox(n1 / n2)
' End If
'End Function
'Dim connectionString As String = "SERVER=mysql.sse.reading.ac.uk;DATABASE=CS2TX6;Uid=;Pwd="
'Dim conn As New MySqlConnection(connectionString)
'Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' conn.Open()
' Dim cmd As MySqlCommand
' cmd = New MySqlCommand("INSERT INTO registrations (user_id,reg_machine) VALUES (?uid,?regm)", conn)
' cmd.Parameters.Add("?uid", MySqlDbType.VarChar, 40, "user_id")
' cmd.Parameters.Add("?regm", MySqlDbType.VarChar, 40, "reg_machine")
' Dim id As Integer
' id = get_user_id("test user2")
' cmd.Parameters("?uid").Value = id
' cmd.Parameters("?regm").Value = "my PC serial"
' cmd.ExecuteNonQuery()
'End Sub
'Private Function get_user_id(ByVal name As String)
' Dim da As New MySqlDataAdapter("Select id from users where name=?name", conn)
' da.SelectCommand.Parameters.Add("?name", MySqlDbType.VarChar, 40, "name")
' da.SelectCommand.Parameters("?name").Value = name
' Dim ds As New DataSet("users")
' da.Fill(ds)
' Dim t As DataTable = ds.Tables(0)
' Return t.Rows(0).Item("id")
'End Function
End Module
James
