20223328 2022-2023-5 《Python程序设计》实验四报告

LZH18195821397 2023-05-18 19:24:31

课程:《Python程序设计》
班级: 2233
姓名: 刘泽华
学号:20223328
实验教师:王志强
实验日期:2023年5月11日
必修/选修: 公选课

1.实验内容

Python综合应用:编写小游戏:扫雷

2. 实验过程及结果

print("欢迎来到简洁扫雷游戏。")

difficulty = input("请选择难度(1 简单 2 中等 3 困难 4 自定义):")

if difficulty == str(1):

mine = 10

if difficulty == str(2):

mine = 20

if difficulty == str(3):

mine = 30

if difficulty == str(4):

mine = int(input("请输入数量(80以内):"))

import random

t = 0

leis = []

while t < mine:

t += 1

a = random.randint(1,9)

b = random.randint(1,9)

leis.append(a)

leis.append(b)

class block:

def __init__(self,condition,x,y,safety):

self.condition = condition

self.x = x

self.y = y

self.safety = safety

a1 = block("■ ",1,1,"safe")

a2 = block("■ ",2,1,"safe")

a3 = block("■ ",3,1,"safe")

a4 = block("■ ",4,1,"safe")

a5 = block("■ ",5,1,"safe")

a6 = block("■ ",6,1,"safe")

a7 = block("■ ",7,1,"safe")

a8 = block("■ ",8,1,"safe")

a9 = block("■ ",9,1,"safe")

b1 = block("■ ",1,2,"safe")

b2 = block("■ ",2,2,"safe")

b3 = block("■ ",3,2,"safe")

b4 = block("■ ",4,2,"safe")

b5 = block("■ ",5,2,"safe")

b6 = block("■ ",6,2,"safe")

b7 = block("■ ",7,2,"safe")

b8 = block("■ ",8,2,"safe")

b9 = block("■ ",9,2,"safe")

c1 = block("■ ",1,3,"safe")

c2 = block("■ ",2,3,"safe")

c3 = block("■ ",3,3,"safe")

c4 = block("■ ",4,3,"safe")

c5 = block("■ ",5,3,"safe")

c6 = block("■ ",6,3,"safe")

c7 = block("■ ",7,3,"safe")

c8 = block("■ ",8,3,"safe")

c9 = block("■ ",9,3,"safe")

d1 = block("■ ",1,4,"safe")

d2 = block("■ ",2,4,"safe")

d3 = block("■ ",3,4,"safe")

d4 = block("■ ",4,4,"safe")

d5 = block("■ ",5,4,"safe")

d6 = block("■ ",6,4,"safe")

d7 = block("■ ",7,4,"safe")

d8 = block("■ ",8,4,"safe")

d9 = block("■ ",9,4,"safe")

e1 = block("■ ",1,5,"safe")

e2 = block("■ ",2,5,"safe")

e3 = block("■ ",3,5,"safe")

e4 = block("■ ",4,5,"safe")

e5 = block("■ ",5,5,"safe")

e6 = block("■ ",6,5,"safe")

e7 = block("■ ",7,5,"safe")

e8 = block("■ ",8,5,"safe")

e9 = block("■ ",9,5,"safe")

f1 = block("■ ",1,6,"safe")

f2 = block("■ ",2,6,"safe")

f3 = block("■ ",3,6,"safe")

f4 = block("■ ",4,6,"safe")

f5 = block("■ ",5,6,"safe")

f6 = block("■ ",6,6,"safe")

f7 = block("■ ",7,6,"safe")

f8 = block("■ ",8,6,"safe")

f9 = block("■ ",9,6,"safe")

g1 = block("■ ",1,7,"safe")

g2 = block("■ ",2,7,"safe")

g3 = block("■ ",3,7,"safe")

g4 = block("■ ",4,7,"safe")

g5 = block("■ ",5,7,"safe")

g6 = block("■ ",6,7,"safe")

g7 = block("■ ",7,7,"safe")

g8 = block("■ ",8,7,"safe")

g9 = block("■ ",9,7,"safe")

h1 = block("■ ",1,8,"safe")

h2 = block("■ ",2,8,"safe")

h3 = block("■ ",3,8,"safe")

h4 = block("■ ",4,8,"safe")

h5 = block("■ ",5,8,"safe")

h6 = block("■ ",6,8,"safe")

