Gomoku 对战系统

本贴最后更新于 468 天前,其中的信息可能已经水流花落

Tips

本文章是作者自己的小学期项目,是和学校的服务器进行交互下五子棋的一个项目,为进行记录本人的开发历程所以记录到博客中,可能对大部分朋友来说没什么阅读价值……

综述

从接触五子棋项目到成功部署并不断优化下棋算法之间仅仅经过了不到两周的时间,短暂仓促的时间让项目增加了一些挑战性。在完成了与服务器进行交互,并利用权值法算法进行下棋后,我发现这个简单的算法存在诸多的缺点,无法在人机亦或机器对弈中取得优势。于是通过网络大量查阅并学习的一些前辈们已经实践的算法对本项目的算法部分进行不断优化,以期实现较好的对弈效果。

目前,在实现五子棋机器人时,有如下常用的算法:

1.极小化极大算法(Minimax): Minimax 是一种基于博弈树搜索的算法,用于在对抗性游戏中找到最优的下棋策略。它通过递归地遍历游戏树,并对每个可能的走法进行评估,最终选择对自己最有利的走法。

2.Alpha-beta 剪枝: Alpha-beta 剪枝是对 Minimax 算法的一种优化,通过排除一些明显不好的走法,减少搜索的深度,从而提高搜索效率。它利用了极小化极大搜索过程中的剪枝思想,避免对无关的子树进行评估。

3.启发式搜索: 启发式搜索使用一些特定的规则和策略,以引导搜索过程并加速决策的过程。这可能包括评估函数、棋型识别、棋局评估等。通过合理设计和调整启发式规则,可以提高机器人的下棋水平。

4.蒙特卡洛树搜索(Monte Carlo Tree Search,MCTS): MCTS 是一种基于随机模拟的搜索算法,适用于状态空间较大的游戏。它通过模拟大量的随机对局来评估每个可能的走法,并根据模拟结果进行选择。MCTS 在近年来在围棋领域取得了很大的成功,也适用于五子棋。

5.机器学习: 通过使用一些机器学习算法进行训练学习,使其能够通过学习从棋局状态到落子策略的映射关系来改进下棋的决策。

因为时间仓促,我所做的仅是摸着前人的路进行实践和思考可能对于一些复杂知识仅仅停留在应用层面,还有待深入了解。

其实对战平台的本质就是将自己的指令比如下一步走棋方法通过 http 请求发送到服务器进行对战。

第一步-第四步

使用 http_get 函数发送请求

import urllib.request
f = urllib.request.urlopen("http://183.175.12.27:8003/step_01")
print(f.read())
b'{"is_success": true, "message": "Please visit http://183.175.12.27:8000/context/86e0caa3527526e88c3300ff4c2c3d6e"}'

通过 GET 访问第一步网址,得到第二步网址

import requests as re
prama = {
'name' : 'Ren Min Shuai',
'student_number' : '0211122692'

}
url = 'http://183.175.12.27:8003/step_02'
getHml = re.get(url , params = prama)

print(getHml.text)

{"is_success": true, "message": "Hi Ren Min Shuai, please visit http://183.175.12.27:8000/context/da4ce64e326bf89a62a301cd3ea8eda8"}

通过 GET 访问第二步网址并添加姓名学号,返回得到第三步网址

得到第四步网址,并进行访问

'http://183.175.12.27:8003/context/f64710b4861e6ff10678036548bf7afa'

import urllib.request
import json
import requests
url = "http://183.175.12.27:8003/step_04"
response = requests.request("GET", url)
data = response.json()
print(type(data))
questions = eval(data['questions'])

print(questions)
type(questions)
<class 'dict'>
[[710707501, 882063017, 516479913268], [648336897, 950313054, 811882559], [204496984, 732218009, 9384], [7049, 255661325, 1590], [404296531041, 134854127, 658062671], [987600747, 895535348635, 203522106], [765752460, 162204568531, 834124084767], [368155026987, 7, 179436820825], [386261505, 826760750, 346872412], [121343804, 397364833, 689455625]]
list
def modular_exponentiation(base, exponential, mod):
    base = base % mod
    res = 1
    while exponential:
        if exponential & 1 == 1: #判断最后一位
            res = (res * base) % mod #为1则乘幂
        base = (base * base) % mod #累乘记录幂
        exponential >>= 1  #右移一位
    return res
# (a ^ b) % c = (a%c) ^ (b %c)
ans=''
type(questions)
for i in range(10):
 a = questions[i][0]
 b = questions[i][1]
 c = questions[i][2]
 print("快速模幂:%d"  % (modular_exponentiation(a, b, c)))
 if(i==0):
  ans=ans+(str(modular_exponentiation(a, b, c)))
 else:
   ans=ans+','+(str(modular_exponentiation(a, b, c)))
    
print(ans)



快速模幂:386827997505
快速模幂:689606426
快速模幂:7168
快速模幂:689
快速模幂:477699308
快速模幂:188308647
快速模幂:692498311389
快速模幂:4372632308
快速模幂:231523513
快速模幂:74605614
386827997505,689606426,7168,689,477699308,188308647,692498311389,4372632308,231523513,74605614
import requests as re
url1 = 'http://183.175.12.27:8003/step_04'
step4url=url1+'?ans='+ans
print(step4url)
getHml = re.get(step4url)

print(getHml.text)
http://183.175.12.27:8003/step_04?ans=386827997505,689606426,7168,689,477699308,188308647,692498311389,4372632308,231523513,74605614
{"is_success": true, "message": "please visit http://183.175.12.27:8000/context/eb63fffd85c01a0a5d8f3cadea18cf56"}

通过提交对获取到的十个问题进行加密的信息得到第五步地址

第五步

import urllib.request
import requests as re
import json
def modular_exponentiation(base, exponential, mod):
    base = base % mod
    res = 1
    while exponential:
        if exponential & 1 == 1: #判断最后一位
            res = (res * base) % mod #为1则乘幂
        base = (base * base) % mod #累乘记录幂
        exponential >>= 1  #右移一位
    return res
a=31415926
b=65537
c=135261828916791946705313569652794581721330948863485438876915508683244111694485850733278569559191167660149469895899348939039437830613284874764820878002628686548956779897196112828969255650312573935871059275664474562666268163936821302832645284397530568872432109324825205567091066297960733513602409443790146687029
password=str(modular_exponentiation(a, b, c))
print(password)
import requests as re
url1 = 'http://183.175.12.27:8003/step_05'
step5url=url1+'?num='+password
print(step5url)
getHml = re.get(step5url)#第五步_验证1

print(getHml.text)
93881021929864980809174342694135963369278672668363116591635346676508616959803466385395110947590572285871635015799641415119061735125830808855763471006378541296998154431918842025919210489545546222811516276906010085975565730381040208928087282527065856602316379316169602170574703110733688951416363596477076057604
http://183.175.12.27:8003/step_05?num=93881021929864980809174342694135963369278672668363116591635346676508616959803466385395110947590572285871635015799641415119061735125830808855763471006378541296998154431918842025919210489545546222811516276906010085975565730381040208928087282527065856602316379316169602170574703110733688951416363596477076057604
{"is_success": true}
str2='hello, world!'
count=len(str2)-1
print("len(str2):%d" %count)
str2num=0
for i in str2:
    str2num+=ord(i)*(256**count)
    count-=1
print(str2num)
url1 = 'http://183.175.12.27:8003/step_05'
step5url2=url1+'?str2num='+str(str2num)
print(step5url2)
getHml = re.get(step5url2)#第五步_验证2

print(getHml.text)
len(str2):12
8271117963530313756381553648673
http://183.175.12.27:8003/step_05?str2num=8271117963530313756381553648673
{"is_success": true}
a=str2num
b=65537
c=135261828916791946705313569652794581721330948863485438876915508683244111694485850733278569559191167660149469895899348939039437830613284874764820878002628686548956779897196112828969255650312573935871059275664474562666268163936821302832645284397530568872432109324825205567091066297960733513602409443790146687029
password=str(modular_exponentiation(a, b, c))
print(password)
url1 = 'http://183.175.12.27:8003/step_05'
step5url3=url1+'?str='+password
print(step5url3)
getHml = re.get(step5url3)#第五步_验证3

print(getHml.text)
41717036756922086167767786786712423269418006844571540642506559265291791915434146228186913317848309015094527019477424371059092802944619669306914382602706827360526972065492061812094685980946805703427198780190727908992795654922187272049190698839062058797703366041101170679313306423879973490422740979302379859262
http://183.175.12.27:8003/step_05?str=41717036756922086167767786786712423269418006844571540642506559265291791915434146228186913317848309015094527019477424371059092802944619669306914382602706827360526972065492061812094685980946805703427198780190727908992795654922187272049190698839062058797703366041101170679313306423879973490422740979302379859262
{"is_success": true}
password2=hex(modular_exponentiation(a, b, c))
step5url4=url1+'?hex='+password2
print(step5url4)
getHml = re.get(step5url4)#第五步_验证4
print(getHml.text)
http://183.175.12.27:8003/step_05?hex=0x3b68334487f782c227eefff8510c6d7b8568e50580e1d585360df7b381c15bc663ab6e867a475b52921ef254cfcbe328e074d5761e605b6aa8ab608fc9914a58d1120294986dcff70b46e92fa7d48edf4dbb104fa272eb459e8896e29d5d0473f75bb866d9f73ec1c134d827156ee355d19bdd94b8d73fe2526ada033a5cad3e
{"is_success": true}
import requests as re
my_user=input("请输入用户名:")
my_password=input("请输入密码:")
count=len(my_password)-1
strpw=0
for i in my_password:
    strpw+=ord(i)*(256**count)
    count-=1
print("strpw:%s"%strpw)
a=strpw
b=65537
c=135261828916791946705313569652794581721330948863485438876915508683244111694485850733278569559191167660149469895899348939039437830613284874764820878002628686548956779897196112828969255650312573935871059275664474562666268163936821302832645284397530568872432109324825205567091066297960733513602409443790146687029
my_new_password=modular_exponentiation(a, b, c)
my_new_password=hex(my_new_password)
prama = {
	'user' : my_user,
	'password' :my_new_password
}
print(prama)
url = 'http://183.175.12.27:8003/step_05'
getHml = re.get(url , params = prama)
print(getHml.text)
print(type(getHml.text))
请输入用户名:0123456
请输入密码:123456
strpw:251285381232892045589114
{'user': '', 'password': ''}
{"is_success": true, "message": "a26d95e8c2e1ac82cf1d54b4ecf3e790378c7910a1fb243beb87ef485f8543214d5bc47ccf46474167f452c62d34f392fc838f52f1d0d85d997661c91ee249851397a5ad5a0675bb18934c4e29043ebdf27d67721d20b2e73367452b8b9167fadcd96ac240c5f7ac35767082bbcd92c24e65eebdb6fefa9f7a8e9fc329201aa7"}
<class 'str'>
step5rtmg=getHml.text
#print(step5rtmg)
message=step5rtmg.split('"')[5]
dec_message= int(message, 16)
#print(dec_message)
a=dec_message
b=65537
c=135261828916791946705313569652794581721330948863485438876915508683244111694485850733278569559191167660149469895899348939039437830613284874764820878002628686548956779897196112828969255650312573935871059275664474562666268163936821302832645284397530568872432109324825205567091066297960733513602409443790146687029
open_message=modular_exponentiation(a, b, c)
print('open_message:%d\n'%open_message)

a =open_message
str1 = []
while a:
    str1.append(chr(a % 256))  
    a = a // 256   #用/会报错因为结果是浮点数,所以用//
str1.reverse()
for i in str1:
    print(i, end="")
open_message:8100631147174048917906894111262287789688173318131212646276114930677589325580516820434214737951816402171467869732890777591764250280070173899304623954729235156437186840158763438020700523443468736868884389858742151225448228292003599948065148169196238964

Hi 0211122692, please visit http://183.175.12.27:8000/context/3ff280105813f582c7c38dabedd901bcfill text

进行登录得到返回 message,公钥解密得到第六步的地址

第六步

import urllib.request
import requests as re
import json
url = 'http://183.175.12.27:8003/step_06'
getHml = re.get(url)
print(getHml.text)
print(type(getHml.text))
step6rtmg=getHml.text
#print(step5rtmg)
st6questions=step6rtmg.split('"')[5]
print(st6questions)

{"is_success": true, "questions": "ggffhggfhffgiefhfeheidehdidhghegcidfgiefcfeeeied"}
<class 'str'>
ggffhggfhffgiefhfeheidehdidhghegcidfgiefcfeeeied
def print_map(map):   #打印棋盘函数且生成棋盘序列字符串
    map_ans=''
    for i in range(0,15):
        print()
        for j in range(0,15):
                print(map[i][j],end="")
                map_ans+=map[i][j]
    return map_ans
