pythonpygame实现五子棋人机对战之一pythonpygame实现五子棋人机对战之二pythonpygame实现五子棋人机对战之三pythonpygame实现五子棋人机对战之四在之前的文章中已经完成了所有的基础工作剩下的就是把空填上就可以了。六、完成程序# encoding:utf-8 import pygame from pygame.locals import * import sys import os import piece from params import Params from utils import * pygame.init() pygame.mixer.init() #...略见文章之二 https://blog.csdn.net/cxhold/article/details/140133224 ... # 判断棋盘某位置上是否有棋子 def isempty(theposition): #...略见文章之二 https://blog.csdn.net/cxhold/article/details/140133224 ... # 电脑选取落子的位置 def computerdecision(): value max1 max2 0 pos1 pos2 () # 进攻 for i in range(0, rows): row 28 i * blocksize for j in range(0, rows): col 28 j * blocksize pos (row, col) if isempty(pos): continue value pointvalue(chess_map,pos, 1, 2) if value max1: max1 value pos1 (row, col) # 防守 for i in range(0, rows): for j in range(0, rows): row 28 i * blocksize col 28 j * blocksize if isempty((row, col)): continue value pointvalue(chess_map,(row, col), 2, 1) if value max2: max2 value pos2 (row, col) if max1 max2: return pos1 else: return pos2 # 初始化棋盘 def init(): #...略见文章之二 https://blog.csdn.net/cxhold/article/details/140133224 ... def main(): #...略见文章之二 https://blog.csdn.net/cxhold/article/details/140133224 ... running True clock pygame.time.Clock() # 字体 #...略见文章之二 https://blog.csdn.net/cxhold/article/details/140133224 ... # 初始化 init() while running: screen.blit(bg_image, (0, 0)) # 绘制游戏菜单 if not is_choise: screen.blit(zz_image, (0, 0)) #遮罩层 screen.blit(txtplayerandplayer, txtplayerandplayer_rect) screen.blit(txtplayerandcomputer, txtplayerandcomputer_rect) if not is_play_sound: screen.blit(txtclosesound, txtclosesound_rect) else: screen.blit(txtplaysound, txtplaysound_rect) # 绘制棋盘 if is_choise: if chesses: for i in chesses: screen.blit(i.image, i.image_rect()) for event in pygame.event.get(): if event.type QUIT: pygame.quit() sys.exit() if event.type MOUSEBUTTONDOWN: #人下棋 if event.button 1: if is_choise: pos pygame.mouse.get_pos() # 判断是否是人下棋时间 if is_player and not is_finish: me piece.Piece(pos,pieces_white.png) if not isempty(me.pointtrans()): white_chesses.append(me) white_positions.append(me.pointtrans()) chesses.append(me) chess_map[str(me.pointtrans()[0]) , str(me.pointtrans()[1])] 1 is_player False piece_sound.play() # 判断输赢 新增判断输赢 if len(white_chesses)5 and not is_finish and checkwin(white_positions,me.pointtrans()[0],me.pointtrans()[1]) : is_finish True white_win True else: del (me) else: pos pygame.mouse.get_pos() if txtplayerandplayer_rect.left pos[0] txtplayerandplayer_rect.left 170 and \ txtplayerandplayer_rect.top 100 pos[1] txtplayerandplayer_rect.top 130: is_choise True people2computer True if txtplayerandplayer_rect.left pos[0] txtplayerandplayer_rect.left 160 and \ txtplayerandplayer_rect.top 200 pos[1] txtplayerandplayer_rect.top 230: is_play_sound not is_play_sound if not is_play_sound: pygame.mixer.stop() pygame.mixer.music.stop() else: pygame.mixer.music.play() if is_finish : pos pygame.mouse.get_pos() if successtext_rect.left pos[0] successtext_rect.right - 50 and \ successtext_rect.top pos[1] successtext_rect.top 30: if people2computer: is_playagain True is_player True is_finish False init() if successtext_rect.left pos[0] successtext_rect.right - 50 and \ successtext_rect.top 50 pos[1] successtext_rect.top 120: people2people False main() # 电脑落子 if people2computer and not is_player: robot piece.Piece(computerdecision(),pieces_black.png) black_chesses.append(robot) black_positions.append(robot.pointtrans()) chesses.append(robot) chess_map[str(robot.pointtrans()[0]) , str(robot.pointtrans()[1])] 2 is_player True piece_sound.play() # 判断输赢 if len(black_chesses)5 and not is_finish and checkwin(black_positions,robot.pointtrans()[0],robot.pointtrans()[1]) : is_finish True black_win True #...略见文章之二 https://blog.csdn.net/cxhold/article/details/140133224 if is_playagain: #...略见文章之二 https://blog.csdn.net/cxhold/article/details/140133224 pygame.display.flip() clock.tick(60) if __name__ __main__: main()理解一下这个过程人下了一个子赋值给me然后添加到white_chesseswhite_positionschesses中改变chess_map相应坐标上的值为1is_player设置为False表示人不能再下棋了当white_chesses的个数是5个以上时调用checkwin判断是否连5。如果还要继续则下棋权力在电脑调用computerdecision函数算出最有利的步骤电脑下黑棋赋值给robot然后添加到black_chessesblack_positionschesses中改变chess_map相应坐标上的值为2is_player设置为True表示下一步由人下棋了当black_chesses的个数是5个以上时调用checkwin判断是否连5。到此为止实现pythonpygame五子棋人机对战。后续会使用套接字socket实现联网对战。pythonpygame实现五子棋人机对战源码及资源另一个五子棋电脑自动应手代码就不详细讲了大家可以直接参考下面的源码pythonpygame实现五子棋人机源码电脑应手两套
企业数字化 ERP 产品动态
相关推荐
开放式代码审查:从形式化走查到团队知识共创的落地实践 1. 从“形式化走查”到“开放式共创”:我为什么重写团队代码审查体系先说一个我踩过的坑。几年前带前端小组的时候,组里代码审查率看起来挺高——PR 基本都能在 24 小时内合入,评论区也有互动。但后来我做了一次代码缺陷复盘,发现… · 2026/9/23 10:34:38
python+pygame实现五子棋网络对战之一 之前我们系统地描述了五子棋的人机对战,了解了五子棋的基本操作,例如电脑的应手算法,输赢判断等等。那如何实现五子棋的网络对战,即人人对战呢?这里就用到了套接字socket编程,听上去很高级,其实… · 2026/9/23 10:34:38
Cocotb PCIe仿真:Python驱动Verilog验证实战 简介:这份资源是面向数字IC验证工程师与PCIe协议学习者的Cocotb仿真框架,聚焦用Python驱动Verilog硬件模型完成PCI Express验证。包内共55个文件,以38个Python脚本为核心,承担测试序列生成、协议层校验与结果分析;5个V… · 2026/9/23 10:34:19
自然月、自然周、自然日:统计口径的底层逻辑与实战避坑指南 1. 时间维度里的“自然”到底指什么刚入行做数据统计那会儿,我对“自然月”这个词一直没太在意,觉得不就是每个月1号到月底嘛,能有什么讲究。直到有一次做用户活跃度报表,业务方看完之后问我:“为什么2月份的数据比1月… · 2026/9/23 11:13:58
华为hisuite实战避坑:3个完整示例帮你搞定驱动难题 华为hisuite实战避坑:3个完整示例帮你搞定驱动难题 复制来的代码跑不通不知道怎么调?别急着甩锅给编译器。很多时候,问题不在语法,而在环境依赖和底层接口调用的微妙差异。尤其是在处理华为手机设备时,HiSuite作为官方配套工具,其接口行… · 2026/9/23 11:13:52
Python+CNN网络入侵检测:从PCAP到实时告警的完整实践 简介:本资源是一套基于Python与卷积神经网络(CNN)实现的网络入侵检测系统源码,面向网络安全方向的初学者、高校学生及AI安全实践者,解决传统规则引擎在未知攻击识别上的局限性,提供可复现的深度学习落地方案… · 2026/9/23 11:13:52
RGB图像预测叶绿素含量:农业AI轻量化落地实践 简介:本资源是一套基于Python机器学习实现图像数据预测植物叶绿素含量的完整毕业设计项目,面向软件工程、人工智能、电子信息等专业的本科生与研究生,解决农业遥感、作物生理参数无损检测中的建模与部署实际问题。压缩包共9个文件(… · 2026/9/23 11:13:45
3招搞定手机怎么下载微信面试难题实战项目解析 3招搞定手机怎么下载微信面试难题实战项目解析 面试被问“手机怎么下载微信”背后的原理,90%的人答不上来。别笑,这看似弱智的问题,实则是考察你对移动应用分发机制、安全校验及网络协议理解的试金石。我带过不少校招新人,他们背了八股文,却连一个A… · 2026/9/23 0:00:03
你有新短消息请注意查收:3个新手避坑指南搞定消息系统选型 你有新短消息请注意查收:3个新手避坑指南搞定消息系统选型 面试被问“高并发下如何保证消息不丢失”,你张口就是“用Redis”,结果面试官追问“如果Redis宕机了怎么办”,你瞬间卡壳。这种场景太常见了,很多新手在背八股文时,只记住了技术名词… · 2026/9/23 0:00:29