본문으로 바로가기

최종적으로 파이썬 GUI프로그램을 완성했다.


우선 GUI를 구현한 sudoku.py는 아래와 같다.


from tkinter import *

from tkinter import font

from sudoku_al import *


arr=[[[]for i in range(9)]for i in range(9)]


def solve():

    for y in range(9):

        for x in range(9):

            tm = arr[y][x].get()

            if tm == '':

                tm = 0

            if int(tm) > 9:

                exit(1)

            game[y][x] = int(tm)

    for i in game:

        print(i)

    startTree(0)

    mainOutput()



window = Tk()

window.title("뚝딱 스도쿠")


font1 = font.Font(size=15)


def mainInput():

    row_index = 1

    col_index = 0

    for y in range(9):

        for x in  range(9):

            arr[y][x]=Entry(window, width=2, font = font1, bg="gray", fg="white")

            arr[y][x].grid(row=row_index,column=col_index,columnspan=1)

            col_index += 1

            if col_index > 8:

                row_index += 1

                col_index = 0

    Button(window, text = "Solve!!", width=32,height=1,command = solve).grid(row =13, column =0, columnspan=9)


def mainOutput():

    row_index = 1

    col_index = 0

    for i in range(9):

        for button_text in  game[i]:

            Label(window, text = button_text, width = 2, height = 1, font = font1,relief="raised").grid(row = row_index, column=col_index)

            col_index += 1

            if col_index > 8:

                row_index += 1

                col_index = 0


mainInput()


스도쿠의 알고리즘을 구현한 sudoku_al.py의 코드는 아래와 같다.



game=[[0,0,0,0,0,0,0,0,0],

      [0,0,0,0,0,0,0,0,0],

      [0,0,0,0,0,0,0,0,0],

      [0,0,0,0,0,0,0,0,0],

      [0,0,0,0,0,0,0,0,0],

      [0,0,0,0,0,0,0,0,0],

      [0,0,0,0,0,0,0,0,0],

      [0,0,0,0,0,0,0,0,0],

      [0,0,0,0,0,0,0,0,0]]


tmp = [[] for i in range(81)]


def startTree(count): #트리구조 대입

    if count ==81:

        return 7

    y0=count//9

    x0=count%9

    findPossibility(x0,y0)

    

    tt=1

    while True:

        if tt==7:

            return 7

        elif tt==0:

            if tmp[count-1]!=[0]:

                game[(count-1)//9][(count-1)%9]=0

                return 1

            else:

                return 0

        if tmp[count]==[]:

            tt=0

            continue

        elif tmp[count] !=[0]:

            game[y0][x0]=tmp[count][0]

            del tmp[count][0]

        tt = startTree(count+1)

        if tmp[count] == [0] and tt!=7:

            tt=0

    

def findBase(locate):#기본 사각형 위치 초기화

    if locate<=2:

        return 0

    elif locate>2 and locate<=5:

        return 3

    else:

        return 6

                    


def findPossibility(x1,y1):#해당좌표 가능성 검사

    yTmp=findBase(y1)

    xTmp=findBase(x1)

    tmp[y1*9+x1] = []

    if game[y1][x1] != 0:

        tmp[y1*9+x1] = [0]

        return

    for target in range(1,10):

        for find in range(9):

            if target == game[find][x1]:#세로검사

                find=10

            if find==0 or find==1 or find==2:#네모칸 검사

                if target in game[yTmp+find][xTmp:xTmp+2]:

                    find=10

            if find==10:

                break

        if target in game[y1] or find==10:#가로검사

            continue

        else:

            tmp[y1*9+x1].append(target)


sudoku.py

sudoku_al.py