st6_que=list(st6questions)
for i in range(len(st6_que)):
    st6_que[i]=ord(st6_que[i])-97
print("落子序列坐标表示:", st6_que)
map=[['.' for _ in range(15)] for i in range (15)]
fg=0
st6_ans=''
for i in range(0,len(st6_que),2):
    if fg==0:
        map[st6_que[i]][st6_que[i+1]]='x'
        fg=1
    else:
        map[st6_que[i]][st6_que[i+1]]='o'
        fg=0
    if(i!=len(st6_que)-2):
        st6_ans+=print_map(map)+','
    else:
        st6_ans+=print_map(map)
    print()
    


落子序列坐标表示: [6, 6, 5, 5, 7, 6, 6, 5, 7, 5, 5, 6, 8, 4, 5, 7, 5, 4, 7, 4, 8, 3, 4, 7, 3, 8, 3, 7, 6, 7, 4, 6, 2, 8, 3, 5, 6, 8, 4, 5, 2, 5, 4, 4, 4, 8, 4, 3]

...............
...............
...............
...............
...............
...............
......x........
...............
...............
...............
...............
...............
...............
...............
...............

...............
...............
...............
...............
...............
.....o.........
......x........
...............
...............
...............
...............
...............
...............
...............
...............

...............
...............
...............
...............
...............
.....o.........
......x........
......x........
...............
...............
...............
...............
...............
...............
...............

...............
...............
...............
...............
...............
.....o.........
.....ox........
......x........
...............
...............
...............
...............
...............
...............
...............

...............
...............
...............
...............
...............
.....o.........
.....ox........
.....xx........
...............
...............
...............
...............
...............
...............
...............

...............
...............
...............
...............
...............
.....oo........
.....ox........
.....xx........
...............
...............
...............
...............
...............
...............
...............

...............
...............
...............
...............
...............
.....oo........
.....ox........
.....xx........
....x..........
...............
...............
...............
...............
...............
...............

...............
...............
...............
...............
...............
.....ooo.......
.....ox........
.....xx........
....x..........
...............
...............
...............
...............
...............
...............

...............
...............
...............
...............
...............
....xooo.......
.....ox........
.....xx........
....x..........
...............
...............
...............
...............
...............
...............

...............
...............
...............
...............
...............
....xooo.......
.....ox........
....oxx........
....x..........
...............
...............
...............
...............
...............
...............

...............
...............
...............
...............
...............
....xooo.......
.....ox........
....oxx........
...xx..........
...............
...............
...............
...............
...............
...............

...............
...............
...............
...............
.......o.......
....xooo.......
.....ox........
....oxx........
...xx..........
...............
...............
...............
...............
...............
...............

...............
...............
...............
........x......
.......o.......
....xooo.......
.....ox........
....oxx........
...xx..........
...............
...............
...............
...............
...............
...............

...............
...............
...............
.......ox......
.......o.......
....xooo.......
.....ox........
....oxx........
...xx..........
...............
...............
...............
...............
...............
...............

...............
...............
...............
.......ox......
.......o.......
....xooo.......
.....oxx.......
....oxx........
...xx..........
...............
...............
...............
...............
...............
...............

...............
...............
...............
.......ox......
......oo.......
....xooo.......
.....oxx.......
....oxx........
...xx..........
...............
...............
...............
...............
...............
...............

...............
...............
........x......
.......ox......
......oo.......
....xooo.......
.....oxx.......
....oxx........
...xx..........
...............
...............
...............
...............
...............
...............

...............
...............
........x......
.....o.ox......
......oo.......
....xooo.......
.....oxx.......
....oxx........
...xx..........
...............
...............
...............
...............
...............
...............

...............
...............
........x......
.....o.ox......
......oo.......
....xooo.......
.....oxxx......
....oxx........
...xx..........
...............
...............
...............
...............
...............
...............

...............
...............
........x......
.....o.ox......
.....ooo.......
....xooo.......
.....oxxx......
....oxx........
...xx..........
...............
...............
...............
...............
...............
...............

