Advertisement
ssoni

memory.vb (done)

Jun 1st, 2022
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. Public Class Form1
  2. Public cardsarray(15) As PictureBox
  3. Public pics(15) As Image
  4. Public isFirstPick As Boolean
  5. Public firstCardNumber As Integer
  6.  
  7. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  8. cardsarray = {PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5, PictureBox6, PictureBox7, PictureBox8, PictureBox9, PictureBox10, PictureBox11, PictureBox12, PictureBox13, PictureBox14, PictureBox15, PictureBox16}
  9. Call loadDeck()
  10. Call shuffleDeck()
  11. Call resetGame()
  12.  
  13. End Sub
  14.  
  15. Public Sub loadDeck()
  16. pics(0) = My.Resources.card1
  17. pics(1) = pics(0)
  18. pics(2) = My.Resources.card2
  19. pics(3) = pics(2)
  20. pics(4) = My.Resources.card3
  21. pics(5) = pics(4)
  22. pics(6) = My.Resources.card4
  23. pics(7) = pics(6)
  24. pics(8) = My.Resources.card5
  25. pics(9) = pics(8)
  26. pics(10) = My.Resources.card6
  27. pics(11) = pics(10)
  28. pics(12) = My.Resources.card7
  29. pics(13) = pics(12)
  30. pics(14) = My.Resources.card8
  31. pics(15) = pics(14)
  32. End Sub
  33.  
  34. Public Sub shuffleDeck()
  35. Dim tmp As Image
  36. Dim x As Integer
  37. Dim y As Integer
  38. Dim generator As New Random
  39.  
  40. For x = 0 To 15
  41. y = generator.Next(0, 15)
  42. tmp = pics(x)
  43. pics(x) = pics(y)
  44. pics(y) = tmp
  45. Next
  46.  
  47. End Sub
  48.  
  49. Public Sub resetGame()
  50.  
  51. 'make all hidden cards visible again, with back pic
  52. For x = 0 To 15
  53. cardsarray(x).Visible = True
  54. cardsarray(x).Image = My.Resources.back
  55. Next
  56.  
  57. Call shuffleDeck()
  58. isFirstPick = True
  59.  
  60. End Sub
  61.  
  62. Private Sub PictureBoxALL_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click, PictureBox2.Click, PictureBox3.Click, PictureBox4.Click, PictureBox5.Click, PictureBox6.Click, PictureBox7.Click, PictureBox8.Click, PictureBox9.Click, PictureBox10.Click, PictureBox11.Click, PictureBox12.Click, PictureBox13.Click, PictureBox14.Click, PictureBox15.Click, PictureBox16.Click
  63. Dim index As Integer
  64.  
  65. 'get which card number was clicked
  66. index = Array.IndexOf(cardsarray, sender)
  67.  
  68. 'show the picture
  69. cardsarray(index).Image = pics(index)
  70. cardsarray(index).Refresh()
  71.  
  72. 'do the matching logic
  73. If isFirstPick = True Then
  74. firstCardNumber = index
  75. isFirstPick = False
  76. Else
  77. Threading.Thread.Sleep(1000)
  78. If (pics(firstCardNumber) Is pics(index)) Then
  79. cardsarray(firstCardNumber).Visible = False
  80. cardsarray(index).Visible = False
  81. Call checkDone()
  82. Else
  83. cardsarray(firstCardNumber).Image = My.Resources.back
  84. cardsarray(index).Image = My.Resources.back
  85. End If
  86. isFirstPick = True
  87. End If
  88. End Sub
  89.  
  90. Public Sub checkDone()
  91. Dim done As Boolean
  92. done = True
  93.  
  94. For x = 0 To 15
  95. If cardsarray(x).Visible = True Then
  96. done = False
  97. End If
  98. Next
  99.  
  100. If done = True Then
  101. MsgBox("You win!")
  102. Call resetGame()
  103. End If
  104. End Sub
  105.  
  106. End Class
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement