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正则表达式抓取成语网站
Nov 20 Python
python通过get,post方式发送http请求和接收http响应的方法
May 26 Python
Windows下使Python2.x版本的解释器与3.x共存的方法
Oct 25 Python
python批量替换多文件字符串问题详解
Apr 22 Python
一篇文章搞懂Python的类与对象名称空间
Dec 10 Python
基于Python获取照片的GPS位置信息
Jan 20 Python
Pandas时间序列基础详解(转换,索引,切片)
Feb 26 Python
python 的numpy库中的mean()函数用法介绍
Mar 03 Python
PyQt5如何将.ui文件转换为.py文件的实例代码
May 26 Python
Python脚本实现Zabbix多行日志监控过程解析
Aug 26 Python
python collections模块的使用
Oct 16 Python
详解基于python的图像Gabor变换及特征提取
Oct 26 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获取本周星期一具体日期的方法
2015/04/20 PHP
php 计算两个时间相差的天数、小时数、分钟数、秒数详解及实例代码
2016/11/09 PHP
Yii2设置默认控制器的两种方法
2017/05/19 PHP
PHP常用操作类之通信数据封装类的实现
2017/07/16 PHP
Laravel框架使用monolog_mysql实现将系统日志信息保存到mysql数据库的方法
2018/08/16 PHP
Linux基于php-fpm模式的lamp搭建phpmyadmin的方法
2018/10/25 PHP
用js怎么把&字符换成"&amp:"
2006/10/19 Javascript
javascript 装载iframe子页面,自适应高度
2009/03/20 Javascript
基于jQuery的仿flash的广告轮播代码
2010/11/04 Javascript
获取客户端网卡MAC地址和IP地址实现JS代码
2013/03/17 Javascript
js验证整数加保留小数点的简单实例
2013/12/02 Javascript
jQuery实现行文字链接提示效果的方法
2015/03/10 Javascript
Ajax分页插件Pagination从前台jQuery到后端java总结
2016/07/22 Javascript
浅谈JS继承_借用构造函数 & 组合式继承
2016/08/16 Javascript
AngularJS表单基本操作
2017/01/09 Javascript
vue实现的双向数据绑定操作示例
2018/12/04 Javascript
浅谈js中的bind
2019/03/18 Javascript
layer.prompt使文本框为空的情况下也能点击确定的方法
2019/09/24 Javascript
JavaScript定时器使用方法详解
2020/03/26 Javascript
vue 监听 Treeselect 选择项的改变操作
2020/08/31 Javascript
Python标准库之循环器(itertools)介绍
2014/11/25 Python
Python使用minidom读写xml的方法
2015/06/03 Python
Python 最强编辑器详细使用指南(PyCharm )
2019/09/16 Python
互斥锁解决 Python 中多线程共享全局变量的问题(推荐)
2020/09/28 Python
Python通过递归函数输出嵌套列表元素
2020/10/15 Python
详解纯CSS3制作的20种loading动效
2017/07/05 HTML / CSS
详解HTML5中ol标签的用法
2015/09/08 HTML / CSS
6PM官网:折扣鞋、服装及配饰
2018/08/03 全球购物
极度干燥澳大利亚官方网站:Superdry澳大利亚
2019/03/28 全球购物
护士岗位求职应聘自荐书范文
2014/02/12 职场文书
黄继光的英雄事迹材料
2014/02/13 职场文书
大队干部竞选演讲稿
2014/04/28 职场文书
企业优秀团员事迹材料
2014/08/20 职场文书
专题组织生活会思想汇报
2014/10/01 职场文书
css position fixed 左右双定位的实现代码
2021/04/29 HTML / CSS
浅谈Redis的事件驱动模型
2022/05/30 Redis