h7 = block("■ ",7,8,"safe")

h8 = block("■ ",8,8,"safe")

h9 = block("■ ",9,8,"safe")

i1 = block("■ ",1,9,"safe")

i2 = block("■ ",2,9,"safe")

i3 = block("■ ",3,9,"safe")

i4 = block("■ ",4,9,"safe")

i5 = block("■ ",5,9,"safe")

i6 = block("■ ",6,9,"safe")

i7 = block("■ ",7,9,"safe")

i8 = block("■ ",8,9,"safe")

i9 = block("■ ",9,9,"safe")

i10 = block("■ ",9,9,"safe")

blocks = [0,0,0,0,0,0,0,0,0,0,0,a1,a2,a3,a4,a5,a6,a7,a8,a9,0,b1,b2,b3,b4,b5,b6,b7,b8,b9,0,c1,c2,c3,c4,c5,c6,c7,c8,c9,0,d1,d2,d3,d4,d5,d6,d7,d8,d9,0,e1,e2,e3,e4,e5,e6,e7,e8,e9,0,f1,f2,f3,f4,f5,f6,f7,f8,f9,0,g1,g2,g3,g4,g5,g6,g7,g8,g9,0,h1,h2,h3,h4,h5,h6,h7,h8,h9,0,i1,i2,i3,i4,i5,i6,i7,i8,i9,]

def site(x,y):

if x > 0 and x < 10 and y > 0 and y < 10:

p = 10*y + x

return blocks[p]

else:

return i10

t = 0

a = 0

b = 1

while t < mine:

t += 1

site(int(leis[a]),int(leis[b])).safety = "boom"

a += 2

b += 2

def printblocks():

print(" 1 2 3 4 5 6 7 8 9")

print("1 "+a1.condition+a2.condition+a3.condition+a4.condition+a5.condition+a6.condition+a7.condition+a8.condition+a9.condition+"1")

print('2 '+b1.condition+b2.condition+b3.condition+b4.condition+b5.condition+b6.condition+b7.condition+b8.condition+b9.condition+"2")

print('3 '+c1.condition+c2.condition+c3.condition+c4.condition+c5.condition+c6.condition+c7.condition+c8.condition+c9.condition+"3")

print('4 '+d1.condition+d2.condition+d3.condition+d4.condition+d5.condition+d6.condition+d7.condition+d8.condition+d9.condition+"4")

print('5 '+e1.condition+e2.condition+e3.condition+e4.condition+e5.condition+e6.condition+e7.condition+e8.condition+e9.condition+"5")

print('6 '+f1.condition+f2.condition+f3.condition+f4.condition+f5.condition+f6.condition+f7.condition+f8.condition+f9.condition+"6")

print('7 '+g1.condition+g2.condition+g3.condition+g4.condition+g5.condition+g6.condition+g7.condition+g8.condition+g9.condition+"7")

print('8 '+h1.condition+h2.condition+h3.condition+h4.condition+h5.condition+h6.condition+h7.condition+h8.condition+h9.condition+"8")

print('9 '+i1.condition+i2.condition+i3.condition+i4.condition+i5.condition+i6.condition+i7.condition+i8.condition+i9.condition+"9")

print(" 1 2 3 4 5 6 7 8 9")

printblocks()

#计时

import time

start = time.time()

#操作

def detect_win_or_not():

l = 0

for i in blocks:

if type(i) != type(1):

if i.condition == "■ ":

l += 1

return l

while detect_win_or_not() > mine:

op1 = input("请输入横坐标")

op2 = input("请输入纵坐标")

if site(int(op1),int(op2)).safety == "boom":

print("挖到地雷,游戏失败")

t = 0

a = -2

b = -1

while t < mine:

t += 1

a += 2

b += 2

site(int(leis[a]), int(leis[b])).condition = "◎ "

printblocks()

exit()

#检测

def detect(a,b):

num = 0

if site(a-1,b-1).safety == "boom":

num += 1

if site(a,b-1).safety == "boom":

num += 1

if site(a+1,b-1).safety == "boom":

num += 1

if site(a-1,b).safety == "boom":

num += 1

if site(a+1,b).safety == "boom":

num += 1

if site(a-1,b+1).safety == "boom":

num += 1

if site(a,b+1).safety == "boom":

num += 1

if site(a+1,b+1).safety == "boom":

num += 1

return num

