设为首页 加入收藏

TOP

[深度学习]实现一个博弈型的AI,从五子棋开始(1)(二)
2017-11-14 09:18:16 】 浏览:927
Tags:深度 学习 实现 一个 博弈 五子棋 开始
currentState
: break hcount = hcount + 1 #H-结果 if hcount >= 5: return True #V-上 for i in range(current_i - 1, -1, -1): # from (current_i - 1) to 0 temp = self.__chessMap[i][current_j] if temp == ChessboardState.EMPTY or temp != self.__currentState: break vcount = vcount + 1 #V-下 for i in range(current_i + 1, N): # from (current_i + 1) to N temp = self.__chessMap[i][current_j] if temp == ChessboardState.EMPTY or temp != self.__currentState: break vcount = vcount + 1 #V-结果 if vcount >= 5: return True #LB-上 for i, j in zip(range(current_i - 1, -1, -1), range(current_j - 1, -1, -1)): temp = self.__chessMap[i][j] if temp == ChessboardState.EMPTY or temp != self.__currentState: break lbhcount = lbhcount + 1 #LB-下 for i, j in zip(range(current_i + 1, N), range(current_j + 1, N)): temp = self.__chessMap[i][j] if temp == ChessboardState.EMPTY or temp != self.__currentState: break lbhcount = lbhcount + 1 #LB-结果 if lbhcount >= 5: return True #RB-上 for i, j in zip(range(current_i - 1, -1, -1), range(current_j + 1, N)): temp = self.__chessMap[i][j] if temp == ChessboardState.EMPTY or temp != self.__currentState: break rbhcount = rbhcount + 1 #RB-下 for i, j in zip(range(current_i + 1, N), range(current_j - 1, -1, -1)): temp = self.__chessMap[i][j] if temp == ChessboardState.EMPTY or temp != self.__currentState: break rbhcount = rbhcount + 1 #LB-结果 if rbhcount >= 5: return True

 

 

这样是不是就写完了,五子棋的逻辑全部实现~ 

NO,别高兴得太早,我想说,我好恶心,上面那个代码,简直丑爆了,再看一眼,重复的写了这么多for,这么多if,这么多重复的代码块,让我先去吐会儿……

好了,想想办法怎么改,至少分了4根轴,是重复的对不对,然后每根轴分别从正负两个方向去统计,最后加起来,两个方向,也是重复的对不对。

于是我们能不能只写一个方向的代码,分别调2次,然后4根轴,分别再调4次,2*4=8,一共8行代码搞定试试。

因为有45°和135°这两根斜轴的存在,所以方向上应该分别从x和y两个轴来控制正负,于是可以这样,先写一个函数,按照方向来统计:

xdirection=0,ydirection=1       表示从y轴正向数;

xdirection=0,ydirection=-1     表示从y轴负向数;

xdirection=1,ydirection=1       表示从45°斜轴正向数;

……

不一一列举了,再加上边界条件的判断,于是有了以下函数:

    def count_on_direction(self, i, j, xdirection, ydirection, color):
        count = 0
        for step in range(1, 5): #除当前位置外,朝对应方向再看4步
            if xdirection != 0 and (j + xdirection * step < 0 or j + xdirection * step >= N):
                break
            if ydirection != 0 and (i + ydirection * step < 0 or i + ydirection * step >= N):
                break
            if self.__chessMap[i + ydirection * step][j + xdirection * step] == color:
                count += 1
            else:
                break
        return count

 

 

于是乎,前面的have_five稍微长的好看了一点,可以变成这样:

def have_five(self, i, j, color):
        #四个方向计数 竖 横 左斜 右斜
        hcount = 1
        vcount = 1
        lbhcount = 1
        rbhcount = 1

        hcount += self.count_on_direction(i, j, -1, 0, color)
        hcount += self.count_on_direction(i, j, 1, 0, color)
        if hcount >= 5:
            return True

        vcount += self.count_on_direction(i, j, 0, -1, color)
        vcount += self.count_on_direction(i, j, 0, 1, color)
        if vcount >= 5:
            return True

        lbhcount += self.count_on_direction(i, j, -1, 1, color)
        lbhcount += self.count_on_direction(i, j, 1, -1, color)
        if lbhcount >= 5:
            return True

        rbhcount += self.count_on_direction(i, j, -1, -1, color)
        rbhcount += self.count_on_direction(i, j, 1, 1, color)
        if rbhcount >= 5:
            return True

 

 

还是一大排重复的代码呀,我还是觉得它丑啊,我真的不是处女座,但是这个函数是真丑啊,能不能让它再帅一点,当然可以,4个重复块再收成一个函数,循环调4次,是不是可以,好,就这么干,于是have_five就又漂亮了一点点:

    def have_five(self, i, j, color):
        #四个方向计数 竖 横 左斜 右斜
        directions = [[(-
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python实现翻译功能 下一篇Python中import机制

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目