...............
...............
.....x..x......
.....o.ox......
.....ooo.......
....xooo.......
.....oxxx......
....oxx........
...xx..........
...............
...............
...............
...............
...............
...............

...............
...............
.....x..x......
.....o.ox......
....oooo.......
....xooo.......
.....oxxx......
....oxx........
...xx..........
...............
...............
...............
...............
...............
...............

...............
...............
.....x..x......
.....o.ox......
....oooox......
....xooo.......
.....oxxx......
....oxx........
...xx..........
...............
...............
...............
...............
...............
...............

...............
...............
.....x..x......
.....o.ox......
...ooooox......
....xooo.......
.....oxxx......
....oxx........
...xx..........
...............
...............
...............
...............
...............
...............
#print(st6_ans) #落子顺序序列
url6 = 'http://183.175.12.27:8003/step_06'#棋盘图形顺序写入字段ans,结果提交
step6url=url6+'?ans='+st6_ans
getHml = re.get(step6url)

print(getHml.text)
{"is_success": true, "message": "please visit http://183.175.12.27:8000/context/c03152f5db8918b9905d449686685f77"}

通过提交棋盘序列获得第七步网址

第七步

import urllib.request
import requests as re
import json
url = 'http://183.175.12.27:8003/step_07'
response = re.request("GET", url)
data = response.json()
print(data['board'])
print(data['coord'])
check=data['coord']
ghhggggfgigjhhfhiijjfghiihigjhkhhjgkjgffhfkgjijfiejdjeheidkfkikekdlfmgmfifnf
['nk', 'ok', 'bn', 'ef', 'lk', 'im', 'gb', 'fd', 'ha']
def print_map(map):   #打印棋盘函数且生成棋盘序列字符串
    map_ans=''
    for i in range(0,15):
        print()
        for j in range(0,15):
                print(map[i][j],end="")
                map_ans+=map[i][j]
st7_que=list(data['board'])
for i in range(len(st7_que)):
    st7_que[i]=ord(st7_que[i])-97
print("落子序列坐标表示:", st7_que)
map=[['.' for _ in range(15)] for i in range (15)]
fg=0
st7_ans=''
for i in range(0,len(st7_que),2):
    if fg==0:
        map[st7_que[i]][st7_que[i+1]]='x'
        fg=1
    else:
        map[st7_que[i]][st7_que[i+1]]='o'
        fg=0
print_map(map)
落子序列坐标表示: [6, 7, 7, 6, 6, 6, 6, 5, 6, 8, 6, 9, 7, 7, 5, 7, 8, 8, 9, 9, 5, 6, 7, 8, 8, 7, 8, 6, 9, 7, 10, 7, 7, 9, 6, 10, 9, 6, 5, 5, 7, 5, 10, 6, 9, 8, 9, 5, 8, 4, 9, 3, 9, 4, 7, 4, 8, 3, 10, 5, 10, 8, 10, 4, 10, 3, 11, 5, 12, 6, 12, 5, 8, 5, 13, 5]

...............
...............
...............
...............
...............
.....oxo.......
.....oxxxoo....
....oxoxox.....
...xxxoxx......
...oxoxxxo.....
...xoooox......
.....o.........
.....ox........
.....o.........
...............
def transverse_judge(i):  # 横向判断
    ans=''
    a = ord(i[0]) - 97
    b = ord(i[1]) - 97
    for j in range(-4, 5):
        if 0 <= b + j <= 14:
            ans+=map[a][b + j]
        else:
            continue
    ans+=","
    return ans

def portrait_judge(i):  # 纵向判断
    ans=''
    a = ord(i[0]) - 97
    b = ord(i[1]) - 97
    for j in range(-4, 5):
        if 0 <= a + j <= 14:
            ans+=map[a + j][b]
        else:
            continue
    ans+=","
    return ans

def left_leaning_judge(i):  # 左倾判断(\)
    ans=''
    a = ord(i[0]) - 97
    b = ord(i[1]) - 97
    for j in range(-4, 5):
        if 0 <= a + j <= 14 and 0 <= b + j <= 14:
            ans+=map[a + j][b + j]
        else:
            continue
    ans+=","
    return ans

