if x < X1-gobang_r or x > X2+gobang_r or y < Y1-gobang_r or y > Y2+gobang_r:
return [x,y],False
a = int((x-X1)/LEN)*LEN + X1
b = int((y-Y1)/LEN)*LEN + Y1
d1 = Distance(x, y, a, b)
if d1<gobang_r:
if gobangboard[int((a-X1)/LEN)][int((b-Y1)/LEN)] == 0:
return [a,b],True
else: return [a,b],False
d2 = Distance(x, y, a, b+LEN)
if d2<gobang_r:
if gobangboard[int((a-X1)/LEN)][int((b+LEN-Y1)/LEN)] == 0:
return [a,b+LEN],True
else: return [a,b+LEN],False
d3 = Distance(x, y, a+LEN, b)
if d3<gobang_r:
if gobangboard[int((a+LEN-X1)/LEN)][int((b-Y1)/LEN)] == 0:
return [a+LEN,b],True
else: return [a+LEN,b],False
d4 = Distance(x, y, a+LEN, b+LEN)
if d4<gobang_r:
if gobangboard[int((a+LEN-X1)/LEN)][int((b+LEN-Y1)/LEN)] == 0:
return [a+LEN,b+LEN],True
else: return [a+LEN,b+LEN],False
return [a,b],False
def getGobang(x, y, dire, dis):
if dire == 1:
x += dis
elif dire == 2:
x += dis
y += dis
elif dire == 3:
y += dis
elif dire == 4:
x -= dis
y += dis
elif dire == 5:
x -= dis
elif dire == 6:
x -= dis
y -= dis
elif dire == 7:
y -= dis
elif dire == 8:
x += dis
y -= dis
if x<0 or y<0 or x>14 or y>14:
return -2
return gobangboard[x][y]
def Evaluate(x, y, player):
value = 0
for i in np.arange(1,9):
if getGobang(x,y,i,1) == player and getGobang(x,y,i,2) == player and getGobang(x,y,i,3) == player and getGobang(x,y,i,4) == player:
value += 2000
continue
if getGobang(x,y,i,-1) == player and getGobang(x,y,i,1) == player and getGobang(x,y,i,2) == player and getGobang(x,y,i,3) == player:
value += 2000
continue
if getGobang(x,y,i,-2) == player and getGobang(x,y,i,-1) == player and getGobang(x,y,i,1) == player and getGobang(x,y,i,2) == player:
value += 2000
continue
if getGobang(x,y,i,1) == player and getGobang(x,y,i,2) == player and getGobang(x,y,i,3) == player:
if getGobang(x,y,i,-1) == 0:
if getGobang(x,y,i,4) == 0:
value += 400
continue
elif getGobang(x,y,i,4) == -player or getGobang(x,y,i,4) == -2:
value += 210
continue
elif (getGobang(x,y,i,-1) == -player or getGobang(x,y,i,-1) == -2) and getGobang(x,y,i,4) == 0:
value += 210
continue
if getGobang(x,y,i,-1) == player and getGobang(x,y,i,1) == player and getGobang(x,y,i,2) == player:
if getGobang(x,y,i,-2) == 0:
if getGobang(x,y,i,3) == 0:
value += 400
continue
elif getGobang(x,y,i,3) == -player or getGobang(x,y,i,3) == -2:
value += 210
continue
elif (getGobang(x,y,i,-2) == -player or getGobang(x,y,i,-2) == -2) and getGobang(x,y,i,3) == 0:
value += 210
continue
if getGobang(x,y,i,1) == player and getGobang(x,y,i,2) == player:
if getGobang(x,y,i,-1) == 0:
if getGobang(x,y,i,3) == 0:
value += 110
continue
elif (getGobang(x,y,i,3) == -player or getGobang(x,y,i,3) == -2) and (getGobang(x,y,i,-2) == 0 or getGobang(x,y,i,-2) == player):
value += 25
continue
elif (getGobang(x,y,i,-1) == -player or getGobang(x,y,i,-1) == -2) and getGobang(x,y,i,3) == 0 and (getGobang(x,y,i,4) == 0 or getGobang(x,y,i,4) == player):
value += 25
continue
if getGobang(x,y,i,-1) == player and getGobang(x,y,i,1) == player:
if getGobang(x,y,i,-2) == 0:
if getGobang(x,y,i,2) == 0:
value += 110
continue
elif (getGobang(x,y,i,2) == -player or getGobang(x,y,i,2) == -2) and (getGobang(x,y,i,-3) == 0 or getGobang(x,y,i,-3) == player):
value += 25
continue
elif (getGobang(x,y,i,-2) == -player or getGobang(x,y,i,-2) == -2) and getGobang(x,y,i,2) == 0 and (getGobang(x,y,i,3) == 0 or getGobang(x,y,i,3) == player):
value += 25
continue
if getGobang(x,y,i,-1) == player:
if getGobang(x,y,i,1) == 0 and (getGobang(x,y,i,-2) == 0 or getGobang(x,y,i,-2) == -player or getGobang(x,y,i,-2) == -2):
value += 5
continue
elif getGobang(x,y,i,1) == -player or getGobang(x,y,i,1) == -2:
value += 1
continue
return value
def getMaxValue(player):
maxvalue=0;
x = []; y = []
for i in range(15):
for j in range(15):
if gobangboard[i][j] == 0:
weight = Evaluate(i,j,player)
if maxvalue < weight:
maxvalue = weight
x.clear(); y.clear()
x.append(i)
y.append(j)
elif maxvalue == weight:
x.append(i)
y.append(j)
t = random.randint(0,len(x)-1)
xx = X1+x[t]*LEN
yy = Y1+y[t]*LEN
return maxvalue,[xx,yy]
def Win(turn):
player = 1 if turn else -1
for i in range(15):
for j in range(11):
if gobangboard[i][j] == player and gobangboard[i][j+1] == player and gobangboard[i][j+2] == player and gobangboard[i][j+3] == player and gobangboard[i][j+4] == player:
return True
for i in range(15):
for j in range(11):
if gobangboard[j][i] == player and gobangboard[j+1][i] == player and gobangboard[j+2][i] == player and gobangboard[j+3][i] == player and gobangboard[j+4][i] == player:
return True
for i in range(11):
for j in range(11):
if gobangboard[i][j] == player and gobangboard[i+1][j+1] == player and gobangboard[i+2][j+2] == player and gobangboard[i+3][j+3] == player and gobangboard[i+4][j+4] == player:
return True
for i in range(11):
for j in np.arange(4,15):
if gobangboard[i][j] == player and gobangboard[i+1][j-1] == player and gobangboard[i+2][j-2] == player and gobangboard[i+3][j-3] == player and gobangboard[i+4][j-4] == player: