新闻详情

新闻详情

首页 / 资讯中心 / 详情

python+pygame实现五子棋人机对战之五

发布时间:2026/11/7 20:28:56来源:尧图网络
python+pygame实现五子棋人机对战之五
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实现五子棋人机源码电脑应手两套
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

【外设】之大彩串口显示屏 2026/11/7 13:27:02

【外设】之大彩串口显示屏

大彩串口屏初步使用 1 .官网下载 STM32 屏幕 GUI 设计资料 http://www.gz-dc.com/category/typeid/4112 找到 STM32 Keil 工程,移植相关代码因项目而异进行移植,由于项目简单,本人只对用到的指令接口进行修改。 比如:注意事项&…

阅读更多 →
无法下载Windows系统iso文件 2026/11/7 0:39:20

无法下载Windows系统iso文件

当我遇到这个问题的时候,我打开了一个网站: 登录 然后我打算下载的时候: 突然那个官方的连接就可以下载了:

阅读更多 →
【清华代码熊】DeepSeek V4.1 Flash 后训练详解 2026/11/7 6:36:21

【清华代码熊】DeepSeek V4.1 Flash 后训练详解

📌 上期解析了 DeepSeek V4.1 Flash 模型架构改进,本期解析 DeepSeek V4.1 Flash 预训练/后训练技术: 🌟 预训练:45T 文本 多模态混合语料、直接训练 sparse attention(取消 DeepSeek V4 的 dense 冷启动&…

阅读更多 →
Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ... 2026/11/6 17:25:36

Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ...

文章主要内容和创新点 主要内容 本文聚焦于多模态大语言模型(MLLM)强化学习(RL)训练中的效率问题,提出了一个名为Shuffle-R1的框架。研究发现,当前RL训练存在两个关键缺陷: 优势值坍缩(Advantage Collapsing):批次中大多数优势值集中在零附近,导致有效梯度信号被淹…

阅读更多 →
PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction 2026/11/6 14:29:05

PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction

一、文章主要内容总结 本文聚焦于利用大型语言模型(LLMs)实现个人身份信息(PII)脱敏的研究,旨在解决传统脱敏方法(如基于规则的系统、领域特定命名实体识别(NER)模型)泛化能力差、跨格式/跨语境适应性弱的问题。 研究通过全面评估多种LLM架构(包括密集型LLM(D-LLM…

阅读更多 →
LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model 2026/11/6 14:29:07

LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model

文章主要内容和创新点 主要内容 本文聚焦于二进制图像-文本相关性评估任务(判断图像与文本“相关”或“不相关”),针对该任务中文本格式多样、相关性定义随场景变化等挑战,提出了基于多模态大语言模型(MLLM)的解决方案LLaVA-RE。 模型设计:LLaVA-RE基于LLaVA 1.5架构,…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