欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python:棋盘覆盖问题

程序员文章站 2022-05-22 12:37:51
...
利用Python的tkinter库,实现棋盘覆盖的GUI界面
import tkinter as tk
class chess:
    
    def __init__(self):
        window = tk.Tk()
        window.title('Chess')
        self.mark =0
        self.cv = tk.Canvas(window,bg = 'white',width = 522, height = 522)
#创建frame的框架,窗口window为这个框架的父容器
        frame = tk.Frame(window)
        frame.pack()

#控件设置
        self.BoardSet = tk.Button(frame,text = 'SetBoard', command = self.ChessBoardSet)
        self.Set = tk.Button(frame,text = 'Start', command = self.SpecialPointSet)
        self.entrySize = tk.Entry(frame)
        self.entryCd1 = tk.Entry(frame)
        self.entryCd2 = tk.Entry(frame)

#控件布局
        self.entrySize.grid(row = 500,column = 50)
        self.BoardSet.grid(row = 500,column = 60)
        self.entryCd1.grid(row = 500,column = 100)
        self.entryCd2.grid(row = 500,column = 150)
        self.Set.grid(row = 500,column = 200)
        window.mainloop()
    
    
#设置棋盘
    def ChessBoardSet(self):
        number = int(self.entrySize.get())
        singlelength = 512//number
        for i in range(number):
            for j in range(number+1):
                self.cv.create_rectangle(10+j*singlelength,10+i*singlelength,10+singlelength,10+i*singlelength+singlelength)       
        self.cv.pack()
        
#设置特殊点    
    def SpecialPointSet(self):
        number = int(self.entrySize.get())
        singlelength = 512//number
        prow = int(self.entryCd1.get())
        pcolumn = int(self.entryCd2.get())
        sprt = self.cv.create_rectangle(10+prow*singlelength,10+pcolumn*singlelength,10+singlelength+prow*singlelength,10+pcolumn*singlelength+singlelength)
        self.cv.itemconfig(sprt, fill="black")
        self.cv.pack()
        self.Arrange(0,0,prow,pcolumn,int(self.entrySize.get()))

    def colorfill(self,prow,pcolumn,count):
        color = ['#FFB6C1','#A9A9A9','#DC143C','#FFF0F5','#800080','#40E0D0','#4B0082','#F8F8FF','#0000FF','#D2B48C','#87CEEB',
             '#A52A2A','#40E0D0','#98FB98','#008000','#9ACD32','#FFFFF0','#FFFF00','#FFD700','#F5DEB3','#FFEFD5','#FF0000',
             '#F5F5F5','#C0C0C0','#808080']
        select = int((count%10))
        number = int(self.entrySize.get())
        singlelength = 512//number
        sprt = self.cv.create_rectangle(10+prow*singlelength,10+pcolumn*singlelength,10+singlelength+prow*singlelength,10+pcolumn*singlelength+singlelength)
        self.cv.itemconfig(sprt, fill=color[select])
        self.cv.pack()
    
#放入骨牌    
    def Arrange(self,trow,tcolumn,prow,pcolumn,size):
        global mark
        self.mark +=1  
        count = self.mark 
        if size == 1: 
            return  
        half = size//2  
    
        if prow<trow+half and pcolumn<tcolumn+half:
            self.Arrange(trow,tcolumn,prow,pcolumn,half) 
        else:  
            self.Arrange(trow,tcolumn,trow+half-1,tcolumn+half-1,half)  
            self.colorfill(trow+half-1,tcolumn+half-1,count)
   
        if prow<trow+half and pcolumn>=tcolumn+half:  
            self.Arrange(trow,tcolumn+half,prow,pcolumn,half)  
        else:  
            self.Arrange(trow,tcolumn+half,trow+half-1,tcolumn+half,half)  
            self.colorfill(trow+half-1,tcolumn+half,count)
    
        if prow>=trow+half and pcolumn<tcolumn+half:  
            self.Arrange(trow+half,tcolumn,prow,pcolumn,half)  
        else:  
            self.Arrange(trow+half,tcolumn,trow+half,tcolumn+half-1,half) 
            self.colorfill(trow+half,tcolumn+half-1,count)
    
        if prow>=trow+half and pcolumn>=tcolumn+half:  
            self.Arrange(trow+half,tcolumn+half,prow,pcolumn,half)  
        else:  
            self.Arrange(trow+half,tcolumn+half,trow+half,tcolumn+half,half)  
            self.colorfill(trow+half,tcolumn+half,count)  

start = chess()
start.__init__()

以下为实验结果图

Python:棋盘覆盖问题

经过测试,本程序最多只能在规格为256*256的棋盘上完成,当规格为512*512及其以上级别时,程序将会报错。


相关标签: Python