20212305杨贯宇《Python程序设计》实验四报告

20212305《Python程序设计》实验四报告

课程:《Python程序设计》
班级: 2123
姓名: 杨贯宇
学号:20212305
实验教师:王志强
实验日期:2020年5月30日
必修/选修: 公选课


1.实验内容

(一)Python综合应用:爬虫、数据处理、可视化、机器学习、神经网络、游戏、网络安全等。

(二)实验要求:

(1)程序能运行,功能丰富。(需求提交源代码,并建议录制程序运行的视频)10分
(2)综合实践报告,要体现实验分析、设计、实现过程、结果等信息,格式规范,逻辑清晰,结构合理。10分。
(3)在实践报告中,需要对全课进行总结,并写课程感想体会、意见和建议等。5分
(4)如果没有使用华为云服务(ECS或者MindSpore均可),本次实践扣10分。


2. 实验过程及结果(井字棋)

 

 

 

 

 

 

 

 

代码如下:

  1 from tkinter import *
  2 import tkinter.messagebox as msg
  3 
  4 root = Tk()
  5 root.title('TIC-TAC-TOE---Project Gurukul')
  6 # labels
  7 Label(root, text="player1 : X", font="times 15").grid(row=0, column=1)
  8 Label(root, text="player2 : O", font="times 15").grid(row=0, column=2)
  9 
 10 digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]
 11 
 12 # for player1 sign = X and for player2 sign= Y
 13 mark = ''
 14 
 15 # counting the no. of click
 16 count = 0
 17 
 18 panels = ["panel"] * 10
 19 
 20 
 21 def win(panels, sign):
 22     return ((panels[1] == panels[2] == panels[3] == sign)
 23             or (panels[1] == panels[4] == panels[7] == sign)
 24             or (panels[1] == panels[5] == panels[9] == sign)
 25             or (panels[2] == panels[5] == panels[8] == sign)
 26             or (panels[3] == panels[6] == panels[9] == sign)
 27             or (panels[3] == panels[5] == panels[7] == sign)
 28             or (panels[4] == panels[5] == panels[6] == sign)
 29             or (panels[7] == panels[8] == panels[9] == sign))
 30 
 31 
 32 def checker(digit):
 33     global count, mark, digits
 34 
 35     # Check which button clicked
 36 
 37     if digit == 1 and digit in digits:
 38         digits.remove(digit)
 39         ##player1 will play if the value of count is even and for odd player2 will play
 40         if count % 2 == 0:
 41             mark = 'X'
 42             panels[digit] = mark
 43         elif count % 2 != 0:
 44             mark = 'O'
 45             panels[digit] = mark
 46 
 47         button1.config(text=mark)
 48         count = count + 1
 49         sign = mark
 50 
 51         if (win(panels, sign) and sign == 'X'):
 52             msg.showinfo("Result", "Player1 wins")
 53             root.destroy()
 54         elif (win(panels, sign) and sign == 'O'):
 55             msg.showinfo("Result", "Player2 wins")
 56             root.destroy()
 57 
 58     if digit == 2 and digit in digits:
 59         digits.remove(digit)
 60 
 61         if count % 2 == 0:
 62             mark = 'X'
 63             panels[digit] = mark
 64         elif count % 2 != 0:
 65             mark = 'O'
 66             panels[digit] = mark
 67 
 68         button2.config(text=mark)
 69         count = count + 1
 70         sign = mark
 71 
 72         if (win(panels, sign) and sign == 'X'):
 73             msg.showinfo("Result", "Player1 wins")
 74             root.destroy()
 75         elif (win(panels, sign) and sign == 'O'):
 76             msg.showinfo("Result", "Player2 wins")
 77             root.destroy()
 78 
 79     if digit == 3 and digit in digits:
 80         digits.remove(digit)
 81 
 82         if count % 2 == 0:
 83             mark = 'X'
 84             panels[digit] = mark
 85         elif count % 2 != 0:
 86             mark = 'O'
 87             panels[digit] = mark
 88 
 89         button3.config(text=mark)
 90         count = count + 1
 91         sign = mark
 92 
 93         if (win(panels, sign) and sign == 'X'):
 94             msg.showinfo("Result", "Player1 wins")
 95             root.destroy()
 96         elif (win(panels, sign) and sign == 'O'):
 97             msg.showinfo("Result", "Player2 wins")
 98             root.destroy()
 99 