#调色板

numbers = ["0","\033[0;34m1\033[0m","\033[0;32m2\033[0m","\033[0;31m3\033[0m","\033[0;35m4\033[0m","\033[0;36m5\033[0m","\033[0;33m6\033[0m","\033[0;30m7\033[0m","\033[0;37m8\033[0m"]

# 自动机制

def auto_ppppppprepare(x, y):

if site(x, y).condition == "0" + " " and x > 0 and x < 10 and y > 0 and y < 10:

site(x - 1, y - 1).condition = numbers[detect(x - 1, y - 1)] + " "

site(x, y - 1).condition = numbers[detect(x, y - 1)] + " "

site(x + 1, y - 1).condition = numbers[detect(x + 1, y - 1)] + " "

site(x - 1, y).condition = numbers[detect(x - 1, y)] + " "

site(x + 1, y).condition = numbers[detect(x + 1, y)] + " "

site(x - 1, y + 1).condition = numbers[detect(x - 1, y + 1)] + " "

site(x, y + 1).condition = numbers[detect(x, y + 1)] + " "

site(x + 1, y + 1).condition = numbers[detect(x + 1, y + 1)] + " "

def auto_pppppprepare(x, y):

if site(x, y).condition == "0" + " " and x > 0 and x < 10 and y > 0 and y < 10:

site(x - 1, y - 1).condition = numbers[detect(x - 1, y - 1)] + " "

site(x, y - 1).condition = numbers[detect(x, y - 1)] + " "

site(x + 1, y - 1).condition = numbers[detect(x + 1, y - 1)] + " "

site(x - 1, y).condition = numbers[detect(x - 1, y)] + " "

site(x + 1, y).condition = numbers[detect(x + 1, y)] + " "

site(x - 1, y + 1).condition = numbers[detect(x - 1, y + 1)] + " "

site(x, y + 1).condition = numbers[detect(x, y + 1)] + " "

site(x + 1, y + 1).condition = numbers[detect(x + 1, y + 1)] + " "

auto_ppppppprepare(x - 1, y - 1)

auto_ppppppprepare(x, y - 1)

auto_ppppppprepare(x + 1, y - 1)

auto_ppppppprepare(x - 1, y)

auto_ppppppprepare(x + 1, y)

auto_ppppppprepare(x - 1, y + 1)

auto_ppppppprepare(x, y + 1)

auto_ppppppprepare(x + 1, y + 1)

def auto_ppppprepare(x, y):

if site(x, y).condition == "0" + " " and x > 0 and x < 10 and y > 0 and y < 10:

site(x - 1, y - 1).condition = numbers[detect(x - 1, y - 1)] + " "

site(x, y - 1).condition = numbers[detect(x, y - 1)] + " "

site(x + 1, y - 1).condition = numbers[detect(x + 1, y - 1)] + " "

site(x - 1, y).condition = numbers[detect(x - 1, y)] + " "

site(x + 1, y).condition = numbers[detect(x + 1, y)] + " "

site(x - 1, y + 1).condition = numbers[detect(x - 1, y + 1)] + " "

site(x, y + 1).condition = numbers[detect(x, y + 1)] + " "

site(x + 1, y + 1).condition = numbers[detect(x + 1, y + 1)] + " "

auto_pppppprepare(x - 1, y - 1)

auto_pppppprepare(x, y - 1)

auto_pppppprepare(x + 1, y - 1)

auto_pppppprepare(x - 1, y)

auto_pppppprepare(x + 1, y)

auto_pppppprepare(x - 1, y + 1)

auto_pppppprepare(x, y + 1)

auto_pppppprepare(x + 1, y + 1)

def auto_pppprepare(x, y):

if site(x, y).condition == "0" + " " and x > 0 and x < 10 and y > 0 and y < 10:

site(x - 1, y - 1).condition = numbers[detect(x - 1, y - 1)] + " "

site(x, y - 1).condition = numbers[detect(x, y - 1)] + " "

site(x + 1, y - 1).condition = numbers[detect(x + 1, y - 1)] + " "

site(x - 1, y).condition = numbers[detect(x - 1, y)] + " "

site(x + 1, y).condition = numbers[detect(x + 1, y)] + " "

site(x - 1, y + 1).condition = numbers[detect(x - 1, y + 1)] + " "

site(x, y + 1).condition = numbers[detect(x, y + 1)] + " "

site(x + 1, y + 1).condition = numbers[detect(x + 1, y + 1)] + " "

auto_ppppprepare(x - 1, y - 1)

auto_ppppprepare(x, y - 1)

auto_ppppprepare(x + 1, y - 1)

auto_ppppprepare(x - 1, y)

auto_ppppprepare(x + 1, y)

auto_ppppprepare(x - 1, y + 1)

auto_ppppprepare(x, y + 1)

auto_ppppprepare(x + 1, y + 1)

def auto_ppprepare(x, y):

if site(x, y).condition == "0" + " " and x > 0 and x < 10 and y > 0 and y < 10:

site(x - 1, y - 1).condition = numbers[detect(x - 1, y - 1)] + " "

site(x, y - 1).condition = numbers[detect(x, y - 1)] + " "

site(x + 1, y - 1).condition = numbers[detect(x + 1, y - 1)] + " "

site(x - 1, y).condition = numbers[detect(x - 1, y)] + " "

site(x + 1, y).condition = numbers[detect(x + 1, y)] + " "

site(x - 1, y + 1).condition = numbers[detect(x - 1, y + 1)] + " "

site(x, y + 1).condition = numbers[detect(x, y + 1)] + " "

site(x + 1, y + 1).condition = numbers[detect(x + 1, y + 1)] + " "

auto_pppprepare(x - 1, y - 1)

auto_pppprepare(x, y - 1)

auto_pppprepare(x + 1, y - 1)

auto_pppprepare(x - 1, y)

auto_pppprepare(x + 1, y)

auto_pppprepare(x - 1, y + 1)

auto_pppprepare(x, y + 1)

auto_pppprepare(x + 1, y + 1)

def auto_pprepare(x, y):

if site(x, y).condition == "0" + " " and x > 0 and x < 10 and y > 0 and y < 10:

site(x - 1, y - 1).condition = numbers[detect(x - 1, y - 1)] + " "

site(x, y - 1).condition = numbers[detect(x, y - 1)] + " "

site(x + 1, y - 1).condition = numbers[detect(x + 1, y - 1)] + " "

site(x - 1, y).condition = numbers[detect(x - 1, y)] + " "

site(x + 1, y).condition = numbers[detect(x + 1, y)] + " "

site(x - 1, y + 1).condition = numbers[detect(x - 1, y + 1)] + " "

site(x, y + 1).condition = numbers[detect(x, y + 1)] + " "

site(x + 1, y + 1).condition = numbers[detect(x + 1, y + 1)] + " "

auto_ppprepare(x - 1, y - 1)

auto_ppprepare(x, y - 1)

auto_ppprepare(x + 1, y - 1)

auto_ppprepare(x - 1, y)

auto_ppprepare(x + 1, y)

auto_ppprepare(x - 1, y + 1)

auto_ppprepare(x, y + 1)

auto_ppprepare(x + 1, y + 1)

def auto_prepare(x, y):

if site(x, y).condition == "0" + " " and x > 0 and x < 10 and y > 0 and y < 10:

site(x - 1, y - 1).condition = numbers[detect(x - 1, y - 1)] + " "

site(x, y - 1).condition = numbers[detect(x, y - 1)] + " "

site(x + 1, y - 1).condition = numbers[detect(x + 1, y - 1)] + " "

site(x - 1, y).condition = numbers[detect(x - 1, y)] + " "

site(x + 1, y).condition = numbers[detect(x + 1, y)] + " "

site(x - 1, y + 1).condition = numbers[detect(x - 1, y + 1)] + " "

site(x, y + 1).condition = numbers[detect(x, y + 1)] + " "

site(x + 1, y + 1).condition = numbers[detect(x + 1, y + 1)] + " "

auto_pprepare(x - 1, y - 1)

auto_pprepare(x, y - 1)

auto_pprepare(x + 1, y - 1)

auto_pprepare(x - 1, y)

auto_pprepare(x + 1, y)

auto_pprepare(x - 1, y + 1)

auto_pprepare(x, y + 1)

auto_pprepare(x + 1, y + 1)

def auto(x, y):

if site(x, y).condition == "0" + " " and x > 0 and x < 10 and y > 0 and y < 10:

site(x - 1, y - 1).condition = numbers[detect(x - 1, y - 1)] + " "

site(x, y - 1).condition = numbers[detect(x, y - 1)] + " "

