HTML5技术

[深度学习]实现一个博弈型的AI,从五子棋开始(1) - xerwin(2)

字号+ 作者:H5之家 来源:H5之家 2017-11-15 12:01 我要评论( )

def have_five(self, current_i, current_j): # 四个方向计数 横 竖 左斜 右斜 hcount = 1 vcount = 1 lbhcount = 1 rbhcount = 1 temp = ChessboardState.EMPTY j in range(current_j - 1, -1, -1): # 横向往左 fr

def have_five(self, current_i, current_j): #四个方向计数 横 竖 左斜 右斜 hcount = 1 vcount = 1 lbhcount = 1 rbhcount = 1 temp = ChessboardState.EMPTY j in range(current_j - 1, -1, -1): #横向往左 from (current_j - 1) to 0 temp = self.__chessMap[current_i][j] if temp == ChessboardState.EMPTY or temp != self.__currentState: break hcount = hcount + 1 j in range(current_j + 1, N): #横向往右 from (current_j + 1) to N temp = self.__chessMap[current_i][j] if temp == ChessboardState.EMPTY or temp != self.__currentState: break hcount = hcount + 1 hcount >= 5: return True
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 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 vcount >= 5: return True
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 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 lbhcount >= 5: return True
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 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 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 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

 

 

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • vuejs实现本地数据的筛选分页 - 金振宗

    vuejs实现本地数据的筛选分页 - 金振宗

    2017-11-15 09:02

  • 使用原生JavaScript的Canvas实现拖拽式图形绘制,支持画笔、线条、箭头、三角形、矩形、平行四边形、梯形以及多

    使用原生JavaScript的Canvas实现拖拽式图形绘制,支持画笔、线条、箭

    2017-11-09 16:05

  • 实现输入框【输入填写+动态提示信息+下拉选择】 - 酒不醉心

    实现输入框【输入填写+动态提示信息+下拉选择】 - 酒不醉心

    2017-10-30 18:01

  • 开始Java8之旅(六) -- 使用lambda实现Java的尾递归 - 祈求者-

    开始Java8之旅(六) -- 使用lambda实现Java的尾递归 - 祈求者-

    2017-10-26 10:02

网友点评