python小项目之五子棋游戏


Posted in Python onDecember 26, 2019

本文实例为大家分享了python五子棋游戏的具体代码,供大家参考,具体内容如下

1.项目简介

在刚刚学习完python套接字的时候做的一个五子棋小游戏,可以在局域网内双人对战,也可以和电脑对战

2.实现思路

局域网对战

对于局域网功能来说,首先建立连接(tcp),然后每次下棋时将棋子的坐标发送给对方,当接收到坐标后实例化成棋子对象,这个接收时用了select函数,因为pygame需要循环渲染图片,所以要用非阻塞方式接收消息

select()的机制中提供一fd_set的数据结构,实际上是一long类型的数组, 每一个数组元素都能与一打开的文件句柄(不管是Socket句柄,还是其他文件或命名管道或设备句柄)建立联系,建立联系的工作由程序员完成, 当调用select()时,由内核根据IO状态修改fd_set的内容,由此来通知执行了select()的进程哪一Socket或文件可读或可写,主要用于Socket通信当中

主要代码如下:

# 接收cli的消息
if is_people:
 rs, ws, es = select.select(inputs, [], [], 0)
  for r in rs:
    if r is tcpclisock:
    try:
     data = r.recv(BUFSIZ)
      islink = True
      print(data.decode('utf8'))
      if data.decode('utf8') == 'again':
       is_recieve1 = True
      if data.decode('utf8') == 'yes':
       is_playagain = True
       is_play = True
      if data.decode('utf8') == 'no':
       is_recieve2 = True
       islink = False
      if not is_play and not result:
       me = storn.Storn_Black(eval(data))
       black_chesses.append(me)
        chesses.append(me)
       is_play = True
     except error:
      islink = False

电脑对战

电脑对战的思路也很简单,用了应该是最常见的也是最简单的方法,就是循环遍历棋盘的每一个点,判断该点的价值,选取价值最大的点落子,这个需要对五子棋的棋型有一定了解,这里介绍几种常见的棋型(约定1为己方棋子,2为对方棋子,0为空)

活四(011110):这时候四颗棋子相连,同时两端为空,已经阻止不了一方的胜利了,此时价值应该设置最高
死四(011112|10111|11011):四颗棋子,只有一个地方能形成连五,如果是自己的棋可以赢,是对方的也可以阻止对方赢棋,此时价值次高

就这样把每种棋型判断一下,获得该点的价值,主要代码如下:

# 判断每个点的价值
def point_value(pos, white_chesses, black_chesses, identify1, identify2):
 value = 0
 for i in range(1,9):
  # *1111_ 活四
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == 0:
   value += 40000
  # *11112 死四1
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == identify2:
   value += 30000
  # 1*111 死四2
  if get_point(pos, i, -1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1:
   value += 30000
  # 11*11 死四3
  if get_point(pos, i, -2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, -1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1:
   value += 30000
  # *111_ 活三1
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == 0:
   value += 20000
  # *1_11_ 活三2
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == 0:
   value += 20000
  # *1112 死三1
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify2:
   value += 15000
  # _1_112 死三2
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == identify2:
   value += 15000
  # _11_12 死三3
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == identify2:
   value += 15000
  # 1__11 死三4
  if get_point(pos, i, -1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1:
   value += 15000
  # 1_1_1 死三5
  if get_point(pos, i, -1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1:
   value += 15000
  # 2_111_2 死三6
  if get_point(pos, i, -1, white_chesses, black_chesses) == identify2 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == identify2:
   value += 15000
  # __11__ 活二1
  if get_point(pos, i, -1, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == 0:
   value += 1000
  # _1_1_ 活二2
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == 0:
   value += 1000
  # *1__
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == 0:
   value += 30
  # *1_
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0:
   value += 20
  # *1
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1:
   value += 10
 return value

电脑选择落子位置时,要判断是进攻还是防守,需要两次遍历棋盘,获取进攻时的最大价值和防守的最大价值,主要代码如下:

# 电脑选取落子的位置
def ai(white_chesses, black_chesses, chesses):
 value = max1 = max2 = 0
 pos1 = pos2 = ()
 # 进攻时
 for i in range(0,15):
   row = 28 + i * 40
   for j in range(0,15):
    col = 28 + j * 40
    pos = (row,col)
   if is_empty(pos, chesses):
     continue
    value = point_value(pos, white_chesses, black_chesses, 1, 2)
    if value > max1:
     max1 = value
     pos1 = (row,col)
 
  # 防守时
  for i in range(0,15):
   for j in range(0,15):
    row = 28 + i * 40
    col = 28 + j * 40
    if is_empty((row,col), chesses):
     continue
   value = point_value((row,col), white_chesses, black_chesses, 2, 1)
    if value > max2:
     max2 = value
     pos2 = (row,col)
  if max1 > max2:
   return pos1
  else:
   return pos2

3.游戏截图

python小项目之五子棋游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
使用python的chardet库获得文件编码并修改编码
Jan 22 Python
Python中获取网页状态码的两个方法
Nov 03 Python
python使用正则表达式匹配字符串开头并打印示例
Jan 11 Python
基于Python Numpy的数组array和矩阵matrix详解
Apr 04 Python
python3中获取文件当前绝对路径的两种方法
Apr 26 Python
Python 文本文件内容批量抽取实例
Dec 10 Python
简单了解python的内存管理机制
Jul 08 Python
Python_查看sqlite3表结构,查询语句的示例代码
Jul 17 Python
python-序列解包(对可迭代元素的快速取值方法)
Aug 24 Python
解决pycharm 安装numpy失败的问题
Dec 05 Python
基于Python fminunc 的替代方法
Feb 29 Python
python利用xlsxwriter模块 操作 Excel
Oct 14 Python
python自动识别文本编码格式代码
Dec 26 #Python
Python基于pygame实现单机版五子棋对战
Dec 26 #Python
用python3读取python2的pickle数据方式
Dec 25 #Python
python文件绝对路径写法介绍(windows)
Dec 25 #Python
python 实现将list转成字符串,中间用空格隔开
Dec 25 #Python
python 输出列表元素实例(以空格/逗号为分隔符)
Dec 25 #Python
python 定义类时,实现内部方法的互相调用
Dec 25 #Python
You might like
php 远程关机操作的代码
2008/12/05 PHP
PHP 网页过期时间的控制代码
2009/06/29 PHP
php用数组返回无限分类的列表数据的代码
2010/08/08 PHP
PHP中几种常见的超时处理全面总结
2012/09/11 PHP
php遍历所有文件及文件夹的方法深入解析
2013/06/08 PHP
PHP下的Oracle客户端扩展(OCI8)安装教程
2014/09/10 PHP
深入解析PHP的Yii框架中的event事件机制
2016/03/17 PHP
PHP中phar包的使用教程
2017/06/14 PHP
PHP对称加密算法(DES/AES)类的实现代码
2017/11/14 PHP
php和js实现根据子网掩码和ip计算子网功能示例
2019/11/09 PHP
2020最新版 PhpStudy V8.1版本下载安装使用详解
2020/10/30 PHP
火狐下table中创建form导致两个table之间出现空白
2013/09/02 Javascript
jQuery+PHP星级评分实现方法
2015/10/02 Javascript
利用js获取下拉框中所选的值
2016/12/01 Javascript
jQuery简单获取DIV和A标签元素位置的方法
2017/02/07 Javascript
js中变量的连续赋值(实例讲解)
2017/07/08 Javascript
详谈AngularJs 控制器、数据绑定、作用域
2017/07/09 Javascript
webpack-dev-server自动更新页面方法
2018/02/22 Javascript
vue-cli3 项目从搭建优化到docker部署的方法
2019/01/28 Javascript
微信小程序实现滑动翻页效果(完整代码)
2019/12/06 Javascript
Python编程实现及时获取新邮件的方法示例
2017/08/10 Python
PyQt5打开文件对话框QFileDialog实例代码
2018/02/07 Python
解决python3 urllib 链接中有中文的问题
2018/07/16 Python
python抓取网页内容并进行语音播报的方法
2018/12/24 Python
为何人工智能(AI)首选Python?读完这篇文章你就知道了(推荐)
2019/04/06 Python
python获取txt文件词向量过程详解
2019/07/05 Python
python中时间、日期、时间戳的转换的实现方法
2019/07/06 Python
html5标记文字_动力节点Java学院整理
2017/07/11 HTML / CSS
师恩难忘教学反思
2014/04/27 职场文书
健康教育评估方案
2014/05/25 职场文书
个人简历自荐信
2014/06/26 职场文书
2014年幼儿园安全工作总结
2014/11/10 职场文书
长江三峡导游词
2015/01/31 职场文书
Vue图片裁剪组件实例代码
2021/07/02 Vue.js
pt-archiver 主键自增
2022/04/26 MySQL
Python如何快速找到多个字典中的公共键(key)
2022/04/29 Python