import requests
import time
def quick_pow_mod(params):#RSA加密实现 处理用户认证和密码安全
"""快速模幂运算
参数: [底数, 指数, 模数]
返回: (底数^指数) mod 模数
高效计算大数的模幂运算,用于RSA加密。
"""
base, exponent, mod = params # 解包参数:底数、指数、模数
result = 1 # 初始化结果为1
while exponent > 0: # 当指数大于0时循环
if exponent & 1: # 如果指数是奇数(二进制最后一位为1)
result = result * base % mod # 累乘当前底数并取模
exponent = exponent >> 1 # 指数右移一位(相当于除以2)
base = base * base % mod # 底数平方并取模
return result
def string_to_number(s): #字符串编码转换 处理用户认证和密码安全
"""字符串转数字编码
将每个字符的ASCII码按256进制转换
"""
num = 0 # 初始化结果为0
length = len(s) # 获取字符串长度
for i in range(length):
# 将每个字符视为256进制的一位
num += ord(s[i]) * 256 ** (length - i - 1)
return num
def encrypt_password(pwd):#字符串编码转换 处理用户认证和密码安全
"""RSA密码加密
使用公钥(e,n)对密码进行加密
"""
#加密过程:
#将密码字符串转换为大整数
#使用快速模幂计算 ciphertext = (plaintext^e) mod n
#返回16进制表示的密文
# 公钥参数
e = 65537
n = 135261828916791946705313569652794581721330948863485438876915508683244111694485850733278569559191167660149469895899348939039437830613284874764820878002628686548956779897196112828969255650312573935871059275664474562666268163936821302832645284397530568872432109324825205567091066297960733513602409443790146687029
# 加密流程:字符串→数字→模幂运算→16进制
return hex(quick_pow_mod([string_to_number(pwd), e, n]))
def create_game_session(username, encrypted_pwd):#创建游戏 游戏会话管理
"""创建新游戏会话"""
"""使用GET请求创建游戏
参数包含用户名、加密密码和返回格式
服务器返回包含game_id的JSON响应"""
url = 'http://183.175.12.27:8003/join_game/' # 服务器地址
params = {
'user': username,
'password': encrypted_pwd,
'data_type': 'json' # 要求返回JSON格式
}
response = requests.get(url, params=params) # 发送GET请求
print(f"新游戏已创建: {response.text}") # 打印响应
return response # 返回响应对象
def get_game_status(game_id):
"""获取当前游戏状态"""
url = f'http://183.175.12.27:8003/check_game/{game_id}' # 动态URL
status = requests.get(url) # 发送GET请求
return status # 返回状态对象
def make_move(username, encrypted_pwd, game_id, position):#落子操作
"""在游戏中落子"""
url = f'http://183.175.12.27:8003/play_game/{game_id}'
params = {
'user': username,
'password': encrypted_pwd,
'data_type': 'json',
'coord': position
}
requests.get(url, params=params)#发送落子请求
def coord_to_index(coords):#坐标转换算法
"""坐标转换算法"""
"""棋盘坐标转字符串索引(考虑换行符)"""
y = ord(coords[0]) - ord('a') # 行字母转数字(0-14)
x = ord(coords[1]) - ord('a') # 列字母转数字(0-14)
return y * 16 + x # 计算索引(每行16字符:15点+换行符)
def generate_all_coords():
"""生成所有可能的棋盘坐标"""
coords = []
for y in range(15): # 行 a-o
for x in range(15): # 列 a-o
coords.append(chr(y + 97) + chr(x + 97))
return coords
def get_surrounding_lines(coord, board_state):#坐标转换算法
"""
获取一个位置四周的连线情况
返回: 4个字符串列表,分别代表横、斜、竖、反斜线方向
"""
lines = ['', '', '', '']
for i in range(15):
# 水平线(左右)
x_pos = ord(coord[1]) - ord('a') - 7 + i# 中心点左右各7格
if 0 <= x_pos < 15:# 确保在棋盘内
lines[0] += board_state[(ord(coord[0]) - ord('a')) * 16 + x_pos]
else:
lines[0] += ' ' # 超出边界用空格填充
# 垂直线(上下)
y_pos = ord(coord[0]) - ord('a') - 7 + i
if 0 <= y_pos < 15:
lines[2] += board_state[y_pos * 16 + ord(coord[1]) - ord('a')]
else:
lines[2] += ' '
# 斜线(\)
x_pos = ord(coord[1]) - ord('a') - 7 + i
y_pos = ord(coord[0]) - ord('a') - 7 + i
if 0 <= x_pos < 15 and 0 <= y_pos < 15:
lines[1] += board_state[y_pos * 16 + x_pos]
else:
lines[1] += ' '
# 反斜线(/)
x_pos = ord(coord[1]) - ord('a') + 7 - i
y_pos = ord(coord[0]) - ord('a') - 7 + i
if 0 <= x_pos < 15 and 0 <= y_pos < 15:
lines[3] += board_state[y_pos * 16 + x_pos]
else:
lines[3] += ' '
return lines
def determine_player_color(move_history):
"""根据落子历史确定当前玩家是黑棋还是白棋"""
if (len(move_history) // 2) % 2 == 0:
return 'MO' # 黑棋先手
return 'OM' # 白棋后手
def create_scoring_rules():
"""创建评分规则"""
return {
# 五连珠
("CMMMM", "MCMMM", "MMCMM", "MMMCM", "MMMMC"): 10000,
# 活四
("COOOO", "OCOOO", "OOCOO", "OOOCO", "OOOOC"): 6000,
# 冲四
(".CMMM.", ".MCMM.", ".MMCM.", ".MMMC."): 5000,
# 活三
("COOO.", ".OOOC", ".OOCO.", ".OCOO."): 2500,
# 眠三
("OCMMM.", "OMCMM.", "OMMCM.", "OMMMC.", ".CMMMO", ".MCMMO", ".MMCMO", ".MMMCO"): 2000,
# 活二
(".MMC.", ".MCM.", ".CMM."): 400,
# 其他棋型
(".OOC", "COO.", "MOOOC", "COOOM"): 400,
(".MMCO", ".MCMO", ".CMMO", "OMMC.", "OMCM.", "OCMM.", "MOOC", "COOM"): 200,
(".MC.", ".CM."): 50,
('.'): 1 # 空位基础分
}
def find_best_move(move_history, scoring_rules, all_coords):#最佳落子决策
"""评估棋盘并返回最佳落子位置"""
# 初始化空棋盘(15行,每行15个点+换行符)
board = '\n'.join(['.' * 15 for _ in range(15)])
player_symbols = determine_player_color(move_history)
move_count = 0
# 根据落子历史重建棋盘状态
for i in range(0, len(move_history), 2):
pos = move_history[i:i + 2]
idx = coord_to_index(pos) # 这个函数已经正确处理了换行符的影响
symbol = player_symbols[0] if move_count % 2 == 0 else player_symbols[1]
board = board[:idx] + symbol + board[idx + 1:]
move_count += 1
print(board) # 打印当前棋盘状态
best_pos = ''
highest_score = 0
# 只遍历有效的棋盘位置(跳过换行符)
for i in range(len(board)):
if board[i] == '\n':
continue # 跳过换行符
if board[i] == '.':
# 计算真实坐标索引(减去换行符的影响)
row = i // 16 # 每行16字符(15点+1换行符)
col = i % 16
if col == 15: # 这是换行符位置
continue
# 获取对应的坐标
coord_idx = row * 15 + col # 转换为all_coords中的索引
current_coord = all_coords[coord_idx]
# 在当前空位临时放置我们的棋子('C')
temp_board = board[:i] + 'C' + board[i + 1:]
# 获取通过该位置的所有连线
surrounding_lines = ','.join(get_surrounding_lines(current_coord, temp_board))
# 根据评分规则计算当前得分
current_score = 0
for patterns, value in scoring_rules.items():
for pattern in patterns:
if pattern in surrounding_lines:
current_score += value * surrounding_lines.count(pattern)
# 如果当前得分更高,则更新最佳落子位置
if current_score > highest_score:
highest_score = current_score
best_pos = current_coord
print(f"选择位置 {best_pos}, 得分 {highest_score}", end=' ')
return best_pos
# 主游戏参数
username = '0231121868'
password = '123456lby'
encrypted_password = encrypt_password(password)
scoring_rules = create_scoring_rules()
all_coordinates = generate_all_coords()
# 创建游戏会话
game_response = create_game_session(username, encrypted_password)
game_id = game_response.json()["game_id"]
status = get_game_status(game_id).json()
print("等待对手加入...")
while status['ready'] == "False":
status = get_game_status(game_id).json()
print(status['ready'], end=" ")
time.sleep(5)
# 确定对手
opponent = status['creator'] if status['creator'] != username else status['opponent_name']
# 主游戏循环
while status['ready'] == "True":
if status['current_turn'] == username:
# 我方回合 - 计算并落子
board_state = status['board']
move = find_best_move(board_state, scoring_rules, all_coordinates)
make_move(username, encrypted_password, game_id, move)
print(f"落子位置 {move}")
else:
# 对手回合 - 等待
print(f"等待 {opponent} 落子")
time.sleep(5) # 避免频繁请求
status = get_game_status(game_id).json()
# 检查游戏是否结束
if status['winner'] != "None":
print(f"游戏结束! 胜利者: {status['winner']}")
break
可以修改这个代码,给它加上基于蒙特卡洛树搜索(AlphaZero思想)实现自我对弈的强化对抗训练
基于近端策略优化(Proximal Policy Optimization, PPO)实现与固定对手对战的强化对抗训练吗
最新发布