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. jQuery中ready和load的区别

    <span style="white-space:pre">        </span>//document ready $(document).read ...

  2. c/c++技巧总结

    1.bzero().memset()初始化结构体. 2.求结构体分量在结构体中地址偏移量 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) ...

  3. 【Java】Tomcat 5默认的管理员用户和密码

    <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename="tomcat&quo ...

  4. [技术交流ppt]babel7中preset-env的优化使用

    备注 pdf在这里

  5. hibernate课程 初探单表映射1-4 hibernate开发前准备

    开发前准备: 1 eclipse 2 hibernate tools的安装(需要相关的jar包)(可以简化orm框架) hibernate tools的安装步骤: 1 到官网下载 https://so ...

  6. hibernate课程 初探单表映射1-2 ORM定义

    1 什么是ORM? ORM(Object / RelationShip Mapping) 对象/关系映射 面向对象编程(OOP)最终要把对象信息保存在关系性数据库中,要写好多sql语句.这与面向对象编 ...

  7. css最佳实践(reset.css)

    html, body, div, span, object, iframe,h1, h2, h3, h4, h5, h6, p, blockquote, pre,abbr, address, cite ...

  8. C#对话框-打开和保存对话框(转)

    //打开文件            OpenFileDialog openFileDialog = new OpenFileDialog();            openFileDialog.In ...

  9. datagrid数据表格使用总结

    一.加载的css文件 easyui 基本样式: <link href="../easyui/easyui1.5.css" rel="stylesheet" ...

  10. draggable与overflow同时存在,无法拖拽出父元素问题解决

    在使用jquery-ui的拖拽功能对列表内的选项拖拽时,发现无法将选项拖拽出列表的范围,一出范围就自动隐藏在列表下,查找到最后的原因是css中的overflow的原因,overflow存在则不能将选项 ...