import pygame
import pygame.ftfont
from pygame import *
import sys
import button
import random
import re
import copy
colors = {
"white": (255, 255, 255),
"black": (0, 0, 0)
}
r_lines = [
((20, 20), (20, 620)),
((20, 620), (620, 620)),
((20, 20), (620, 20)),
((620, 20), (620, 620))
]
# 棋形匹配
l = [[] for i in range(5)]
l[4] = ["11111"]
l[3] = ["011110", "211110"]
l[2] = ["11101", "01110", "211100", "2011102"]
l[1] = ["011010", "11011", "211010", "210110", "11001", "001100", "211000"]
l[0] = ["10101", "01010", "010010", "210100", "210010", "10001"]
# 棋形与分值索引
values = {
# 连五
"11111": 999900000,
# 活四
"011110": 33300000,
# 冲四
"211110": 6250000,
"11101": 6250000,
"11011": 6250000,
# 活三
"01110": 625000,
"011010": 625000,
# 眠三
"211100": 12500,
"211010": 12500,
"210110": 12500,
"11001": 12500,
"10101": 12500,
"2011102": 12500,
# 活二
"001100": 250,
"01010": 250,
"010010": 250,
# 眠二
"211000": 25,
"210100": 25,
"210010": 25,
"10001": 25,
# 其他
"others": 8
}
# 位置得分, 越靠近中心位置分越高
values_of_pos = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0],
[0, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 0],
[0, 2, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 2, 0],
[0, 2, 4, 6, 7, 7, 7, 7, 7, 7, 7, 6, 4, 2, 0],
[0, 2, 4, 6, 7, 8, 8, 8, 8, 8, 7, 6, 4, 2, 0],
[0, 2, 4, 6, 7, 8, 9, 9, 9, 8, 7, 6, 4, 2, 0],
[0, 2, 4, 6, 7, 8, 9, 10, 9, 8, 7, 6, 4, 2, 0],
[0, 2, 4, 6, 7, 8, 9, 9, 9, 8, 7, 6, 4, 2, 0],
[0, 2, 4, 6, 7, 8, 8, 8, 8, 8, 7, 6, 4, 2, 0],
[0, 2, 4, 6, 7, 7, 7, 7, 7, 7, 7, 6, 4, 2, 0],
[0, 2, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 2, 0],
[0, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 0],
[0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
me_first = 1
ai_first = 0 # 设置先后手规则
first = me_first # 默认先手为玩家, 即玩家持黑
Myturn = 1 # 玩家回合
AIturn = 0 # 电脑回合
flag = Myturn # 设置轮流黑白手标志
game_state = 0 # 游戏状态, 0为未决胜负, 1为黑子胜, -1为白子胜
b_num = 0 # 黑子个数
w_num = 0 # 白子个数
MAX = 100000000000 # 正无穷
MIN = -100000000000 # 负无穷
deep = 3 # 规定ai搜索深度
drawed = True # 画出一方的棋另一方再走
point = [[-1] * 3 for i in range(200)] # 存储n中走法对应的点的横坐标[n][0]纵坐标[n][1]及value值[n][2]
num_points = 0 # 记录可走步的个数
p1 = [0, 0] #
p2 = [0, 0] #
pygame.init() # 初始化pygame
pygame.mixer.init() # 初始化mixer
# 设置屏幕大小和标题
size = width, height = 740, 640 # 设置窗口大小
screen = pygame.display.set_mode(size) # 显示窗口
pygame.display.set_caption("欢乐五子棋 -- (HMR出品)")
font = pygame.font.SysFont('comicsansms', 16) # 字体
ai_text = font.render('AI Steps', True, (84, 115, 135))
player_text = font.render('Player Steps', True, (84, 115, 135))
clock = pygame.time.Clock() # 设置时钟,定时刷新
whiteq = [] # 存储白棋位置
blackq = [] # 存储黑棋位置
chess = [[0] * 15 for i in range(15)] # 用来判断棋盘某处是否有棋,1为黑棋, -1为白棋, 0为空
# 更新chess状态
def renew_chess(chess, blackq, whiteq):
for xx in range(15):
for yy in range(15):
chess[xx][yy] = 0
for xx, yy in blackq:
chess[int((yy - 20) / 40)][int((xx - 20) / 40)] = 1 # 黑棋
for xx, yy in whiteq:
chess[int((yy - 20) / 40)][int((xx - 20) / 40)] = -1 # 白棋
# 按钮响应函数
def do_start(btn): # 开始按钮的响应函数
global game_state, flag, b_num, w_num, blackq, whiteq
game_state = 0 # 重置游戏状态及其他全局变量
b_num = 0
w_num = 0
blackq = []
whiteq = []
if first == me_first:
flag = Myturn
if first == ai_first:
flag = AIturn
def do_quit(btn): # 退出按钮的响应函数
pygame.quit()
exit()
def do_ai_first(btn): # 电脑先手
global first, flag
first = ai_first
do_start(btn)
def do_me_first(btn): # 玩家先手
global first, flag
first = me_first
do_start(btn)
# 画棋盘背景
def draw_background(screen):
# 加载背景图片
bg_image = pygame.image.load('D:/py/5go/bg3.png') # 加载图片
screen.blit(bg_image, (20, 20))
# 画网格线,15x15
for line in r_lines:
pygame.draw.line(screen, colors["black"], line[0], line[1], 2)
for i in range(15):
pygame.draw.line(screen, colors["black"], (20, 20 + 40 * i), (620, 20 + 40 * i), 1) # 画行
pygame.draw.line(screen, colors["black"], (20 + 40 * i, 20), (20 + 40 * i, 620), 1) # 画列
# 画棋子
def draw_gos(screen, blackq):
# 画黑棋
for xq, yq in blackq:
b_ = pygame.image.load('D:/py/5go/black.png') # 加载图片
b_rect = b_.get_rect()
b_rect.centerx = xq
b_rect.centery = yq
screen.blit(b_, b_rect)
# 画白棋
for xq, yq in whiteq:
w_ = pygame.image.load('D:/py/5go/white.png') # 加载图片
w_rect = w_.get_rect()
w_rect.centerx = xq
w_rect.centery = yq
screen.blit(w_, w_rect)
# 获取以p点为中心的9个点的连线棋形
def getline(chess, px, py, dir_offset, discolor):
line = [0 for i in range(9)]
tmp_x = px + (-5 * dir_offset[0])
tmp_y = py + (-5 * dir_offset[1])
for i in range(9):
tmp_x += dir_offset[0]
tmp_y += dir_offset[1]
if (tmp_x < 0 or tmp_x >= 15 or
tmp_y < 0 or tmp_y >= 15):
line[i] = discolor # 若超界则假设为对手棋
else:
line[i] = chess[tmp_y][tmp_x] # 黑1白-1
return line
# 分析棋形并得到该方向的分值,最后的参数是,有利分为1不利分为-1
def get_line_score(line, adcolor, discolor, weight, px, py):
global l, values
new_line = [[] for i in range(9)]
num = 0 # 统计连子个数
for i in range(line.index(adcolor), 9):
if line[i] == adcolor:
num += 1
# 将line格式统一化,转字符串,己方棋子为1,对方棋子为2,空为0,便于匹配
for i in range(9):
if line[i] == adcolor:
new_line[i] = '1'
elif line[i] == discolor:
new_line[i] = '2'
else:
new_line[i] = '0'
s = ''.join(new_line) # 转化为字符串
fan_s = s[::-1] # 字符串反转
# 逐个匹配
if num > 5:
return values["11111"] * weight
if 5 >= num >= 1:
for each in l[num - 1]:
result = re.search(each, s)
if result != None: # 找到匹配项
return (values[result.group()] + values_of_pos[py][px]) * weight
result = re.search(each, fan_s)
if result != None: # 找到匹配项
return (values[result.group()] + values_of_pos[py][px]) * weight
# 未找到匹配项,为单子情况
return (values["others"] + values_of_pos[py][px]) * weight
# 评估函数,评估当前棋局的分值
def Evaluate(chess):
advantage = 0 # 有利分, 对上一层有利的分!!
disadvantage = 0 # 不利分, 玩家所持棋子得分, 取负
adcolor = 0 # ai所持棋子颜色
discolor = 0 # 玩家所持棋子颜色
dir_offset = [(1, 0), (0, 1), (1, 1), (1, -1)] # 四个方向,右,上,右上,右下
if first == me_first:
adcolor = 1 # ai持白子
discolor = -1
if first == ai_first:
adcolor = -1 # ai持黑子
discolor = 1
番茄牛腩煲
- 粉丝: 126
- 资源: 1
最新资源
- tomcat6.0配置oracle数据库连接池中文WORD版最新版本
- hibernate连接oracle数据库中文WORD版最新版本
- MyEclipse连接MySQL的方法中文WORD版最新版本
- MyEclipse中配置Hibernate连接Oracle中文WORD版最新版本
- MyEclipseTomcatMySQL的环境搭建中文WORD版3.37MB最新版本
- hggm - 国密算法 SM2 SM3 SM4 SM9 ZUC Python实现完整代码-算法实现资源
- SQLITE操作入门中文WORD版最新版本
- Sqlite操作实例中文WORD版最新版本
- SQLITE特性分析中文WORD版最新版本
- ORACLE创建表空间中文WORD版最新版本
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