def right_deviation_judge(i):  # 右倾判断(/)
    ans=''
    a = ord(i[0]) - 97
    b = ord(i[1]) - 97
    for j in range(-4, 5):
        if 0 <= a + j <= 14 and 0 <= b - j <= 14:
            ans+=map[a + j][b - j]
        else:
            continue
    ans+=","
    return ans

st7ans=''
for k in check:
    st7ans+=transverse_judge(k)
    st7ans+=left_leaning_judge(k)
    st7ans+=portrait_judge(k)
    st7ans+=right_deviation_judge(k)
print(st7ans[0:-1])

url6 = 'http://183.175.12.27:8003/step_07'#棋盘图形顺序写入字段ans,结果提交
step6url=url6+'?ans='+st7ans
getHml = re.get(step6url[0:-1])

print(getHml.text)
.........,xo....,......,......,.........,o....,.....,.....,......,...,......,......,.........,.....xxo.,.....ooxx,.........,.........,oxx.....,........,........,x......,..o....,.........,.......,.....o,...xxo,.........,......,.....oxo,.....xox,.......xo,........,....o,...x.,.........,.....
{"is_success": true, "message": "please visit http://183.175.12.27:8000/context/701672826a45d8d2d998b3a3f66166bf"}

提交序列后得到第八步地址

第八步

实现按照评分规则选择落子位置的算法

import urllib.request
import requests as re
import json
url = 'http://183.175.12.27:8003/step_08'
response = re.request("GET", url)
board_list = response.json()['questions']
print(board_list)
['hhifhjigkhjghghiihjhkjjijjij', 'hhigjhihjgiiifhgjijfkhkeli', 'hhjhiijjkijijkjgjfkhijiflihegdlh', 'hhkhlhihkgjimijfnjoklgjhjgigmgnglfli', 'hhjhihjiijjjjgjkjliihigjkgigkflehf', 'edfefdgdeeefhcdffccfffdddegbgcecicjchb', 'gheifhiihhihhiijighg', 'hhjhiijgghjjjkjijfkjig', 'ggffhggfhffgiefhfeheidehdidhgheg', 'hhjhiijgghjjjkjijfkjiggiijih', 'hhjhiijgghjjjkjijfkjiggi', 'ghhihhhggiggihfhigfjiiifjh', 'hhggfhghifgigfgjgkhfhkhiijfgjikh', 'hhihgihijgiiigjffjekgghggjghijhjgkiffijekdkfgl', 'hhjhihjiijjjjgjkjlii', 'hhjhihjiijjjjgjkjliihigjkgigkflehfhgifjf', 'ghhihhhggiggihfhigfjiiifjhkhjgjfhfgekfijkg']
win_type = {"CMMMM": 10000, "MCMMM": 10000, "MMCMM": 10000, "MMMCM": 10000, "MMMMC": 10000} #1. 致胜棋型
defense_type = {"OOOOC": 6000, "COOOO":6000} #2. 必须防守棋型
latent_win_type = {".CMMM.": 5000, ".MCMM.": 5000, ".MMCM.": 5000, ".MMMC.": 5000} #3. 两步致胜棋型
latent_defense_type = {"COOO.": 2500, ".OOOC": 2500, ".OOCO.": 2500, ".OCOO.": 2500} #4. 两步防守棋型
kill_type = {"OCMMM.": 2000, "OMCMM.": 2000, "OMMCM.": 2000, "OMMMC.": 2000, ".CMMMO": 2000, ".MCMMO": 2000, ".MMCMO": 2000, ".MMMCO": 2000} #5. 进攻棋型
latent_kill_type = {".MMC.": 400, ".MCM.": 400, ".CMM.": 400} #6. 两步进攻棋型
foresee_type = {".OOC": 400, "COO.": 400, "MOOOC": 400, "COOOM": 400} #7. 预防棋型
invalid_type = {".MMCO": 200, ".MCMO": 200, ".CMMO": 200, "OMMC.": 200, "OMCM.": 200, "OCMM.": 200, "MOOC": 200, "COOM": 200} #8. 无效进攻防守棋型
layout_type = {".MC.": 50, ".CM.": 50} #9. 布局棋型
com = [win_type, defense_type, latent_win_type, latent_defense_type, kill_type, latent_kill_type, foresee_type, invalid_type, layout_type]

def calculate_score(str1):    #计算分数
    sum1 = 0
    for i in com:
        for item, value in i.items():
            if item in str1:
                sum1 = sum1 + value
    return int(sum1)
