Pygame——AI重力四子棋

article/2025/7/11 0:32:10

突然奇想,什么时候可以自己实现一个机器对战的小游戏,但一直不敢去尝试,直到偶尔发现了重力四子棋的规则,有限的空间棋盘正好可以拿来练手。

有关下棋AI的算法,说来说去也就那么几种,随机蒙特卡罗方法、UCT信心上限树、博弈论(Alpha−Beta剪枝)...

这个小游戏我时间更多的花在ui界面和游戏逻辑的实现,可以切换先后手顺序,添加了重置游戏和暂停游戏的规则,可落子节点辅助提示,以及对应的文字提示信息。

算法采用了无忧化的蒙特卡罗方法,模拟当前棋盘状态可落子节点扩展的胜利可能性,模拟的次数越多,时间自然也就越久,但智能度相应着也会提高。

源代码:https://download.csdn.net/download/qq_40204582/11212878

代码开源了,如果有复制过去的朋友,可以顺手点个赞吗^_^

import pygame, sys
from pygame.locals import *
import time
import random
import copy# game parameters
pygame.init()
win_width, win_height = 930, 700
displaysurf = pygame.display.set_mode((win_width, win_height), 0, 32)
pygame.display.set_caption('Connect_4')# color parameters
backgroundcolor_chess = (244, 171, 102)
color_white = (255, 255, 255)
color_black = (0, 0, 0)
color_tip_white = (225, 225, 225)
color_tip_black = (25, 25, 25)
color_green = (0, 255, 0)# chess parameters
chess_grid_row, chess_grid_col = 6, 8
chess_list = []
for i in range(chess_grid_row):new_line = [0 for j in range(chess_grid_col)]chess_list.append(new_line)
player = True
play_flag = False# draw chessboard
def draw_chessboard():displaysurf.fill(color_white)fontobj = pygame.font.SysFont('SimHei',70)text = fontobj.render("重力四子棋", True, color_black, color_white)textrect = text.get_rect()textrect.center = (430, 70)displaysurf.blit(text, textrect)pygame.draw.rect(displaysurf, backgroundcolor_chess, (50, 170, 640, 480))for pix_row in range(7):pygame.draw.line(displaysurf, color_black, (50, 170 + pix_row * 80), (690, 170 + pix_row * 80))for pix_col in range(9):pygame.draw.line(displaysurf, color_black, (50 + pix_col * 80, 170), (50 + pix_col * 80, 650))def draw_tip_chess(mousex, mousey, type):for row in range(chess_grid_row):for col in range(chess_grid_col):if chess_list[row][col] in [3, 4]:chess_list[row][col] = 0col = int((mousex - 50) / 80)row = int((mousey - 170) / 80)if row == chess_grid_row:row -= 1if col == chess_grid_col:col -= 1if row < chess_grid_row - 1:if chess_list[row + 1][col] == 0:returnif chess_list[row][col] == 0:chess_list[row][col] = typedef clear_tip_chess():for row in range(chess_grid_row):for col in range(chess_grid_col):if chess_list[row][col] in [3, 4]:chess_list[row][col] = 0def draw_check_chess(mousex, mousey, type):for row in range(chess_grid_row):for col in range(chess_grid_col):if chess_list[row][col] in [3, 4]:chess_list[row][col] = 0col = int((mousex - 50) / 80)row = int((mousey - 170) / 80)if row == chess_grid_row:row -= 1if col == chess_grid_col:col -= 1if row < chess_grid_row - 1:if chess_list[row + 1][col] == 0:returnif chess_list[row][col] in [1, 2]:return Falseelse:chess_list[row][col] = typereturn Truedef draw_chess():for row in range(chess_grid_row):for col in range(chess_grid_col):if chess_list[row][col] == 0:pygame.draw.circle(displaysurf, backgroundcolor_chess, (90 + col * 80, 210 + row * 80), 38)elif chess_list[row][col] == 1:pygame.draw.circle(displaysurf, color_black, (90 + col * 80, 210 + row * 80), 38)elif chess_list[row][col] == 2:pygame.draw.circle(displaysurf, color_white, (90 + col * 80, 210 + row * 80), 38)elif chess_list[row][col] == 3:pygame.draw.circle(displaysurf, color_tip_black, (90 + col * 80, 210 + row * 80), 38)elif chess_list[row][col] == 4:pygame.draw.circle(displaysurf, color_tip_white, (90 + col * 80, 210 + row * 80), 38)def is_win(temp_chess_list):not_null_sum = 0for row in range(chess_grid_row):for col in range(chess_grid_col):if temp_chess_list[row][col] in [3, 4]:temp_chess_list[row][col] = 0if temp_chess_list[row][col] != 0:not_null_sum += 1# horizontalif col < chess_grid_col - 3:if temp_chess_list[row][col] == temp_chess_list[row][col + 1] == temp_chess_list[row][col + 2] == \temp_chess_list[row][col + 3] and temp_chess_list[row][col] != 0:return temp_chess_list[row][col]# verticalif row < chess_grid_row - 3:if temp_chess_list[row][col] == temp_chess_list[row + 1][col] == temp_chess_list[row + 2][col] == \temp_chess_list[row + 3][col] and temp_chess_list[row][col] != 0:return temp_chess_list[row][col]# right slantif col < chess_grid_col - 3 and row < chess_grid_row - 3:if temp_chess_list[row][col] == temp_chess_list[row + 1][col + 1] == temp_chess_list[row + 2][col + 2] == \temp_chess_list[row + 3][col + 3] and temp_chess_list[row][col] != 0:return temp_chess_list[row][col]# left slantif col >= 3 and row < chess_grid_row - 3:if temp_chess_list[row][col] == temp_chess_list[row + 1][col - 1] == temp_chess_list[row + 2][col - 2] == \temp_chess_list[row + 3][col - 3] and temp_chess_list[row][col] != 0:return temp_chess_list[row][col]if not_null_sum == chess_grid_row*chess_grid_col:return 3return 0def AI(chess_list, type):node_list = []chess_null_sum = 0for col in range(chess_grid_col):for row in range(chess_grid_row):if chess_list[row][col] == 0:chess_null_sum += 1if row == chess_grid_row -1:temp_chess_list = copy.deepcopy(chess_list)temp_chess_list[row][col] = typenode_list.append([row, col])flag = is_win(temp_chess_list)if flag != 0:chess_list[row][col] = typereturn row, colelif chess_list[row+1][col] != 0:temp_chess_list = copy.deepcopy(chess_list)temp_chess_list[row][col] = typenode_list.append([row, col])flag = is_win(temp_chess_list)if flag != 0:chess_list[row][col] = typereturn row, colprint(node_list)range_sum = 500 + int((chess_grid_row*chess_grid_col-chess_null_sum)/(chess_grid_row*chess_grid_col)*100)print(range_sum)win_list = [0 for j in range(chess_grid_col)]tip_list = [0 for j in range(chess_grid_col)]start = time.clock()for i in range(len(node_list)):for j in range(100):temp_type = typetemp_chess_list = copy.deepcopy(chess_list)temp_node_list = copy.deepcopy(node_list)temp_chess_list[temp_node_list[i][0]][temp_node_list[i][1]] = temp_typeflag_win = is_win(temp_chess_list)if flag_win != 0:if flag_win == type:win_list[i] += 1continueelif flag_win == 3:tip_list[i] += 1if temp_node_list[i][0] == 0:del temp_node_list[i]else:temp_node_list[i][0] -= 1while True:if temp_type == 1:temp_type = 2else:temp_type = 1try:temp_index = random.randint(0, len(temp_node_list)-1)except:breaktemp_chess_list[temp_node_list[temp_index][0]][temp_node_list[temp_index][1]] = temp_typeflag_win = is_win(temp_chess_list)if flag_win != 0:if flag_win == type:win_list[i] += 1elif flag_win == 3:tip_list[i] += 1breakif temp_node_list[temp_index][0] == 0:del temp_node_list[temp_index]else:temp_node_list[temp_index][0] -= 1end = time.clock()print(end - start)if max(win_list) > range_sum*0.1:print(win_list)check = win_list.index(max(win_list))print(node_list)print(check)else:check = tip_list.index(max(tip_list))print("选择平局")chess_list[node_list[check][0]][node_list[check][1]] = typedef draw_player(player):fontobj = pygame.font.SysFont('SimHei', 25)if player:text = fontobj.render("先手: 电脑    棋色: 黑色", True, color_black, color_white)else:text = fontobj.render("先手: 玩家    棋色: 黑色", True, color_black, color_white)textrect = text.get_rect()textrect.center = (260, 140)displaysurf.blit(text, textrect)def button():pygame.draw.rect(displaysurf, color_green, (730, 200, 160, 80))fontobj1 = pygame.font.SysFont('SimHei', 30)text1 = fontobj1.render("切换先手", True, color_white, color_green)textrect1 = text1.get_rect()textrect1.center = (810, 240)displaysurf.blit(text1, textrect1)pygame.draw.rect(displaysurf, color_green, (730, 360, 160, 80))fontobj2 = pygame.font.SysFont('SimHei', 30)if play_flag == False:text2 = fontobj2.render("开始游戏", True, color_white, color_green)else:text2 = fontobj2.render("暂停游戏", True, color_white, color_green)textrect2 = text2.get_rect()textrect2.center = (810, 400)displaysurf.blit(text2, textrect2)pygame.draw.rect(displaysurf, color_green, (730, 520, 160, 80))fontobj3 = pygame.font.SysFont('SimHei', 30)text3 = fontobj3.render("重置游戏", True, color_white, color_green)textrect3 = text3.get_rect()textrect3.center = (810, 560)displaysurf.blit(text3, textrect3)def play_type():fontobj = pygame.font.SysFont('SimHei', 25)if play_flag == False:win_flag = is_win(chess_list)pygame.draw.rect(displaysurf, color_white, (400, 120, 300, 45))if win_flag == 0:text = fontobj.render("状态: 未开始游戏", True, color_black, color_white)elif win_flag == 1:if player:text = fontobj.render("状态: 电脑胜利", True, color_black, color_white)else:text = fontobj.render("状态: 玩家胜利", True, color_black, color_white)elif win_flag == 2:if player:text = fontobj.render("状态: 玩家胜利", True, color_black, color_white)else:text = fontobj.render("状态: 电脑胜利", True, color_black, color_white)elif win_flag == 3:text = fontobj.render("状态: 平局", True, color_black, color_white)else:null_sum = 0for row in range(chess_grid_row):for col in range(chess_grid_col):if chess_list[row][col] in [0, 3, 4]:null_sum += 1if player:if (chess_grid_row*chess_grid_col-null_sum) % 2 == 0:text = fontobj.render("状态: 请电脑落子", True, color_black, color_white)else:text = fontobj.render("状态: 请玩家落子", True, color_black, color_white)else:if (chess_grid_row*chess_grid_col-null_sum) % 2 == 1:text = fontobj.render("状态: 请电脑落子", True, color_black, color_white)else:text = fontobj.render("状态: 请玩家落子", True, color_black, color_white)textrect = text.get_rect()textrect.center = (570, 140)displaysurf.blit(text, textrect)def new_play():global player, play_flagdraw_chessboard()# AI(chess_list, 1)# main game loopwhile True:for event in pygame.event.get():# close gameif event.type == QUIT:pygame.quit()sys.exit()elif event.type == MOUSEMOTION:mousex, mousey = event.posif play_flag:if mousex > 50 and mousex < 690 and mousey > 170 and mousey < 730:if player:draw_tip_chess(mousex, mousey, 4)else:draw_tip_chess(mousex, mousey, 3)else:clear_tip_chess()elif event.type == MOUSEBUTTONUP:mousex, mousey = event.posif play_flag:if mousex > 50 and mousex < 690 and mousey > 170 and mousey < 730:if player:flag = draw_check_chess(mousex, mousey, 2)draw_chess()pygame.display.update()if flag:if is_win(chess_list) != 0:play_flag = Falseplay_type()pygame.display.update()if play_flag:AI(chess_list, 1)if is_win(chess_list) != 0:play_flag = Falseelse:flag = draw_check_chess(mousex, mousey, 1)draw_chess()pygame.display.update()if flag:if is_win(chess_list) != 0:play_flag = Falseplay_type()pygame.display.update()if play_flag:AI(chess_list, 2)if is_win(chess_list) != 0:play_flag = Falseelse:if mousex > 730 and mousex < 890 and mousey > 200 and mousey < 280:if is_win(chess_list) == 0:if player:player = Falseelse:player = Truedraw_player(player)if mousex > 730 and mousex < 890 and mousey > 360 and mousey < 440:if play_flag:play_flag = Falseelse:if is_win(chess_list) == 0:play_flag = Trueplay_type()button()pygame.display.update()if player and sum(chess_list[-1]) == 0:AI(chess_list, 1)elif mousex > 730 and mousex < 890 and mousey > 520 and mousey < 600:play_flag = Falsefor row in range(chess_grid_row):for col in range(chess_grid_col):chess_list[row][col] = 0draw_player(player)button()draw_chess()play_type()pygame.display.update()def main():new_play()main()

 