100     if digit == 4 and digit in digits:
101         digits.remove(digit)
102 
103         if count % 2 == 0:
104             mark = 'X'
105             panels[digit] = mark
106         elif count % 2 != 0:
107             mark = 'O'
108             panels[digit] = mark
109 
110         button4.config(text=mark)
111         count = count + 1
112         sign = mark
113 
114         if (win(panels, sign) and sign == 'X'):
115             msg.showinfo("Result", "Player1 wins")
116             root.destroy()
117         elif (win(panels, sign) and sign == 'O'):
118             msg.showinfo("Result", "Player2 wins")
119             root.destroy()
120 
121     if digit == 5 and digit in digits:
122         digits.remove(digit)
123 
124         if count % 2 == 0:
125             mark = 'X'
126             panels[digit] = mark
127         elif count % 2 != 0:
128             mark = 'O'
129             panels[digit] = mark
130 
131         button5.config(text=mark)
132         count = count + 1
133         sign = mark
134 
135         if (win(panels, sign) and sign == 'X'):
136             msg.showinfo("Result", "Player1 wins")
137             root.destroy()
138         elif (win(panels, sign) and sign == 'O'):
139             msg.showinfo("Result", "Player2 wins")
140             root.destroy()
141 
142     if digit == 6 and digit in digits:
143         digits.remove(digit)
144 
145         if count % 2 == 0:
146             mark = 'X'
147             panels[digit] = mark
148         elif count % 2 != 0:
149             mark = 'O'
150             panels[digit] = mark
151 
152         button6.config(text=mark)
153         count = count + 1
154         sign = mark
155 
156         if (win(panels, sign) and sign == 'X'):
157             msg.showinfo("Result", "Player1 wins")
158             root.destroy()
159         elif (win(panels, sign) and sign == 'O'):
160             msg.showinfo("Result", "Player2 wins")
161             root.destroy()
162 
163     if digit == 7 and digit in digits:
164         digits.remove(digit)
165 
166         if count % 2 == 0:
167             mark = 'X'
168             panels[digit] = mark
169         elif count % 2 != 0:
170             mark = 'O'
171             panels[digit] = mark
172 
173         button7.config(text=mark)
174         count = count + 1
175         sign = mark
176 
177         if (win(panels, sign) and sign == 'X'):
178             msg.showinfo("Result", "Player1 wins")
179             root.destroy()
180         elif (win(panels, sign) and sign == 'O'):
181             msg.showinfo("Result", "Player2 wins")
182             root.destroy()
183 
184     if digit == 8 and digit in digits:
185         digits.remove(digit)
186 
187         if count % 2 == 0:
188             mark = 'X'
189             panels[digit] = mark
190         elif count % 2 != 0:
191             mark = 'O'
192             panels[digit] = mark
193 
194         button8.config(text=mark)
195         count = count + 1
196         sign = mark
197 
198         if (win(panels, sign) and sign == 'X'):
199             msg.showinfo("Result", "Player1 wins")
200             root.destroy()
201         elif (win(panels, sign) and sign == 'O'):
202             msg.showinfo("Result", "Player2 wins")
203             root.destroy()
204 
205     if digit == 9 and digit in digits:
206         digits.remove(digit)
207 
208         if count % 2 == 0:
209             mark = 'X'
210             panels[digit] = mark
211         elif count % 2 != 0:
212             mark = 'O'
213             panels[digit] = mark
214 
215         button9.config(text=mark)
216         count = count + 1
217         sign = mark
218 
219         if (win(panels, sign) and sign == 'X'):
220             msg.showinfo("Result", "Player1 wins")
221             root.destroy()
222         elif (win(panels, sign) and sign == 'O'):
223             msg.showinfo("Result", "Player2 wins")
224             root.destroy()
225 
226     ###if count is greater then 8 then the match has been tied
227     if (count > 8 and win(panels, 'X') == False and win(panels, 'O') == False):
228         msg.showinfo("Result", "Match Tied")
229         root.destroy()
230 
231 
232 ####define buttons
233 button1 = Button(root, width=15, font=('Times 16 bold'), height=7, command=lambda: checker(1))
234 button1.grid(row=1, column=1)
235 button2 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(2))
236 button2.grid(row=1, column=2)
237 button3 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(3))
238 button3.grid(row=1, column=3)
239 button4 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(4))
240 button4.grid(row=2, column=1)
241 button5 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(5))
242 button5.grid(row=2, column=2)
243 button6 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(6))
244 button6.grid(row=2, column=3)
245 button7 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(7))
246 button7.grid(row=3, column=1)
247 button8 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(8))
248 button8.grid(row=3, column=2)
249 button9 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(9))
250 button9.grid(row=3, column=3)
251 
252 root.mainloop()

 

 

 

华为云主机运行:


putty登录及勾选Enable X11 forwarding

 

 

 


将文件上传至云主机

 

 

 

 

输入执行代码的命令

 

 

 

 

配置好的xming

 一路next直到最后。

 

 

3. 实验过程中遇到的问题和解决过程
    其他(感悟、思考等)
最开始想做的不是这个井字棋,而是一个吃金币的小游戏,算是对童年的一种回忆和缅怀。但是查遍网络,也始终不能成功配置cfg模块和各种素材库,所以只得放弃,选择了相对简单一点的井字棋。二百多行代码不是我一个人的成果,这其中有着同学们的集思广益,花费了很长时间。我从来没有想到,半年前还是代码小白的我如今竟然能写出一个游戏!到现在我还有些窃喜、不可思议,当初在我眼中无比神秘的“码农”、“写游戏的大佬”这些称号有朝一日能让我感觉不在陌生。这一个学期的python课程,教会了我魔法一半的技能,让我对代码产生了无比浓厚的兴趣。感谢python,感谢强哥。我相信,我会在敲代码的路上越走越远,终有一日,我能成为我眼中的大佬!

posted @ 2022-05-31 23:02  20212305杨贯宇  阅读(36)  评论(0编辑  收藏  举报