site(x + 1, y - 1).condition = numbers[detect(x + 1, y - 1)] + " "

site(x - 1, y).condition = numbers[detect(x - 1, y)] + " "

site(x + 1, y).condition = numbers[detect(x + 1, y)] + " "

site(x - 1, y + 1).condition = numbers[detect(x - 1, y + 1)] + " "

site(x, y + 1).condition = numbers[detect(x, y + 1)] + " "

site(x + 1, y + 1).condition = numbers[detect(x + 1, y + 1)] + " "

auto_prepare(x - 1, y - 1)

auto_prepare(x, y - 1)

auto_prepare(x + 1, y - 1)

auto_prepare(x - 1, y)

auto_prepare(x + 1, y)

auto_prepare(x - 1, y + 1)

auto_prepare(x, y + 1)

auto_prepare(x + 1, y + 1)

#展示结果

site(int(op1), int(op2)).condition =numbers[detect(int(op1),int(op2))] + " "

auto(int(op1), int(op2))

printblocks()

end = time.time()

cost = end - start

print("游戏胜利!你的用时为" + str(int(round(cost,0))) + "秒")

#排行榜

if difficulty == str(1):

grades = []

grades.append((str(int(round(cost,0)))))

file_handle = open('eazy.txt',mode='a')

for num in grades:

file_handle.write("," + str(num))

file_handle.close()

file_handle = open('eazy.txt',mode='r')

contents = file_handle.readlines()

import re

print("简单模式最小用时为:" + str(min(map(int,re.findall("\d+",str(contents))))) + "秒")

if difficulty == str(2):

grades = []

grades.append(str(int(round(cost,0))))

file_handle = open('medium.txt',mode='a')

for num in grades:

file_handle.write("," + str(num))

file_handle.close()

file_handle = open('medium.txt',mode='r')

contents = file_handle.readlines()

import re

print("中等模式最小用时为:" + str(min(map(int, re.findall("\d+", str(contents)))))+ "秒")

if difficulty == str(3):

grades = []

grades.append(str(int(round(cost,0))))

file_handle = open('hard.txt',mode='a')

for num in grades:

file_handle.write("," + str(num))

file_handle.close()

file_handle = open('hard.txt',mode='r')

contents = file_handle.readlines()

import re

print("困难模式最小用时为:" + str(min(map(int, re.findall("\d+", str(contents)))))+ "秒")

上传Gitee

 实验遇到的问题

扫雷是一个十分经典的游戏,它的原理和运行方式也是比较容易理解,但是用python设计一个游戏对我来说有一些困难,在网上搜索并参考了相关资料,自己尽力理解和学习了代码的实现。自己独立编写程序的能力还是不足,希望以后能继续深入学习python知识,争取独立完成。参考资料:(8条消息) 在python中实现扫雷_python扫雷_Yune884的博客-CSDN博客

课程总结感想体会、意见和建议

在本学期的Python课程,我学习了以下内容:

1.Python概述;
2.Python语法:注释,代码缩进,保留字与标识符,字符串类型,布尔类型,数据类型转换,运算符;
3.流程控制语句:if语句,条件表达式,while循环,for循环,循环嵌套,break语句,continue语句等;
4.序列:列表,元组,字典,集合;
5.字符串与正则表达式;
6.面向对象程序设计:面向对象三要素:封装、继承、多态;
7.模块与异常处理;
8.异常处理及程序调试;
9.文件及目录操作:冯诺依曼模型,文件的基本操作:打开、读写、关闭;
10.socket编程技术:Socket=IP+端口;
11.数据库;
12.GUI界面编程;
13.Pygame游戏编程;
14.Python网络爬虫技术:快速开发、跨平台、解释性、多种爬虫框架;
15.web编程;

感想体会

Python是一门公选课,但是这门学科十分有必要我们认真学习,课程上老师用通俗易懂的方式给我们介绍了python的基础知识,并实践操作,我收获了很多知识,希望以后能继续接触python,把它学好。王志强老师上课风趣幽默,讲课细致,认真解答同学疑问,对同学们也十分理解尊重,是一位特别好的老师。人生苦短,我用python。

...全文
45 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

144

社区成员

发帖
与我相关
我的任务
社区描述
开展Python教学和技术交流
python 高校 北京·丰台区
社区管理员
  • blackwall0321
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