数字拼图问题(十五数码) VB
发布网友
发布时间:2022-04-21 19:56
我来回答
共2个回答
热心网友
时间:2023-09-27 15:39
欢迎加QQ群30686911一起学VB
我已经做成贴图的魔板了,很考校眼力的,图片打散之后再重新拼出来算完成。双击右边图片框可以更换图片。放在群空间里啦,名字是魔板。欢迎大家来下载测试。
至于这个题目
picture控件组都用正方形的,下标1-16。然后如下排好位置。
(1)(2)(3)(4)
(5)(6)(7)(8)
(9)(10)(11)(12)
(13)(14)(15)(16)
还要加一个开始按钮。
Public A As Integer
Private Sub Command1_Click() '开始按钮,作用:打乱顺序;因为每次都是把正常交换,所以不可能出现无法复位的情况
Randomize
For I = 1 To 10000
Call change(Int(Rnd * 15 + 1))
Next
End Sub
Private Sub Form_Load()
for i=1 to 15
Picture1(i).AutoRedraw = True
picture1(i).print i
next
A = Picture1(2).Left - Picture1(1).Left
For I = 1 To 15
Picture1(I).Appearance = 0
Next
Picture1(16).BackColor = vbYellow '就这一个是凹的,当作空位使用。
End Sub
Private Function beOK() '判断是不是正确完成了排序
For I = 1 To 4
If (Picture1(I).Top = Picture1(I + 4).Top - A) And (Picture1(I).Top = Picture1(I + 8).Top - 2 * A) And (Picture1(I).Top = Picture1(I + 12).Top - 3 * A) Then
sum1 = sum1 + 1
End If
Next
For j = 1 To 13 Step 4
If (Picture1(j).Left = Picture1(j + 1).Left - A And Picture1(j).Left = Picture1(j + 2).Left - 2 * A And Picture1(I).Left = Picture1(I + 3).Left - 3 * A) Then
sum1 = sum1 + 1
End If
Next
If sum1 = 8 Then
MsgBox "顺利完成"
End If
End Function
Private Sub Picture1_Click(index As Integer)
Call change(index)
Call beOK
End Sub
Private Function change(ind As Integer) '判断下标为ind的picture是否与空位挨着,如果是就互换位置
If Picture1(ind).Left = Picture1(16).Left And Abs(Picture1(ind).Top - Picture1(16).Top) = A Or Picture1(ind).Top = Picture1(16).Top And Abs(Picture1(ind).Left - Picture1(16).Left) = A Then
l = Picture1(ind).Left
t = Picture1(ind).Top
Picture1(ind).Left = Picture1(16).Left
Picture1(ind).Top = Picture1(16).Top
Picture1(16).Left = l
Picture1(16).Top = t
End If
End Function
热心网友
时间:2023-09-27 15:40
什么意思?说清楚点