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为空)

  1. 活四(011110):这时候四颗棋子相连,同时两端为空,已经阻止不了一方的胜利了,此时价值应该设置最高
  2. 死四(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.游戏截图

源代码地址:https://github.com/tctctctctc/python- 欢迎star

有不足之处还请指正

Python小项目之五子棋的更多相关文章

  1. Python小项目四:实现简单的web服务器

    https://blog.csdn.net/u010103202/article/details/74002538 本博客是整理在学习实验楼的课程过程中记录下的笔记形成的,参考:https://www ...

  2. 第一个Python小项目:图片转换成字符图片

    实现的效果:                                                                                               ...

  3. python小项目之微信远程控制

    前两天接触了一个有趣的python模块--itchat,这个模块可以非常方便的操作微信,今天就来使用这个模块来实现微信远程控制. 环境准备 itchat模块不是python标准模块(内置模块),是一个 ...

  4. 给大家推荐:五个Python小项目,Github上的人气很高的

    1.深度学习框架 Pytorch https://github.com/pytorch/pytorch PyTorch 是一个 Torch7 团队开源的 Python 优先的深度学习框架,提供两个高级 ...

  5. [IT学习]Python 小项目 通讯录 思路

    建立一个通讯录查询软件,暂时只支持按姓名检索.出发点:无需登录企业门户,即可检索.要注意保护员工手机号,除非他自己同意显示. 欢迎您访问www.cnblogs.com/viphhs.转载请联系作者授权 ...

  6. python小项目之文本编辑器

    高考完后这么久才想起这系列教程,实在抱歉,现在该来继续教程了. 本节利用前面所学知识,来完成一个小工具--文本编辑器! tkinter 在实现文本编辑器之前,先来了解下tkinter这个python库 ...

  7. 【计算机视觉】OpenCV篇(4) - Pycharm+PyQt5+Python小项目实战

    1.下载安装 (1)Pycharm:下载链接 (2)推荐使用Qt Designer来设计界面,如果你装的是Anaconda的话,就已经自带了designer.exe,我这里使用的是Pycharm的虚拟 ...

  8. JAVA小项目之五子棋

    五子棋V1.0 功能: 人人对战,人机对战(初级) 记录双方分数: 主要知识点: 二维坐标系中,各方向坐标的关系及规律. 效果图: 主框架类: package com.gxlee.wzq; /** * ...

  9. python小项目练习之转换像素图片为字符图

    实例来源实验楼网站,没事可以多逛逛,在此多谢实验楼的无私分享 from PIL import Image import argparse """ description: ...

随机推荐

  1. 练习九:time.sleep方法

    让python程序暂停预定时间后再运行,需要用到time.sleep方法要求,随便写入一段代码,测试time.sleep方法 import time dict1 = {1:'a',2:'b',3:'c ...

  2. 物理机和虚拟机互相可以ping通,还是无法连接

    关闭防火墙服务 CentOS # systemctl stop firewalld.service Debian # iptables -F Ubuntu # ufw disable 安装SSH服务 ...

  3. Gym 100886J Sockets 二分答案 + 贪心

    Description standard input/outputStatements Valera has only one electrical socket in his flat. He al ...

  4. FIRST集和FOLLOW集,FIRSTVT集和LASTVT集的求法

    学习编译原理时, 这几个集合相信大家并不陌生:FIRST.FOLLOW.FIRSTVT.LASTVT. 其中First和Follow是一对,而Firstvt和Lastvt是一对. 它们的作用分别是: ...

  5. Spring注解之@Lazy注解,源码分析和总结

    一 关于延迟加载的问题,有次和大神讨论他会不会直接或间接影响其他类.spring的好处就是文档都在代码里,网上百度大多是无用功. 不如,直接看源码.所以把当时源码分析的思路丢上来一波. 二 源码分析 ...

  6. 操作文件方法简单总结(File,Directory,StreamReader,StreamWrite )(转载)

    本文转自http://www.cnblogs.com/zery/p/3315889.html 对于文件夹,文档的操作一直处于一知半解状态,有时间闲下来了,好好练习了一把,对文档,文件的操作有了一个基本 ...

  7. 初识quartz 并分析 项目中spring整合quartz的配置【原创+转载】

    初识quartz 并分析 项目中spring整合quartz的配置[原创+转载]2018年01月29日 12:08:07 守望dfdfdf 阅读数:114 标签: quartz 更多个人分类: 工具 ...

  8. a href="javascript:"与a href="#"

    <a href="javascript:;"></a> <a href="#"></a> 这两种写法.这两种写法 ...

  9. ArcGIS中Features与JSON的互相转化

    实际操作过程非常简单,这里就简单记录下转换工具的位置:

  10. notepad++ 等用正则表达式自动添加sql引号(宏)

    一般sql语句会经常用到给括号里的内容添加引号,sql如下 Select * From Test ', ', ', ', ', '); 一开始参考了http://blog.sina.com.cn/s/ ...