python小项目之五子棋游戏

作者:程序员lamed 时间:2022-07-12 06:24:23 

本文实例为大家分享了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小项目之五子棋游戏

来源:https://blog.csdn.net/weixin_45342712/article/details/98954371

标签:python,五子棋
0
投稿

猜你喜欢

  • 解决python3 pika之连接断开的问题

    2021-09-28 18:40:09
  • Git 教程之工作区、暂存区和版本库详解

    2022-11-28 04:07:40
  • MYSQL教程:检查数据表和修复数据表

    2009-03-11 15:24:00
  • 服务器端的代码是如何被解释成客户端的?

    2009-11-01 15:15:00
  • 如何制作K线图?

    2010-06-29 17:25:00
  • Python中数组切片的用法实例详解

    2022-09-18 16:51:42
  • 详解Python list和numpy array的存储和读取方法

    2022-05-04 05:58:10
  • 一次Mysql使用IN大数据量的优化记录

    2024-01-29 07:49:19
  • python爬虫用scrapy获取影片的实例分析

    2023-09-25 09:22:30
  • Python标准库uuid模块(生成唯一标识)详解

    2023-07-04 14:03:05
  • SQL里类似SPLIT的分割字符串函数

    2024-01-23 07:59:57
  • 能否用显示/隐藏层来控制FLASH播放与停止

    2008-10-27 14:08:00
  • python中pickle模块浅析

    2022-02-06 06:27:38
  • 语义化的label?

    2009-02-11 12:44:00
  • 40个有创意的jQuery图片和内容滑动及弹出插件收藏集之三

    2024-04-22 22:20:09
  • JavaScript Date()在页面内显示日期

    2008-02-05 10:18:00
  • python如何创建TCP服务端和客户端

    2021-05-20 04:52:52
  • 将MSSQL Server 导入/导出到远程服务器教程的图文方法分享

    2024-01-13 21:55:42
  • Python面向对象实现方法总结

    2022-03-11 08:50:41
  • MATLAB中text函数使用的语法与示例代码

    2022-09-29 00:21:38
  • asp之家 网络编程 m.aspxhome.com