大家好,我是3分钟学堂的郭立员,今天继续我们的按键精灵安卓版学习。
前些天下载了一个数独的游戏,简单的难度玩起来还行,稍微难点的解起来就比较费劲了。
于是决定写个脚本来替我完成。
可能有些朋友没玩过数独游戏,我简单说一下它的规则,如上图: 它是一个9*9的格子,分别用1-9数字填入空白格中。
规则:
① 每一行9个数字不重复
② 每一列9个数字不重复
③ 分成九宫,每一宫的9个数字也不重复
在空白格中填写数字使其同时满足上面3个规则,就可以得到一个唯一的数独表格了。
我去写脚本也是围绕着这3个规则去写。
说一下我的思路,拿好小本子记一下。
第一步:把每一个空格用0代替
第二步:从左上角开始,按行去把每一个0用数字1-9替换,替换的数字满足上面说的3个规则。
从第一个0(空格)开始试着填,填写的数字从1开始,如果1不满足规则,填2,以此类推,直到有满足规则的数字,填上临时保存。
第三步:当填到某一空格的时候,发现1到9都不能满足规则,那么说明前面有填错的地方,这时要回溯到上一个空格,然后把之前填的数字加上1再去尝试,如果能继续填下去,就接着填,如果不能了就继续再回溯一个空格,重复这个操作,知道所有的空格都填完。
看一下我做的效果:
代码如下:
Dim arr={_
{0,0,5,0,4,0,0,3,0},_
{8,4,2,0,0,0,0,9,0},_
{0,0,0,9,6,0,0,5,4},_
{0,0,0,6,0,4,8,0,0},_
{6,0,3,0,0,0,7,0,9},_
{0,0,4,3,0,7,0,0,0},_
{4,2,0,0,3,1,0,0,0},_
{0,5,0,0,0,0,3,6,1},_
{0,9,0,0,7,0,4,0,0}_
}
file.Write ("/sdcard/pictures/shudu.txt")
Dim arrshu,shu,k=0
For 200
If k=0 Then
If writeshu(1, 1) Then
shu=File.ReadLine("/sdcard/pictures/shudu.txt",1)
TracePrint shu
TracePrint "清除"&shu
file.DeleteLine "/sdcard/pictures/shudu.txt", 1
TracePrint "在",shu,"基础上+1"
arrshu=split(shu,",")
TracePrint "坐标",arrshu(0),arrshu(1)
TracePrint "数",arrshu(2)+1
If arrshu(2) = 9 Then
TracePrint "已经是9了,无法继续加"
arr[CInt(arrshu(0))][CInt(arrshu(1))] = 0
TracePrint arrshu(0),arrshu(1),"归零"
k=1
Else
TracePrint "开始加1"
For i = arrshu(2) + 1 To 9
If CheckUp(arr, i, CInt(arrshu(0)), CInt(arrshu(1))) Then
TracePrint "添加新的数字",arrshu(0),arrshu(1),i
arr[CInt(arrshu(0))][CInt(arrshu(1))] = i
TracePrint arrshu(0)&","&arrshu(1)&","&i
file.WriteLine "/sdcard/pictures/shudu.txt", 1, arrshu(0) & "," & arrshu(1) & "," & i
Exit For
End If
If i = 9 Then
TracePrint "9个数字都不满足"
arr[CInt(arrshu(0))][CInt(arrshu(1))] = 0
TracePrint arrshu(0),arrshu(1),"归零"
k=1
End If
Next
End If
End If
Else
TracePrint "再次打开"
shu = File.ReadLine("/sdcard/pictures/shudu.txt", 1)
file.DeleteLine "/sdcard/pictures/shudu.txt", 1
TracePrint shu
arrshu = Split(shu, ",")
arr[CInt(arrshu(0))][CInt(arrshu(1))] = 0
If arrshu(2) = 9 Then
TracePrint "已经是9了,无法继续加"
arr[CInt(arrshu(0))][CInt(arrshu(1))] = 0
TracePrint arrshu(0),arrshu(1),"归零"
Else
TracePrint "开始加1"
For i = arrshu(2) + 1 To 9
If CheckUp(arr, i, CInt(arrshu(0)), CInt(arrshu(1))) Then
TracePrint "添加新的数字",arrshu(0),arrshu(1),i
arr[CInt(arrshu(0))][CInt(arrshu(1))] = i
TracePrint arrshu(0)&","&arrshu(1)&","&i
file.WriteLine "/sdcard/pictures/shudu.txt", 1, arrshu(0) & "," & arrshu(1) & "," & i
k=0
Exit For
End If
If i = 9 Then
TracePrint "9个数字都不满足"
TracePrint arrshu(0),arrshu(1),"归零"
arr[CInt(arrshu(0))][CInt(arrshu(1))] =0
End If
Next
End If
End If
Next
For i = 1 To 9
Dim q=""
For j = 1 To 9
q=q&arr[i][j]&" ,"
Next
TracePrint q
Next
Function writeshu(a,b)
For i = 1 To 9
For j = 1 To 9
If arr[i][j] = 0 Then
traceprint "["&i,j&"]", arr[i][j]
For num = 1 To 9
If CheckUp(arr, num, i, j) Then
arr[i][j] = num
traceprint "修改后["&i,j&"]", arr[i][j]
file.WriteLine("/sdcard/pictures/shudu.txt",1,i&","&j&","&num)
Exit For
End If
If num = 9 Then
TracePrint "没有符合要求的"
writeshu=true
Exit Function
End If
Next
End If
If i = 9 and j = 9 Then
For i = 1 To 9
q=""
For j = 1 To 9
q=q&arr[i][j]&" ,"
Next
TracePrint q
Next
EndScript
End If
Next
Next
End Function
Function CheckUp(arr,num,row,col)
dim row3= int((row-1)/3)*3+1
dim col3= int((col-1)/3)*3+1
For i = 1 To 9
If arr[row][i] = num Then
CheckUp=False
Exit Function
End If
If i = 9 Then
TracePrint "第"&row&"行没有数字"&num
For j = 1 To 9
If arr[j][col] = num Then
CheckUp=False
Exit Function
End If
If j = 9 Then
TracePrint "第"&col&"列没有数字"&num
For s = row3 To row3+2
For t = col3 To col3+2
If arr[s][t] = num Then
CheckUp=False
Exit Function
End If
If s = row3 + 2 and t = col3 + 2 Then
CheckUp=true
TracePrint "九宫格内没有重复的"
End If
Next
Next
End If
Next
End If
Next
End Function
这个代码写了一个上午,写的我头都大了,反反复复测试了好多遍,目前中等难度的数独都可以解出来。
因为没有做大量的测试,肯定会有bug,懒得弄了,就这样了。
好了今天的内容就这些,觉得还行,点个赞吧,更多内容查看历史文章。