http://chatgpt.dhexx.cn/article/Yn3S4xwR.shtml

相关文章

C语言简单游戏编程入门之四子棋

运行截图 源代码 #include<stdio.h> #include<stdlib.h> /*四子棋1.在一个6行7列的棋盘中2.玩家1和玩家2依次交替下子3.每次下子落于棋盘可能达到的最下方4.先四子相连为胜5.棋盘已满且无四子相连为平局 *//*定义常量*/ //行数 #define ROW 7 //列数 #define C…

基于Python实现四子棋游戏

1. 实验介绍 1.1 实验内容 实验利用 Python 模拟 AI 和玩家进行四子棋游戏&#xff0c;利用游戏实验 Pygame 库&#xff0c;为游戏提供界面和操作支持。AI 算法借用蒙特卡洛搜索树思想。通过设置 AI 的难度系数&#xff0c;即 AI 所能考虑到的未来棋子的可能走向&#xff0c;…

运用Python——劳拉下棋_四连环游戏_重力四子棋游戏(代码与游戏判定)

导航 运用Python——爬虫_网易云音乐热评 运用Python——游戏_四子棋_劳拉下棋 文章目录 导航1.游戏规则2.玩法分析3.部分详解第一部分&#xff1a;初始化第二部分&#xff1a;棋盘显示第三部分&#xff1a;玩家输入第四部分&#xff1a;运行第五部分&#xff1a;胜利判断 4.完…

ISME | 热液微生物群落揭示了喷口区的生物地理学和嗜热性的进化历史

Auka热液喷口区的微生物群落揭示了喷口区的生物地理学和嗜热性的进化历史 Microbial community of recently discovered Auka vent field sheds light on vent biogeography and evolutionary history of thermophily Article The ISME Journal, [IF 10.3] DOI&#xff1a;10.…

S32K1xx 固件更新

1 介绍 随着当前技术的进步&#xff0c;车辆变得更加电子化而不是机械化。车辆中的电子创新不断增加。因此&#xff0c;车辆中的软件也在增加&#xff0c;因此存在潜在错误的风险。 每次发现软件错误时&#xff0c;都需要进行召回过程来更新软件。这些召回代表了汽车制造商的…

萤火虫算法综述

1.萤火虫算法概述 萤火虫闪烁的光芒在热带和温带地区的夏季天空中是一道令人惊叹的风景。大约有两千种萤火虫&#xff0c;大多数萤火虫会发出短暂而有节奏的闪光。闪光的模式对于特定物种来说往往是独一无二的。闪光是由生物发光过程产生的&#xff0c;这种信号系统的真正功能仍…

《闪》(霹雳战狗 bolt)电影感悟

when you stuck your head, relax, turn and pull. 有时候做人做事要转换一下方式&#xff0c;否则还是会卡住头。Because all around the planet, there are animals who feel like they cannot, like a little hamster, who once spends his day in the vehicle park dreamin…