def judge(a,b,map):  # 判断
    ans=''
    sum=0
    for j in range(-4, 5):
        if 0 <= b + j <= 14:
            ans+=map[a][b + j]
        else:
            continue
    sum+=calculate_score(ans)
    ans=''
    for j in range(-4, 5):
        if 0 <= a + j <= 14:
            ans+=map[a + j][b]
        else:
            continue
    sum+=calculate_score(ans)
    ans=''
    for j in range(-4, 5):
        if 0 <= a + j <= 14 and 0 <= b + j <= 14:
            ans+=map[a + j][b + j]
        else:
            continue
    sum+=calculate_score(ans)
    ans=''
    for j in range(-4, 5):
        if 0 <= a + j <= 14 and 0 <= b - j <= 14:
            ans+=map[a + j][b - j]
        else:
            continue
    sum+=calculate_score(ans)
    return sum
def ini_board(que):#落子时棋盘判断函数
    for i in range(len(que)):
        que[i]=ord(que[i])-97
    print("落子序列坐标表示:", que)
    map=[['.' for _ in range(15)] for i in range (15)]
    if len(que)/2%2==0:
        print("轮到黑棋落子")
        fg=0
    else:
        print("轮到白棋落子")
        fg=1
    ans=''
    
    for i in range(0,len(que),2):
        if fg==0:
            map[que[i]][que[i+1]]='M'
            fg=1
        else:
            map[que[i]][que[i+1]]='O'
            fg=0
    return map

def print_map(map):   #打印棋盘函数
    for i in range(0,15):
        print()
        for j in range(0,15):
                print(map[i][j],end="")


def find_max(board):    #寻找当前棋盘落子最大得分
    sum_max=0
    for num1 in range(15):
        for num2 in range(15):
            max1 = 0
            if board[num1][num2] == ".":
                board[num1][num2] = "C"
                max1 = max1 +judge(num1,num2,board)
                board[num1][num2] = "."
            if sum_max <= max1:
                sum_max = max1
                site1 = num1
                site2 = num2
    print("最佳落子位置:",end="")
    print(chr(site1 + 97), end="")
    print(chr(site2 + 97))
    new_site=chr(site1 + 97)+chr(site2 + 97)
    return new_site

st8_ans=''
for i in board_list:
    temp_board=ini_board(list(i))
    #print_map(temp_board)
    st8_ans+=","+find_max(temp_board)
    print()
print(st8_ans[1:])
    
    
落子序列坐标表示: [7, 7, 8, 5, 7, 9, 8, 6, 10, 7, 9, 6, 7, 6, 7, 8, 8, 7, 9, 7, 10, 9, 9, 8, 9, 9, 8, 9]
轮到黑棋落子
最佳落子位置:ki

落子序列坐标表示: [7, 7, 8, 6, 9, 7, 8, 7, 9, 6, 8, 8, 8, 5, 7, 6, 9, 8, 9, 5, 10, 7, 10, 4, 11, 8]
轮到白棋落子
最佳落子位置:mj

落子序列坐标表示: [7, 7, 9, 7, 8, 8, 9, 9, 10, 8, 9, 8, 9, 10, 9, 6, 9, 5, 10, 7, 8, 9, 8, 5, 11, 8, 7, 4, 6, 3, 11, 7]
轮到黑棋落子
最佳落子位置:ih

落子序列坐标表示: [7, 7, 10, 7, 11, 7, 8, 7, 10, 6, 9, 8, 12, 8, 9, 5, 13, 9, 14, 10, 11, 6, 9, 7, 9, 6, 8, 6, 12, 6, 13, 6, 11, 5, 11, 8]
轮到黑棋落子
最佳落子位置:le

落子序列坐标表示: [7, 7, 9, 7, 8, 7, 9, 8, 8, 9, 9, 9, 9, 6, 9, 10, 9, 11, 8, 8, 7, 8, 6, 9, 10, 6, 8, 6, 10, 5, 11, 4, 7, 5]
轮到白棋落子
最佳落子位置:hg

落子序列坐标表示: [4, 3, 5, 4, 5, 3, 6, 3, 4, 4, 4, 5, 7, 2, 3, 5, 5, 2, 2, 5, 5, 5, 3, 3, 3, 4, 6, 1, 6, 2, 4, 2, 8, 2, 9, 2, 7, 1]
轮到白棋落子
最佳落子位置:ia

