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. (转)不看绝对后悔的Linux三剑客之grep实战精讲

    不看绝对后悔的Linux三剑客之grep实战精讲 原文:http://blog.51cto.com/hujiangtao/1923675 https://www.cnblogs.com/peida/a ...

  2. easyui databox获取当前时间

    class=easyui-datebox $(document).ready(function() {             $("#thedate").datebox(&quo ...

  3. CONCAT substr group_concat find_in_set

    (SELECT p.*,(SELECT CONCAT(m.name,m.id) FROM service_fastfix_category m WHERE m.id=SUBSTR(p.id,1,4)) ...

  4. JAVA 多线程 Callable 与 FutureTask:有返回值的多线程

    java多线程中,如果需要有返回值,就需要实现Callable接口. 看例子: 先建立一个Dowork这个类,就是平时某个业务的实现 package com.ming.thread.one; impo ...

  5. 使用PM2守护Node.js应用

    PM2简介 PM2是node进程管理工具,可以利用它来简化很多node应用管理的繁琐任务,如性能监控.自动重启.负载均衡等,而且使用非常简单.   安装PM2 $ npm install pm2 -g ...

  6. 八个cmd 命令

    一,ping 它是用来检查网络是否通畅或者网络连接速度的命令.作为一个生活在网络上的管理员或者黑客来说,ping命令是第一个必须掌握的DOS命令,它所利用的原理是这样的:网络上的机器都有唯一确定的IP ...

  7. C#开发android应用实战 源码

    原书名: Professional Android Programming with Mono for Android and .NET/C#     Download Title Size Down ...

  8. 解决windows7系统的快捷方式无法添加到任务栏

    #以下4条,进入cmd命令界面下逐个执行cmd /k reg add "HKEY_CLASSES_ROOT\lnkfile" /v IsShortcut /fcmd /k reg ...

  9. 3D向2D投影

    http://blog.sina.com.cn/s/blog_536e0eaa0100jn7j.html

  10. LeetCode Implement strStr() 实现strstr()

    如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ int strStr(char* haystack, char* needle) { ; ; ; ++i) { ; ; ++j ...