20224222 mmz《Python程序设计入门》实验四报告

hgmx 2023-05-23 21:56:27

20224222 《Python程序设计入门》实验四报告
课程:《Python程序设计入门》
班级: 2242
姓名: mmz
学号:20224222
实验教师:wzq
实验日期:2023年5月18日
必修/选修: 专选课

一、实验内容
具体内容:Python综合应用:爬虫、数据处理、可视化、机器学习、神经网络、游戏、网络安全等。课代表和各小组负责人收集作业(源代码、视频、综合实践报告)
注意事项:在Windows/Linux系统上使用VIM、PDB、IDLE、Pycharm等工具编程实现。
评分标准:
(1)程序能运行,功能丰富。(需求提交源代码,并建议录制程序运行的视频)10分
(2)综合实践报告,要体现实验分析、设计、实现过程、结果等信息,格式规范,逻辑清晰,结构合理。20分。
(3)实验4的遇到的问题以及感想体会。5分。
(4)对全课进行总结,并写课程感想体会、意见和建议等。10分。
二、实验分析
最初打算做一个爬虫作业,但无奈于技术力不够,经验不足,只能放弃,转向在pycharm上编写扫雷小游戏。
三、实验设计与过程
(1)简单编写一个扫雷游戏,可以分为简单、中级、困难、自定义四个等级,在自定义模式下可以自己设定地雷个数。
(2)游戏通过输入横纵坐标来进行扫雷,地雷用符号“◎”来表示。
(3)游戏结束后有提示,并且会计算用时
源代码如下:

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)
#展示结果
    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)))))+ "秒")

(4)实验结果视频

img


四、实验过程中遇到的问题及解决方案
1.对if以及while语句的使用嵌套不熟练,代码的格式仍然会输错
解决方案:及时在CSDN上搜集相关资料,利用书本进行查漏补缺,积极询问同学解决
2.对函数的理解不到位,不清楚其作用
解决方案:在B站搜寻相关课程进行了解
3.无法做出有图形界面的扫雷小游戏,后来发现自己未安装pygame插件
4.感想体会:本次实验为大作业,由于自身技术力与经验不足,我无法未完成较难的爬虫,深刻体会到了自己水平有限,也意识到了上课听讲的重要性,自己对函数语句的理解不透彻,对代码输入的掌握度也不高,这给我本次实验带来了巨大的困难。只能在网上一点点搜集资料进行自学,最终还是在互联网的帮助下有惊无险的完成了本次实验。
五、课程总结
作为一名文科生,在学习python之前,我对于编程的了解可以说是一片空白,但在接触了python后,我才发现python的确很有意思,它与我们的日常生活密切相关,小到一款游戏,大到日常办公,都离不开Python。虽然课程本身的难度较高,但是每节课只要能赶上老师的节奏,跟老师一起敲一敲代码,还是可以学到很多东西的,包括一些字符串的使用,if语句的嵌套,这些让我最起码达到了一个入门的水平,而我在课上也跟随老师敲出了像计算器的小程序,一时间还是蛮有成就感的。虽然我们是文科生,但是老师仍然十分关注我们的学习情况,在我的朋友圈亲自评论担心我们学不好完不成,还担心误人子弟,老师如此认真负责的态度真的很难让人不爱,对于王老师只能说唯有感谢。
最后提建议的话就是希望老师在敲代码的时候节奏放慢一点,有的时候有点跟不上。

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

42

社区成员

发帖
与我相关
我的任务
社区描述
技术交流
教育电商 高校
社区管理员
  • blackwall0321
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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