• 浔川AI五子棋v4.0——浔川总社部


    浔川官方在浔川AI五子棋v1.4的基础上,改变了画布、选项等,为广大五子棋爱好者提供最方便的游戏体验。

    正式代码:

    1. # -*- coding: utf-8 -*-
    2. import tkinter as tk
    3. import tkinter.messagebox
    4. import pickle
    5. import random
    6. # 窗口
    7. window = tk.Tk()
    8. window.title('AI五子棋登录界面')
    9. window.geometry('450x200')
    10. # 画布放置图片
    11. # canvas=tk.Canvas(window,height=300,width=500)
    12. # imagefile=tk.PhotoImage(file='qm.png')
    13. # image=canvas.create_image(0,0,anchor='nw',image=imagefile)
    14. # canvas.pack(side='top')
    15. # 标签 用户名密码
    16. Verification_Code = random.randint(1000, 9999)#设置一个随机的四位数
    17. Verification_Code = str(Verification_Code)#把类型转换为str型
    18. print(type(Verification_Code))
    19. tk.Label(window, text='用户名:').place(x=100, y=30)
    20. tk.Label(window, text='密码:').place(x=100, y=70)
    21. tk.Label(window, text='验证码').place(x=100, y=110)
    22. tk.Label(window, text=Verification_Code).place(x=320, y=110)
    23. # 用户名输入框
    24. var_usr_name = tk.StringVar()
    25. entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
    26. entry_usr_name.place(x=160, y=30)
    27. # 密码输入框
    28. var_usr_pwd = tk.StringVar()
    29. entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
    30. entry_usr_pwd.place(x=160, y=70)
    31. #验证码输入框
    32. var_usr_yzm = tk.StringVar()
    33. entry_usr_yzm = tk.Entry(window, textvariable=var_usr_yzm)
    34. entry_usr_yzm.place(x=160, y=110)
    35. # 登录函数
    36. def usr_log_in():
    37. # 输入框获取用户名密码
    38. usr_name = var_usr_name.get()
    39. usr_pwd = var_usr_pwd.get()
    40. usr_yzm = var_usr_yzm.get()
    41. #测试类型
    42. print(type(usr_yzm),type(Verification_Code))
    43. # 从本地字典获取用户信息,如果没有则新建本地数据库
    44. try:
    45. with open('usr_info.pickle', 'rb') as usr_file:
    46. usrs_info = pickle.load(usr_file)
    47. except FileNotFoundError:
    48. with open('usr_info.pickle', 'wb') as usr_file:
    49. usrs_info = {'admin': 'admin'}
    50. pickle.dump(usrs_info, usr_file)
    51. # 判断验证码是否正确用户名和密码是否匹配
    52. if usr_yzm == Verification_Code:
    53. if usr_name in usrs_info:
    54. if usr_pwd == usrs_info[usr_name]:
    55. tk.messagebox.showinfo(title='welcome',
    56. message='欢迎您:' + usr_name)
    57. else:
    58. tk.messagebox.showerror(message='密码错误')
    59. # 用户名密码不能为空
    60. elif usr_name == '' or usr_pwd == '':
    61. tk.messagebox.showerror(message='用户名或密码为空')
    62. # 不在数据库中弹出是否注册的框
    63. else:
    64. is_signup = tk.messagebox.askyesno('欢迎', '您还没有注册,是否现在注册')
    65. if is_signup:
    66. usr_sign_up()
    67. elif usr_yzm == '':
    68. tk.messagebox.showerror(message='验证码不能为空')
    69. else:
    70. tk.messagebox.showerror(message='验证码有误!')
    71. # 注册函数
    72. def usr_sign_up():
    73. # 确认注册时的相应函数
    74. def signtowcg():
    75. # 获取输入框内的内容
    76. nn = new_name.get()
    77. np = new_pwd.get()
    78. npf = new_pwd_confirm.get()
    79. # 本地加载已有用户信息,如果没有则已有用户信息为空
    80. try:
    81. with open('usr_info.pickle', 'rb') as usr_file:
    82. exist_usr_info = pickle.load(usr_file)
    83. except FileNotFoundError:
    84. exist_usr_info = {}
    85. # 检查用户名存在、密码为空、密码前后不一致
    86. if nn in exist_usr_info:
    87. tk.messagebox.showerror('错误', '用户名已存在')
    88. elif np == '' or nn == '':
    89. tk.messagebox.showerror('错误', '用户名或密码为空')
    90. elif np != npf:
    91. tk.messagebox.showerror('错误', '密码前后不一致')
    92. # 注册信息没有问题则将用户名密码写入数据库
    93. else:
    94. exist_usr_info[nn] = np
    95. with open('usr_info.pickle', 'wb') as usr_file:
    96. pickle.dump(exist_usr_info, usr_file)
    97. tk.messagebox.showinfo('欢迎', '注册成功')
    98. # 注册成功关闭注册框
    99. window_sign_up.destroy()
    100. # 新建注册界面
    101. window_sign_up = tk.Toplevel(window)
    102. window_sign_up.geometry('350x200')
    103. window_sign_up.title('注册')
    104. # 用户名变量及标签、输入框
    105. new_name = tk.StringVar()
    106. tk.Label(window_sign_up, text='用户名:').place(x=10, y=10)
    107. tk.Entry(window_sign_up, textvariable=new_name).place(x=150, y=10)
    108. # 密码变量及标签、输入框
    109. new_pwd = tk.StringVar()
    110. tk.Label(window_sign_up, text='请输入密码:').place(x=10, y=50)
    111. tk.Entry(window_sign_up, textvariable=new_pwd, show='*').place(x=150, y=50)
    112. # 重复密码变量及标签、输入框
    113. new_pwd_confirm = tk.StringVar()
    114. tk.Label(window_sign_up, text='请再次输入密码:').place(x=10, y=90)
    115. tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*').place(x=150, y=90)
    116. # 确认注册按钮及位置
    117. bt_confirm_sign_up = tk.Button(window_sign_up, text='确认注册',
    118. command=signtowcg)
    119. bt_confirm_sign_up.place(x=150, y=130)
    120. # 退出的函数
    121. def usr_sign_quit():
    122. window.destroy()
    123. # 登录 注册按钮
    124. bt_login = tk.Button(window, text='登录', command=usr_log_in)
    125. bt_login.place(x=140, y=150)
    126. bt_logup = tk.Button(window, text='注册', command=usr_sign_up)
    127. bt_logup.place(x=210, y=150)
    128. bt_logquit = tk.Button(window, text='退出', command=usr_sign_quit)
    129. bt_logquit.place(x=280, y=150)
    130. # 主循环
    131. window.mainloop()
    132. #下载、安装
    133. import tkinter as tk
    134. import time
    135. # 创建主窗口
    136. window = tk.Tk()
    137. window.title('进度条')
    138. window.geometry('630x150')
    139. # 设置下载进度条
    140. tk.Label(window, text='下载进度:', ).place(x=50, y=60)
    141. canvas = tk.Canvas(window, width=465, height=22, bg="white")
    142. canvas.place(x=110, y=60)
    143. # 显示下载进度
    144. def progress():
    145. # 填充进度条
    146. fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")
    147. x = 500 # 未知变量,可更改
    148. n = 465 / x # 465是矩形填充满的次数
    149. for i in range(x):
    150. n = n + 465 / x
    151. canvas.coords(fill_line, (0, 0, n, 60))
    152. window.update()
    153. time.sleep(0.02) # 控制进度条流动的速度
    154. # 清空进度条
    155. fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="white")
    156. x = 500 # 未知变量,可更改
    157. n = 465 / x # 465是矩形填充满的次数
    158. for t in range(x):
    159. n = n + 465 / x
    160. # 以矩形的长度作为变量值更新
    161. canvas.coords(fill_line, (0, 0, n, 60))
    162. window.update()
    163. time.sleep(0) # 时间为0,即飞速清空进度条
    164. btn_download = tk.Button(window, text='开始下载', command=progress)
    165. btn_download.place(x=400, y=105)
    166. window.mainloop()
    167. import tkinter as tk
    168. import time
    169. # 创建主窗口
    170. window = tk.Tk()
    171. window.title('进度条')
    172. window.geometry('630x150')
    173. # 设置下载进度条
    174. tk.Label(window, text='安装进度:', ).place(x=50, y=60)
    175. canvas = tk.Canvas(window, width=465, height=22, bg="white")
    176. canvas.place(x=110, y=60)
    177. # 显示下载进度
    178. def progress():
    179. # 填充进度条
    180. fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")
    181. x = 500 # 未知变量,可更改
    182. n = 465 / x # 465是矩形填充满的次数
    183. for i in range(x):
    184. n = n + 465 / x
    185. canvas.coords(fill_line, (0, 0, n, 60))
    186. window.update()
    187. time.sleep(0.02) # 控制进度条流动的速度
    188. # 清空进度条
    189. fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="white")
    190. x = 500 # 未知变量,可更改
    191. n = 465 / x # 465是矩形填充满的次数
    192. for t in range(x):
    193. n = n + 465 / x
    194. # 以矩形的长度作为变量值更新
    195. canvas.coords(fill_line, (0, 0, n, 60))
    196. window.update()
    197. time.sleep(0) # 时间为0,即飞速清空进度条
    198. btn_download = tk.Button(window, text='开始安装', command=progress)
    199. btn_download.place(x=400, y=105)
    200. window.mainloop()
    201. #五子棋
    202. from tkinter import *
    203. import math
    204. #定义棋盘类
    205. class chessBoard() :
    206. def __init__(self) :
    207. self.window = Tk()
    208. self.window.title("五子棋游戏")
    209. self.window.geometry("660x470")
    210. self.window.resizable(0,0)
    211. self.canvas=Canvas(self.window , bg="#EEE8AC" , width=470, height=470)
    212. self.paint_board()
    213. self.canvas.grid(row = 0 , column = 0)
    214. def paint_board(self) :
    215. for row in range(0,15) :
    216. if row == 0 or row == 14 :
    217. self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 2)
    218. else :
    219. self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 1)
    220. for column in range(0,15) :
    221. if column == 0 or column == 14 :
    222. self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 ,width = 2)
    223. else :
    224. self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 , width = 1)
    225. self.canvas.create_oval(112, 112, 118, 118, fill="black")
    226. self.canvas.create_oval(352, 112, 358, 118, fill="black")
    227. self.canvas.create_oval(112, 352, 118, 358, fill="black")
    228. self.canvas.create_oval(232, 232, 238, 238, fill="black")
    229. self.canvas.create_oval(352, 352, 358, 358, fill="black")
    230. #定义五子棋游戏类
    231. #0为黑子 , 1为白子 , 2为空位
    232. class Gobang() :
    233. #初始化
    234. def __init__(self) :
    235. self.board = chessBoard()
    236. self.game_print = StringVar()
    237. self.game_print.set("")
    238. #16*16的二维列表,保证不会out of index
    239. self.db = [([2] * 16) for i in range(16)]
    240. #悔棋用的顺序列表
    241. self.order = []
    242. #棋子颜色
    243. self.color_count = 0
    244. self.color = 'black'
    245. #清空与赢的初始化,已赢为1,已清空为1
    246. self.flag_win = 1
    247. self.flag_empty = 1
    248. self.options()
    249. #黑白互换
    250. def change_color(self) :
    251. self.color_count = (self.color_count + 1 ) % 2
    252. if self.color_count == 0 :
    253. self.color = "black"
    254. elif self.color_count ==1 :
    255. self.color = "white"
    256. #落子
    257. def chess_moving(self ,event) :
    258. #不点击"开始"与"清空"无法再次开始落子
    259. if self.flag_win ==1 or self.flag_empty ==0 :
    260. return
    261. #坐标转化为下标
    262. x,y = event.x-25 , event.y-25
    263. x = round(x/30)
    264. y = round(y/30)
    265. #点击位置没用落子,且没有在棋盘线外,可以落子
    266. while self.db[y][x] == 2 and self.limit_boarder(y,x):
    267. self.db[y][x] = self.color_count
    268. self.order.append(x+15*y)
    269. self.board.canvas.create_oval(25+30*x-12 , 25+30*y-12 , 25+30*x+12 , 25+30*y+12 , fill = self.color,tags = "chessman")
    270. if self.game_win(y,x,self.color_count) :
    271. print(self.color,"获胜")
    272. self.game_print.set(self.color+"获胜")
    273. else :
    274. self.change_color()
    275. self.game_print.set("请"+self.color+"落子")
    276. #保证棋子落在棋盘上
    277. def limit_boarder(self , y , x) :
    278. if x<0 or x>14 or y<0 or y>14 :
    279. return False
    280. else :
    281. return True
    282. #计算连子的数目,并返回最大连子数目
    283. def chessman_count(self , y , x , color_count ) :
    284. count1,count2,count3,count4 = 1,1,1,1
    285. #横计算
    286. for i in range(-1 , -5 , -1) :
    287. if self.db[y][x+i] == color_count :
    288. count1 += 1
    289. else:
    290. break
    291. for i in range(1 , 5 ,1 ) :
    292. if self.db[y][x+i] == color_count :
    293. count1 += 1
    294. else:
    295. break
    296. #竖计算
    297. for i in range(-1 , -5 , -1) :
    298. if self.db[y+i][x] == color_count :
    299. count2 += 1
    300. else:
    301. break
    302. for i in range(1 , 5 ,1 ) :
    303. if self.db[y+i][x] == color_count :
    304. count2 += 1
    305. else:
    306. break
    307. #/计算
    308. for i in range(-1 , -5 , -1) :
    309. if self.db[y+i][x+i] == color_count :
    310. count3 += 1
    311. else:
    312. break
    313. for i in range(1 , 5 ,1 ) :
    314. if self.db[y+i][x+i] == color_count :
    315. count3 += 1
    316. else:
    317. break
    318. #\计算
    319. for i in range(-1 , -5 , -1) :
    320. if self.db[y+i][x-i] == color_count :
    321. count4 += 1
    322. else:
    323. break
    324. for i in range(1 , 5 ,1 ) :
    325. if self.db[y+i][x-i] == color_count :
    326. count4 += 1
    327. else:
    328. break
    329. return max(count1 , count2 , count3 , count4)
    330. #判断输赢
    331. def game_win(self , y , x , color_count ) :
    332. if self.chessman_count(y,x,color_count) >= 5 :
    333. self.flag_win = 1
    334. self.flag_empty = 0
    335. return True
    336. else :
    337. return False
    338. #悔棋,清空棋盘,再画剩下的n-1个棋子
    339. def withdraw(self ) :
    340. if len(self.order)==0 or self.flag_win == 1:
    341. return
    342. self.board.canvas.delete("chessman")
    343. z = self.order.pop()
    344. x = z%15
    345. y = z//15
    346. self.db[y][x] = 2
    347. self.color_count = 1
    348. for i in self.order :
    349. ix = i%15
    350. iy = i//15
    351. self.change_color()
    352. self.board.canvas.create_oval(25+30*ix-12 , 25+30*iy-12 , 25+30*ix+12 , 25+30*iy+12 , fill = self.color,tags = "chessman")
    353. self.change_color()
    354. self.game_print.set("请"+self.color+"落子")
    355. #清空
    356. def empty_all(self) :
    357. self.board.canvas.delete("chessman")
    358. #还原初始化
    359. self.db = [([2] * 16) for i in range(16)]
    360. self.order = []
    361. self.color_count = 0
    362. self.color = 'black'
    363. self.flag_win = 1
    364. self.flag_empty = 1
    365. self.game_print.set("")
    366. #将self.flag_win置0才能在棋盘上落子
    367. def game_start(self) :
    368. #没有清空棋子不能置0开始
    369. if self.flag_empty == 0:
    370. return
    371. self.flag_win = 0
    372. self.game_print.set("请"+self.color+"落子")
    373. def options(self) :
    374. self.board.canvas.bind("",self.chess_moving)
    375. Label(self.board.window , textvariable = self.game_print , font = ("Arial", 20) ).place(relx = 0, rely = 0 ,x = 495 , y = 200)
    376. Button(self.board.window , text= "开始游戏" ,command = self.game_start,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=15)
    377. Button(self.board.window , text= "我要悔棋" ,command = self.withdraw,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=60)
    378. Button(self.board.window , text= "清空棋局" ,command = self.empty_all,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=105)
    379. Button(self.board.window , text= "结束游戏" ,command = self.board.window.destroy,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=420)
    380. self.board.window.mainloop()
    381. if __name__ == "__main__":
    382. game = Gobang()

    使用步骤:

    1:登录浔川AI五子棋

    2:下载浔川AI五子棋

    3:安装浔川AI五子棋

    4:进入五子棋游戏(双人)

  • 相关阅读:
    low power-upf-vcsnlp(一)
    Java异步判断线程池所有任务是否执行完成的方法
    <MySQL> 如何合理的设计数据库中的表?数据表设计的三种关系
    JAVA面试(三)
    第七章:最新版零基础学习 PYTHON 教程—Python 列表(第七节 -在 Python 中反转列表)
    ubuntu16.04下标定Astra相机
    1312. 序列统计
    salesforce零基础学习(一百三十五)项目中的零碎知识点小总结(七)
    python面向对象1--类、对象、属性、魔法方法
    面试官:什么是Java内存模型?
  • 原文地址:https://blog.csdn.net/2401_83104529/article/details/140361246