Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import pickle, time
- WINDOW_WIDTH, WINDOW_HEIGHT = 600, 350
- GEOMETRY_STRING = str(WINDOW_WIDTH)+'x'+str(WINDOW_HEIGHT)
- class Sudoku(Frame):
- #Initialization of the Frame and attributes / empty lists that will
- #be needed
- def __init__(self, master = None):
- Frame.__init__(self, master)
- self.grid()
- self.create_grid()
- self.entry_list = []
- self.integer_read = []
- self.final_list = []
- self.check_list = []
- self.nums_list = []
- self.read_file()
- self.insertion()
- self.convert_nums()
- #self.check_conf()
- self.check_ans()
- #Pre:
- #Post:
- def create_grid(self):
- self.nums = {}
- self.height, self.width = 9, 9
- counter = 0
- text = StringVar()
- bd = 0
- for row in range(self.height):
- bd = 12
- for column in range(self.width):
- bd = 10
- self.nums[counter] = Entry(self,
- width = 5)
- self.nums[counter].config(bg = 'black',
- font = '12',
- bd = bd,
- disabledforeground = 'green',
- fg = 'yellow',
- justify = CENTER,
- relief = RAISED)
- self.nums[counter].grid(row = row, column = column)
- counter += 1
- #Pre: Needs to be given a file containting integers for the sudoku
- #puzzle
- #Post: Breaks the files lines up into 2D lists of integers for every
- #integer in the current string on the line
- def read_file(self):
- file = "assignment1test.txt"
- read = open(file, "r").readlines()
- #Removes \n for just integers in the string
- for element in read:
- self.integer_read.append(element[0:-1])
- #Takes every integer in the string of the subsequent elements
- #and creates a list inside final_list containing these integers
- for element in self.integer_read:
- new_list = []
- for i in range(len(element)):
- self.final_list.append(element[i])
- new_list.append(element[i])
- if i == len(element) - 1:
- self.check_list.append(new_list)
- print(self.final_list)
- print(self.check_list)
- #Pre: None
- #Post: Takes the initial seed values in the list of self.final_list
- #create in the read_file function and places them on our Sudoku grid
- def insertion(self):
- for i in range(len(self.final_list)):
- if int(self.final_list[i]) != 0:
- self.nums[i].insert(0, int(self.final_list[i]))
- #Pre: None
- #Post: Checks the grid for any conflicts in solving the Sudoku puzzle
- #that the user might create while trying to solve it
- def convert_nums(self):
- index = 0
- for i in range(9):
- self.nums_list.append([])
- for i in range(len(self.nums)):
- if i <= 9:
- self.nums_list[0].append(self.nums[i])
- if 9 <= i < 19:
- self.nums_list[1].append(self.nums[i])
- if 18 <= i < 28:
- self.nums_list[2].append(self.nums[i])
- if 27 <= i < 37:
- self.nums_list[3].append(self.nums[i])
- if 36 <= i < 46:
- self.nums_list[4].append(self.nums[i])
- if 45 <= i < 55:
- self.nums_list[5].append(self.nums[i])
- if 54 <= i < 64:
- self.nums_list[6].append(self.nums[i])
- if 63 <= i < 73:
- self.nums_list[7].append(self.nums[i])
- if 72 <= i < 82:
- self.nums_list[8].append(self.nums[i])
- def check_conf(self):
- for r in range(0, self.height, 1):
- for c in range(0, 9, 1):
- if self.nums_list[r][c].get().isdigit():
- if int(self.nums_list[r][c].get()) == int(self.check_list[r][c]):
- self.nums_list[r][c].config(background = 'red')
- def check_ans(self):
- pass
- root = Tk()
- root.geometry(GEOMETRY_STRING)
- # root.bind("<Key-S>", save_pickle)
- # root.bind("<Key-L>", load_pickle)
- app = Sudoku()
- app.master.title('Sudoku')
- app.pack()
- while True:
- app.check_conf()
- time.sleep(0.25)
- app.update()
- app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement