资源简介
python制作的五子棋游戏,利用graphics库来制作,采用ab剪枝法

代码片段和文件信息
from graphics import *
from math import *
import numpy as np
GRID_WIDTH = 40
COLUMN = 15
ROW = 15
list1 = [] # AI
list2 = [] # human
list3 = [] # all
list_all = [] # 整个棋盘的点
next_point = [0 0] # AI下一步最应该下的位置
ratio = 1 # 进攻的系数 大于1 进攻型, 小于1 防守型
DEPTH = 3 # 搜索深度 只能是单数。 如果是负数, 评估函数评估的的是自己多少步之后的自己得分的最大值,并不意味着是最好的棋, 评估函数的问题
# 棋型的评估分数
shape_score = [(50 (0 1 1 0 0))
(50 (0 0 1 1 0))
(200 (1 1 0 1 0))
(500 (0 0 1 1 1))
(500 (1 1 1 0 0))
(5000 (0 1 1 1 0))
(5000 (0 1 0 1 1 0))
(5000 (0 1 1 0 1 0))
(5000 (1 1 1 0 1))
(5000 (1 1 0 1 1))
(5000 (1 0 1 1 1))
(5000 (1 1 1 1 0))
(5000 (0 1 1 1 1))
(50000 (0 1 1 1 1 0))
(99999999 (1 1 1 1 1))]
def ai():
global cut_count # 统计剪枝次数
cut_count = 0
global search_count # 统计搜索次数
search_count = 0
negamax(True DEPTH -99999999 99999999)
print(“本次共剪枝次数:“ + str(cut_count))
print(“本次共搜索次数:“ + str(search_count))
return next_point[0] next_point[1]
# 负值极大算法搜索 alpha + beta剪枝
def negamax(is_ai depth alpha beta):
# 游戏是否结束 | | 探索的递归深度是否到边界
if game_win(list1) or game_win(list2) or depth == 0:
return evaluation(is_ai)
blank_list = list(set(list_all).difference(set(list3)))
order(blank_list) # 搜索顺序排序 提高剪枝效率
# 遍历每一个候选步
for next_step in blank_list:
global search_count
search_count += 1
# 如果要评估的位置没有相邻的子, 则不去评估 减少计算
if not has_neightnor(next_step):
continue
if is_ai:
list1.append(next_step)
else:
list2.append(next_step)
list3.append(next_step)
value = -negamax(not is_ai depth - 1 -beta -alpha)
if is_ai:
list1.remove(next_step)
else:
list2.remove(next_step)
list3.remove(next_step)
if value > alpha:
print(str(value) + “alpha:“ + str(alpha) + “beta:“ + str(beta))
print(list3)
if depth == DEPTH:
next_point[0] = next_step[0]
next_point[1] = next_step[1]
# alpha + beta剪枝点
if value >= beta:
global cut_count
cut_count += 1
return beta
alpha = value
return alpha
# 离最后落子的邻居位置最有可能是最优点
def order(blank_list):
last_pt = list3[-1]
for item in blank_list:
for i in range(-1 2):
for j in range(-1 2):
if i == 0 and j == 0:
continue
if (last_pt[0] + i last_pt[1] + j) in blank_list:
blank_list.remove((last_pt[0] + i last_pt[1] + j))
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-07-14 08:50 gobang_AI-master\
文件 12737 2017-07-14 08:50 gobang_AI-master\README.md
文件 10575 2017-07-14 08:50 gobang_AI-master\gobang_AI.py
文件 31552 2017-07-14 08:50 gobang_AI-master\graphics.py
相关资源
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
评论
共有 条评论