大学英语四新视野 课后习题+答案翻译 Unit1~Unit8

Unit 1 Text A: Words in use 2022年6月16日 20:57 1 As the gender barriers crumbled, the number of women working as lawyers, doctors, or bankers began to increase significantly from the mid-20th century. 随着性别障碍的消除&#xff0c;从20世纪中期开始&am…

拉勾网爬虫

源代码&#xff1a;https://github.com/carlblocking/xxw-for-public/tree/master/LaGouSpider 前几天写了一个知乎网的爬虫并爬取了一些数据&#xff0c;然而新鲜感消失的很快。于是&#xff0c;大概2天前开始试着爬取拉勾网上的数据。 在解析数据的过程中&#xff0c;知乎爬…

爬虫实战(三) 用Python爬取拉勾网

0、前言 最近博主面临着选方向的困难&#xff08;唉&#xff0c;选择困难症患者 &#xff1e;﹏&#xff1c;&#xff09;&#xff0c;所以希望了解一下目前不同岗位的就业前景 这时&#xff0c;就不妨写个小爬虫&#xff0c;爬取一下 拉勾网 的职位数据&#xff0c;并用图形…

python爬取拉勾网公司信息(您操作太频繁,请稍后访问)

最近爬取拉勾网上的公司信息碰到了很多问题&#xff0c;但是都一一解决了。作为一个招聘网站&#xff0c;他的反爬措施做的还是很好的。 为了调查招聘网站上有多少公司&#xff0c;以及公司信息&#xff0c;并把公司信息存入MongoDB数据库中。 先上运行成功代码&#xff1a; …