落子序列坐标表示: [6, 7, 4, 8, 5, 7, 8, 8, 7, 7, 8, 7, 7, 8, 8, 9, 8, 6, 7, 6]
轮到黑棋落子
最佳落子位置:eh

落子序列坐标表示: [7, 7, 9, 7, 8, 8, 9, 6, 6, 7, 9, 9, 9, 10, 9, 8, 9, 5, 10, 9, 8, 6]
轮到白棋落子
最佳落子位置:ke

落子序列坐标表示: [6, 6, 5, 5, 7, 6, 6, 5, 7, 5, 5, 6, 8, 4, 5, 7, 5, 4, 7, 4, 8, 3, 4, 7, 3, 8, 3, 7, 6, 7, 4, 6]
轮到黑棋落子
最佳落子位置:ge

落子序列坐标表示: [7, 7, 9, 7, 8, 8, 9, 6, 6, 7, 9, 9, 9, 10, 9, 8, 9, 5, 10, 9, 8, 6, 6, 8, 8, 9, 8, 7]
轮到黑棋落子
最佳落子位置:hi

落子序列坐标表示: [7, 7, 9, 7, 8, 8, 9, 6, 6, 7, 9, 9, 9, 10, 9, 8, 9, 5, 10, 9, 8, 6, 6, 8]
轮到黑棋落子
最佳落子位置:ke

落子序列坐标表示: [6, 7, 7, 8, 7, 7, 7, 6, 6, 8, 6, 6, 8, 7, 5, 7, 8, 6, 5, 9, 8, 8, 8, 5, 9, 7]
轮到白棋落子
最佳落子位置:kh

落子序列坐标表示: [7, 7, 6, 6, 5, 7, 6, 7, 8, 5, 6, 8, 6, 5, 6, 9, 6, 10, 7, 5, 7, 10, 7, 8, 8, 9, 5, 6, 9, 8, 10, 7]
轮到黑棋落子
最佳落子位置:gl

落子序列坐标表示: [7, 7, 8, 7, 6, 8, 7, 8, 9, 6, 8, 8, 8, 6, 9, 5, 5, 9, 4, 10, 6, 6, 7, 6, 6, 9, 6, 7, 8, 9, 7, 9, 6, 10, 8, 5, 5, 8, 9, 4, 10, 3, 10, 5, 6, 11]
轮到白棋落子
最佳落子位置:gm

落子序列坐标表示: [7, 7, 9, 7, 8, 7, 9, 8, 8, 9, 9, 9, 9, 6, 9, 10, 9, 11, 8, 8]
轮到黑棋落子
最佳落子位置:hi

落子序列坐标表示: [7, 7, 9, 7, 8, 7, 9, 8, 8, 9, 9, 9, 9, 6, 9, 10, 9, 11, 8, 8, 7, 8, 6, 9, 10, 6, 8, 6, 10, 5, 11, 4, 7, 5, 7, 6, 8, 5, 9, 5]
轮到黑棋落子
最佳落子位置:kh

落子序列坐标表示: [6, 7, 7, 8, 7, 7, 7, 6, 6, 8, 6, 6, 8, 7, 5, 7, 8, 6, 5, 9, 8, 8, 8, 5, 9, 7, 10, 7, 9, 6, 9, 5, 7, 5, 6, 4, 10, 5, 8, 9, 10, 6]
轮到白棋落子
最佳落子位置:hj

ki,mj,ih,le,hg,ia,eh,ke,ge,hi,ke,kh,gl,gm,hi,kh,hj
print(st8_ans[1:])
url8 = 'http://183.175.12.27:8003/step_08'#棋盘图形顺序写入字段ans,结果提交
step8url=url8+'?ans='+st8_ans[1:]
getHml = re.get(step8url)

print(getHml.text)
ki,mj,ih,le,hg,ia,eh,ke,ge,hi,ke,kh,gl,gm,hi,kh,hj
{"is_success": true, "message": "please visit http://183.175.12.27:8000/context/2e329756827e42330f7897cc9499588e"}

提交判断得出的最佳落子的序列,返回第九步地址

1 操作
Ms1254 在 2023-08-11 11:13:53 更新了该帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...