Buatlah Form sebagai berikut...
Setelah itu masukkan syntax berikut.....
Public Class Puzzel
Inherits System.Windows.Forms.Form
Dim btn(9) As Button
Const sz As Integer = 100
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim tmptop As Integer, tmpleft As Integer
If sender.top + sz = su.Top And sender.left = su.Left _
Or sender.left + sz = su.Left And sender.top = su.Top _
Or sender.top - sz = su.Top And sender.left = su.Left _
Or sender.left - sz = su.Left And sender.top = su.Top Then
tmptop = su.Top
tmpleft = su.Left
su.Top = sender.top
su.Left = sender.left
sender.top = tmptop
sender.left = tmpleft
End If
win()
End Sub
Private Sub coba_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim left As Single = 1
Dim top As Single = 1
Dim i As Integer
For i = 0 To 2
btn(i) = New Button
AddHandler btn(i).Click, AddressOf Btn_Click
Me.Controls.Add(btn(i))
btn(i).Name = "btn" + CStr(i)
btn(i).Size = New Size(sz, sz)
btn(i).Top = top
btn(i).Left = left
btn(i).Tag = i
left += sz
Next
top += sz
left = 1
For i = 3 To 5
btn(i) = New Button
AddHandler btn(i).Click, AddressOf Btn_Click
Me.Controls.Add(btn(i))
btn(i).Name = "btn" + CStr(i)
btn(i).Size = New Size(sz, sz)
btn(i).Top = top
btn(i).Left = left
btn(i).Tag = i
left += sz
Next
top += sz
left = 1
For i = 6 To 7
btn(i) = New Button
AddHandler btn(i).Click, AddressOf Btn_Click
Me.Controls.Add(btn(i))
btn(i).Name = "btn" + CStr(i)
btn(i).Size = New Size(sz, sz)
btn(i).Top = top
btn(i).Left = left
btn(i).Tag = i
left += sz
Next
su.Top = top
su.Left = left
Me.Top = 25
Me.Left = 25
Me.Size = New Size(sz * 3.3, sz * 4)
rand()
End Sub
Private Sub rand()
Dim temp As Integer, i As Integer
Dim track As Integer = 0
Dim flag As Boolean = False
While track < 8
temp = CInt(16 * Rnd())
If temp < 9 And temp <> 0 Then
For i = 0 To track - 1
If temp = btn(i).Text Then
flag = True
Exit For
Else
flag = False
End If
Next
If flag = False Then
btn(track).Text = temp
track += 1
End If
End If
End While
End Sub
Private Sub su_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles su.Click
End Sub
Private Sub win()
Dim ans As Integer
Dim track As Integer = 1
Dim top As Integer, left As Integer, i As Integer
top = 1
left = 1
While track < 9
For i = 0 To 7
If top = btn(i).Top And left = btn(i).Left Then
If track = btn(i).Text Then
track += 1
left += sz
ans = 0
If ((track - 1) Mod 3) = 0 Then
top += sz
left = 1
End If
Exit For
Else
Exit Sub
End If
Else
ans = 1
End If
Next
If ans = 1 Then
Exit Sub
End If
End While
If ans = 0 Then
MsgBox("Anda menang ")
End If
End Sub
End Class
Hasilnya Apabila di Debugging...
0 komentar:
Posting Komentar