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