小王计划使用VB制作一个扫雷小游戏,他首先需要生成一个“雷区”:
①生成n*n的矩阵,其中计划埋雷m个,n和m的数据分别从text1和text2中获取(n<=10,m<=n)
②随机位置埋好地雷后(埋雷位置用X表示且埋雷位置不重复),未埋雷的位置显示该位置周围一圈地雷的数量。
程序代码如下:
Dim flag(0 To 200) As Boolean, a(0 To 200) As String
Dim n As Integer, m As Integer
Private Sub Command1_Click( )
Dim s As String, x As Integer, k As Integer
Dim i As Integer, j As Integer
Randomize
n = Val(Text1.Text): m = Val(Text2.Text)
‘初始化数组flag中各元素均为False,数组a中各元素值为字符"0",代码略
For i = 1 To m
x = Int(Rnd * (n ^ 2)) + 1
If flag(x) = True Then i = i - 1 Else flag(x) = True
Next i
For k = 1 To n * n
If flag(k) = True Then
a(k) = "X"
Else
For i = (k - 1) \ n To (k - 1) \ n + 2
For j = ①
If i = 0 Then Exit For
If flag((i - 1) * n + j) = True And j >= 1 And j <= n Then
a(k) = ②
End If
Next j
Next i
End If
Next k
For i = 1 To n * n ‘在列表框list1中输出矩阵
s = s + " " + a(i)
If ③ Then
List1.AddItem s
s = ""
End If
Next i
End Sub
根据上述代码,完成下列问题: