一笔画小游戏,好玩的很

就是到了后面比较麻烦,手动找路径太慢了,作为程序员,这又是一个锻炼的好机会是不是!于是乎,了解了一下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],
          ]


class solutiondfs():
    def __init__(self,arr):
        self.arr=arr
        self.rows=len(arr)
        self.cols=len(arr[0])
        self.nowPositionRow=None
        self.nowPositionCol=None
        self.steps=[]

    #寻找开始的点
    def startPoint(self):
        for i in range(len(self.arr)):
            for j in range(len(self.arr[i])):
                if self.arr[i][j]==1:
                    return i,j
    #判断是否结束
    def isFinished(self):
        for i in pointArr:
            for j in i:
                if j ==0:
                    return False
        return True

    #获取下一步的位置
    def getNextEle(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<self.cols-1 and self.arr[now_row][now_col+1]==0:

            nextArr.append( [now_row,now_col+1])
        if now_row<self.rows-1  and self.arr[now_row+1][now_col]==0:

            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])
        return nextArr

    #递归,深度优先
    def step_to_next(self):
        if self.isFinished():
            return True
        next_steps=self.getNextEle(self.steps[-1][0],self.steps[-1][1])
        for i in next_steps:
            self.arr[i[0]][i[1]]=1
            self.steps.append(i)
            if self.step_to_next():
                return True
            else:
                self.steps.pop()
                self.arr[i[0]][i[1]]=0
        return False

    def start_bfs(self):
        self.nowPositionRow,self.nowPositionCol=self.startPoint()
        self.steps.append([self.nowPositionRow,self.nowPositionCol])
        if self.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上,这就需要加一些图像识别和手势转化,这里就不细说了,效果如下,总体来说不慢,但是有的关卡因为初始路径没选对,会计算很久,还是需要优化。