# [深度学习]实现一个博弈型的AI，从五子棋开始（1）

……

（1）五子棋下棋逻辑实现

from enum import Enum

N = 15

class ChessboardState(Enum):
EMPTY = 0
BLACK = 1
WHITE = 2

currentI、currentJ、currentState 分别表示当前这步着棋的坐标和颜色，再定义一个get和set函数，最基本的框架就出来了，代码如下：

from enum import Enum
from consts import *

class GoBang(object):
def __init__(self):
self.__chessMap = [[ChessboardState.EMPTY for j in range(N)] for i in range(N)]
self.__currentI = -1
self.__currentJ = -1
self.__currentState = ChessboardState.EMPTY

def get_chessMap(self):
return self.__chessMap

def get_chessboard_state(self, i, j):
return self.__chessMap[i][j]

def set_chessboard_state(self, i, j, state):
self.__chessMap[i][j] = state
self.__currentI = i
self.__currentJ = j
self.__currentState = state

    def have_five(self, current_i, current_j):
#四个方向计数 竖 横 左斜 右斜
hcount = 1

temp = ChessboardState.EMPTY

#H-左
for 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        #H-右
for 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        #H-结果
if hcount >= 5:
return True

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

temp = ChessboardState.EMPTY

#H-左
for 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
#H-右
for 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
#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，这么多重复的代码块，让我先去吐会儿……

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

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

    def have_five(self, i, j, color):
#四个方向计数 横 竖 左斜 右斜
directions = [[(-1, 0), (1, 0)], \
[(0, -1), (0, 1)], \
[(-1, 1), (1, -1)], \
[(-1, -1), (1, 1)]]

for axis in directions:
axis_count = 1
for (xdirection, ydirection) in axis:
axis_count += self.count_on_direction(i, j, xdirection, ydirection, color)
if axis_count >= 5:
return True

return False

    def get_chess_result(self):
if self.have_five(self.__currentI, self.__currentJ, self.__currentState):
return self.__currentState
else:
return ChessboardState.EMPTY

#coding:utf-8

from enum import Enum
from consts import *

class GoBang(object):
def __init__(self):
self.__chessMap = [[ChessboardState.EMPTY for j in range(N)] for i in range(N)]
self.__currentI = -1
self.__currentJ = -1
self.__currentState = ChessboardState.EMPTY

def get_chessMap(self):
return self.__chessMap

def get_chessboard_state(self, i, j):
return self.__chessMap[i][j]

def set_chessboard_state(self, i, j, state):
self.__chessMap[i][j] = state
self.__currentI = i
self.__currentJ = j
self.__currentState = state

def get_chess_result(self):
if self.have_five(self.__currentI, self.__currentJ, self.__currentState):
return self.__currentState
else:
return ChessboardState.EMPTY

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

def have_five(self, i, j, color):
#四个方向计数 横 竖 左斜 右斜
directions = [[(-1, 0), (1, 0)], \
[(0, -1), (0, 1)], \
[(-1, 1), (1, -1)], \
[(-1, -1), (1, 1)]]

for axis in directions:
axis_count = 1
for (xdirection, ydirection) in axis:
axis_count += self.count_on_direction(i, j, xdirection, ydirection, color)
if axis_count >= 5:
return True

return False

UI部分在这里：

[深度学习]实现一个博弈型的AI，从五子棋开始（2）

posted on 2017-11-13 23:41  xerwin  阅读(12397)  评论(15编辑  收藏  举报