2022年爬取拉勾网详情页面地址信息

众所周知&#xff0c;拉勾网的反爬机制一直做的很好&#xff0c;前些年还可以通过找到网页源代码找到岗位详情页面的地址&#xff0c;而现在拉勾网的详情页面地址直接没了&#xff0c;这就很奇怪&#xff0c;那么鼠标点击他又是如何跳转到别的页面 点开源代码中&#xff0c;每一…

django+拉勾网招聘数据可视化

django拉勾网招聘数据可视化&#xff0c;此系统有详细的录屏&#xff0c;下面只是部门截图&#xff0c;需要看完整录屏联系博主 系统开发语言python&#xff0c;框架为django&#xff0c;数据库mysql&#xff0c;分为爬虫和可视化分析

Python爬虫实战:爬取拉勾网并对其进行数据分析

###整体的 思路 是采用 selenium 模拟浏览器的行为&#xff0c;具体的步骤如下&#xff1a; 初始化爬取数据&#xff0c;这里分为两个部分&#xff1a;一是爬取网页数据&#xff0c;二是进行翻页操作保存数据&#xff0c;将数据保存到文件中数据可视化 ###整体的 代码结构 如…

什么你还不知道招聘信息,小唐来教你——最新2021爬取拉勾网招聘信息(二)

文章目录 前言一、准备我们的库二、数据清洗三、核密度图及词云制作四、完整代码五、扩展 上一篇&#xff1a;什么你还不知道招聘信息&#xff0c;小唐来教你——最新2021爬取拉勾网招聘信息&#xff08;一&#xff09; 下一篇&#xff1a;没有拉&#xff01; 前言 既然我们上…

No.4 拉勾网职位搜索信息爬取

一、简介 网址&#xff1a;https://www.lagou.com/jobs/list_/p-city_0?&clfalse&fromSearchtrue&labelWords&suginput 效果&#xff1a;信息 使用框架&#xff1a;requests 难度系数&#xff1a;✩✩✩ 二、教程 今天我们要为我们的工作写爬虫&#xf…

什么你还不知道招聘信息,小唐来教你——最新2021爬取拉勾网招聘信息(一)

文章目录 前言一、准备我们的库二、分析分析三、 代码四、数据展示小唐的心路历程 上一篇&#xff1a;没有啦&#xff01; 下一篇&#xff1a;什么你还不知道招聘信息&#xff0c;小唐来教你——最新2021爬取拉勾网招聘信息&#xff08;二&#xff09; 前言 有是小唐的数据分析…

拉勾网的反爬介绍和解决方法(更新时间:2019/2/20)

拉勾网的反爬介绍和解决方法&#xff08;更新时间&#xff1a;2019/2/20&#xff09; 目录直达&#xff1a; 文章目录 拉勾网的反爬介绍和解决方法&#xff08;更新时间&#xff1a;2019/2/20&#xff09;1. 前言2. 简述3. 反爬介绍3.1、对于职位的详情页和公司的详情页的反爬…

应对反爬如何爬取拉勾网信息(非常详细)

目录 前期准备请求头Cookies问题手动提取Cookies和自动Cookies相结合自动提取Cookies实现 手动提取Cookies实现 页面分析代码实现 前期准备 我们知道百度其实就是一个爬虫&#xff0c;但是对方的服务器是没有对百度进行反爬的&#xff0c;所以为了防止对方服务器的反爬&#x…

【Python】模拟登陆并抓取拉勾网信息(selenium+phantomjs)

环境 python3.5pip install seleniumphantomjs-2.1.1pip install pyquery 代码 # -*- coding:utf-8 -*-# 防止print中文出错 import time import sys import io sys.stdout io.TextIOWrapper(sys.stdout.buffer, encodinggb18030)from pyquery import PyQuery as pq from se…