一笔画小游戏,好玩的很
就是到了后面比较麻烦,手动找路径太慢了,作为程序员,这又是一个锻炼的好机会是不是!于是乎,了解了一下dfs和bfs算法(都是路径搜索算法),然后就开撸:
#pointArr=[[1,0,0,3],#[0,2,2,0],#[0,1,0,0],#[0,0,0,3]]
pointArr=[[0,0,0,0,0,0],
[-1,0,0,0,0,1],
[0,0,0,0,0,0],
[0,0,0,0,0,0],
[-1,-1,0,0,-1,0],
[0,0,0,0,-1,0],
[0,0,0,0,0,0],
]classsolutiondfs():def __init__(self,arr):
self.arr=arr
self.rows=len(arr)
self.cols=len(arr[0])
self.nowPositionRow=None
self.nowPositionCol=None
self.steps=[]#寻找开始的点
defstartPoint(self):for i inrange(len(self.arr)):for j inrange(len(self.arr[i])):if self.arr[i][j]==1:returni,j#判断是否结束
defisFinished(self):for i inpointArr:for j ini:if j ==0:returnFalsereturnTrue#获取下一步的位置
defgetNextEle(self,now_row,now_col):#顺序是上,右,下,左边
nextArr=[]if now_row>=1 and self.arr[now_row-1][now_col]==0:
nextArr.append([now_row-1,now_col])if now_col
nextArr.append( [now_row,now_col+1])if now_row
nextArr.append( [now_row+1,now_col])if now_col>=1 and self.arr[now_row][now_col-1]==0:
nextArr.append( [now_row,now_col-1])returnnextArr#递归,广度优先
defstep_to_next(self):ifself.isFinished():returnTrue
next_steps=self.getNextEle(self.steps[-1][0],self.steps[-1][1])for i innext_steps:
self.arr[i[0]][i[1]]=1self.steps.append(i)ifself.step_to_next():returnTrueelse:
self.steps.pop()
self.arr[i[0]][i[1]]=0returnFalsedefstart_bfs(self):
self.nowPositionRow,self.nowPositionCol=self.startPoint()
self.steps.append([self.nowPositionRow,self.nowPositionCol])ifself.step_to_next():print(self.steps)else:print('hehe')
s=solutiondfs(pointArr)
s.start_bfs()
嘛,计算出来的路径打印出来就是
[[1, 5], [0, 5], [0, 4], [1, 4], [1, 3], [0, 3], [0, 2], [1, 2], [2, 2], [2, 3], [3, 3], [3, 4], [2, 4], [2, 5], [3, 5], [4, 5], [5, 5], [6, 5], [6, 4], [6, 3], [6, 2], [6, 1], [6, 0], [5, 0], [5, 1], [5, 2], [5, 3], [4, 3], [4, 2], [3, 2], [3, 1], [3, 0], [2, 0], [2, 1], [1, 1], [0, 1], [0, 0]]
速度还不赖,嘿嘿。
然后博主又改了一下,运用在autojs上,这就需要加一些图像识别和手势转化,这里就不细说了,效果如下,总体来说不慢,但是有的关卡因为初始路径没选对,会计算很久,还是需要优化。