鱼C论坛

 找回密码
 立即注册
查看: 1599|回复: 2

[作品展示] python界面展示八皇后求解过程

[复制链接]
发表于 2020-6-4 12:15:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
还是一样的框架,只是把八皇后的类装进去了
效果:
eighqueen.jpg

  1. import pygame
  2. import sys
  3. import os
  4. import random
  5. import time
  6. from pygame.locals import *
  7. import tkinter as tk
  8. from tkinter import *
  9. import tkinter.messagebox  # 要使用messagebox先要导入模块

  10. class Eight_queen:
  11.     queens = 0
  12.     sts = 0
  13.     pows = None
  14.     def __init__(self,queens):
  15.         Eight_queen.queens = queens
  16.         Eight_queen.pows = [pow(queens,queens-i) for i in range(0,queens+1)]
  17.         Eight_queen.sts = 0

  18.     @staticmethod
  19.     def check(rows):
  20.         for row in range(1,len(rows)):
  21.             for i in range(row):
  22.                 if abs(rows[i] - rows[row]) in (0, row - i):
  23.                     return row
  24.         return 0

  25.     @staticmethod
  26.     def queen_next_rows():
  27.         if Eight_queen.sts >= Eight_queen.pows[0]:
  28.             return None
  29.         n = Eight_queen.queens
  30.         rows = [0]*n
  31.         for x in range(0,n):
  32.             rows[x]=(Eight_queen.sts%Eight_queen.pows[x])//Eight_queen.pows[x+1]
  33.         err_row = Eight_queen.check(rows)
  34.         if err_row == 0:
  35.             Eight_queen.sts += 1
  36.             return [0,rows]
  37.         else:
  38.             Eight_queen.sts += Eight_queen.pows[err_row+1]
  39.             return [1,rows]

  40. class CMG: #画面显示管理
  41.     screen = None
  42.     map = None
  43.     gameAnswer = None
  44.     WHITE = (255, 255, 255)
  45.     GREEN = (0, 255, 0)
  46.     RED = (255, 0, 0)
  47.     BLUE = (0, 0, 255)
  48.     blocksize = 0
  49.     step = 0
  50.     last_answer = None

  51.     def __init__(self,screen):
  52.         #if CMG.screen == None:
  53.         #print("#############OS.PATH=%s"% os.path.dirname(os.path.abspath(__file__)))
  54.         CMG.screen = screen
  55.         CMG.blocksize = 350//Eight_queen.queens
  56.         #如果存在对象成员
  57.         self.init_game_info()

  58.     def init_game_info(self):
  59.         pass

  60.     def printAll(self,res):
  61.         #if CMG.step > len(CMG.gameAnswer):
  62.         #    return
  63.         rtn = res[0]
  64.         rows = res[1]
  65.         if rtn == 0:
  66.             CMG.last_answer = rows[:]
  67.         CMG.screen.fill((0, 0, 0))
  68.         queens = Eight_queen.queens
  69.         for x in range(0,queens+1):
  70.             end_pos = [(x*CMG.blocksize,0),(x*CMG.blocksize,queens*CMG.blocksize)]
  71.             pygame.draw.lines(CMG.screen, CMG.GREEN,0 , end_pos, 2)
  72.             end_pos = [(x*CMG.blocksize+400,0),(x*CMG.blocksize+400,queens*CMG.blocksize)]
  73.             pygame.draw.lines(CMG.screen, CMG.GREEN,0 , end_pos, 2)
  74.         for y in range(0,queens+1):
  75.             end_pos = [(0,y*CMG.blocksize),(queens*CMG.blocksize,y*CMG.blocksize)]
  76.             pygame.draw.lines(CMG.screen, CMG.GREEN,0 , end_pos, 2)
  77.             end_pos = [(400,y*CMG.blocksize),(400+queens*CMG.blocksize,y*CMG.blocksize)]
  78.             pygame.draw.lines(CMG.screen, CMG.GREEN,0 , end_pos, 2)
  79.         
  80.         for r in range(0,len(rows)):
  81.             y = r*CMG.blocksize + 3
  82.             x = rows[r] *CMG.blocksize + 3
  83.             pygame.draw.rect(CMG.screen, CMG.WHITE, ((x,y), (CMG.blocksize-6, CMG.blocksize-6)), 2)
  84.         if CMG.last_answer != None:
  85.             rows = CMG.last_answer
  86.             for r in range(0,len(rows)):
  87.                 y = r*CMG.blocksize + 3
  88.                 x = rows[r] *CMG.blocksize + 403
  89.                 pygame.draw.rect(CMG.screen, CMG.WHITE, ((x,y), (CMG.blocksize-6, CMG.blocksize-6)), 2)

  90.         #CMG.step += 1

  91.     def moveAll(self):
  92.         pass

  93. #------------------------------------------------
  94. #tkinter,pygame混合区 START
  95. #------------------------------------------------
  96. root = tk.Tk()
  97. root.resizable(0,0)

  98. embed = tk.Frame(root, width = 800, height = 570) #creates embed frame for pygame window
  99. embed.grid(columnspan = (800), rowspan = 730) # Adds grid
  100. embed.pack(side = TOP) #packs window to the left

  101. buttonwin = tk.Frame(root, width = 800, height = 150)
  102. buttonwin.pack(side = BOTTOM)

  103. os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
  104. os.environ['SDL_VIDEODRIVER'] = 'windib'

  105. screen = pygame.display.set_mode((800,570))

  106. #pygame.init()
  107. pygame.display.init()
  108. pygame.mixer.init()
  109. #------------------------------------------------
  110. #tkinter,pygame混合区 END
  111. #------------------------------------------------

  112. #参数,因为函数内要使用之外的变量,需要globe,因此全部打包
  113. class PARAM:
  114.     STATUS = 0
  115.     TICK_NORMAL = 5
  116.     TICK_STEP = 30

  117. #按钮动作区 =====================================
  118. def get_input_data(inputcell,min,max):
  119.     if inputcell.get().isdigit():
  120.         num = int(inputcell.get())
  121.         if min<=num and num<=max:
  122.             return num
  123.     return -1
  124. def exit_game():
  125.     global param
  126.     param.STATUS = 100
  127. def stop_game():
  128.     global param
  129.     param.STATUS = 10
  130. def sel_game():
  131.     global param
  132.     qs = get_input_data(inputqs,4,9)
  133.     speed = get_input_data(inputspd,11,99)
  134.     if qs == -1 or speed == -1:
  135.         return
  136.     else:
  137.         eq = Eight_queen(qs)
  138.         param.cmg = CMG(screen)
  139.         PARAM.TICK_STEP = speed
  140.         param.STATUS = 0
  141. #控件定义区 =====================================
  142. button_stop_b = Button(buttonwin,text = '本次演示结束', width=11, command=stop_game)
  143. button_stop_b.place(x=700,y=30)

  144. btnwin_qs_l = tk.Label(buttonwin, text='皇后数(4-9):')
  145. btnwin_qs_l.place(x=530,y=70)
  146. inputqs = StringVar()
  147. btnwin_qs_e = tk.Entry(buttonwin, show=None,width=2,textvariable = inputqs)
  148. btnwin_qs_e.place(x=590,y=70)
  149. btnwin_spd_l = tk.Label(buttonwin, text='演示速度(11-99):')
  150. btnwin_spd_l.place(x=610,y=70)
  151. inputspd = StringVar()
  152. btnwin_spd_e = tk.Entry(buttonwin, show=None,width=3,textvariable = inputspd)
  153. btnwin_spd_e.place(x=710,y=70)
  154. button_sel_b = Button(buttonwin,text = '执行', width=7, command=sel_game)
  155. button_sel_b.place(x=740,y=65)

  156. button_exit_b = Button(buttonwin,text = '退出画面', width=7, command=exit_game)
  157. button_exit_b.place(x=740,y=100)

  158. #状态,参数,循环中使用,比如STATUS=0:初次进入,1:...100:退出
  159. param = PARAM()
  160. eq = Eight_queen(6)
  161. param.cmg = CMG(screen)
  162. #------------------------------------------------
  163. #主函数,使用pygame框架,无限LOOP对各种事件然后相应处理
  164. #------------------------------------------------
  165. def main():
  166.     global param
  167.     #pygame.mixer.music.play(-1)
  168.     clock = pygame.time.Clock()

  169.     while True:
  170.         #这段event代码是必须的,哪怕在这个程序中不需要,不执行的话整个框架转不动
  171.         for event in pygame.event.get():
  172.             if event.type == QUIT:
  173.                 sys.exit()

  174.         #画面按钮按下后,修改param.STATUS,实际动作这里实现
  175.         if  param.STATUS == 100:
  176.             #退出按钮
  177.             if tk.messagebox.askokcancel('提示', '要退出画面吗'):
  178.                 break
  179.             param.STATUS = 0
  180.         elif param.STATUS == 10:
  181.             pass
  182.         elif param.STATUS == 0:
  183.             res = Eight_queen.queen_next_rows()
  184.             if res != None:
  185.                 param.cmg.printAll(res)
  186.             else:
  187.                 STATUS = 10

  188.         #显示游戏画面
  189.         pygame.display.flip()
  190.         #设置帧率:长期画面不操作,设置成最闲
  191.         if param.STATUS == 0:
  192.             clock.tick(param.TICK_STEP)
  193.         else:
  194.             clock.tick(param.TICK_NORMAL)
  195.             
  196.         root.update()

  197. main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-4 12:25:05 | 显示全部楼层
不错!

不过我今天的评分用完了,评不了抱歉
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-6 10:47:10 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 09:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表