Advertisement
Guest User

Untitled

a guest
Jan 17th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. from tkinter import *
  2. import pickle, time
  3.  
  4. WINDOW_WIDTH, WINDOW_HEIGHT = 600, 350
  5. GEOMETRY_STRING = str(WINDOW_WIDTH)+'x'+str(WINDOW_HEIGHT)
  6.  
  7. class Sudoku(Frame):
  8.  
  9. #Initialization of the Frame and attributes / empty lists that will
  10. #be needed
  11. def __init__(self, master = None):
  12. Frame.__init__(self, master)
  13. self.grid()
  14. self.create_grid()
  15. self.entry_list = []
  16. self.integer_read = []
  17. self.final_list = []
  18. self.check_list = []
  19. self.nums_list = []
  20. self.read_file()
  21. self.insertion()
  22. self.convert_nums()
  23. #self.check_conf()
  24. self.check_ans()
  25.  
  26. #Pre:
  27. #Post:
  28. def create_grid(self):
  29. self.nums = {}
  30. self.height, self.width = 9, 9
  31. counter = 0
  32. text = StringVar()
  33. bd = 0
  34. for row in range(self.height):
  35. bd = 12
  36. for column in range(self.width):
  37. bd = 10
  38. self.nums[counter] = Entry(self,
  39. width = 5)
  40. self.nums[counter].config(bg = 'black',
  41. font = '12',
  42. bd = bd,
  43. disabledforeground = 'green',
  44. fg = 'yellow',
  45. justify = CENTER,
  46. relief = RAISED)
  47.  
  48. self.nums[counter].grid(row = row, column = column)
  49. counter += 1
  50.  
  51. #Pre: Needs to be given a file containting integers for the sudoku
  52. #puzzle
  53. #Post: Breaks the files lines up into 2D lists of integers for every
  54. #integer in the current string on the line
  55. def read_file(self):
  56. file = "assignment1test.txt"
  57. read = open(file, "r").readlines()
  58.  
  59. #Removes \n for just integers in the string
  60. for element in read:
  61. self.integer_read.append(element[0:-1])
  62.  
  63. #Takes every integer in the string of the subsequent elements
  64. #and creates a list inside final_list containing these integers
  65. for element in self.integer_read:
  66. new_list = []
  67. for i in range(len(element)):
  68. self.final_list.append(element[i])
  69. new_list.append(element[i])
  70. if i == len(element) - 1:
  71. self.check_list.append(new_list)
  72.  
  73. print(self.final_list)
  74. print(self.check_list)
  75. #Pre: None
  76. #Post: Takes the initial seed values in the list of self.final_list
  77. #create in the read_file function and places them on our Sudoku grid
  78. def insertion(self):
  79.  
  80. for i in range(len(self.final_list)):
  81. if int(self.final_list[i]) != 0:
  82. self.nums[i].insert(0, int(self.final_list[i]))
  83.  
  84. #Pre: None
  85. #Post: Checks the grid for any conflicts in solving the Sudoku puzzle
  86. #that the user might create while trying to solve it
  87.  
  88. def convert_nums(self):
  89. index = 0
  90. for i in range(9):
  91. self.nums_list.append([])
  92. for i in range(len(self.nums)):
  93. if i <= 9:
  94. self.nums_list[0].append(self.nums[i])
  95. if 9 <= i < 19:
  96. self.nums_list[1].append(self.nums[i])
  97. if 18 <= i < 28:
  98. self.nums_list[2].append(self.nums[i])
  99. if 27 <= i < 37:
  100. self.nums_list[3].append(self.nums[i])
  101. if 36 <= i < 46:
  102. self.nums_list[4].append(self.nums[i])
  103. if 45 <= i < 55:
  104. self.nums_list[5].append(self.nums[i])
  105. if 54 <= i < 64:
  106. self.nums_list[6].append(self.nums[i])
  107. if 63 <= i < 73:
  108. self.nums_list[7].append(self.nums[i])
  109. if 72 <= i < 82:
  110. self.nums_list[8].append(self.nums[i])
  111.  
  112. def check_conf(self):
  113. for r in range(0, self.height, 1):
  114. for c in range(0, 9, 1):
  115. if self.nums_list[r][c].get().isdigit():
  116. if int(self.nums_list[r][c].get()) == int(self.check_list[r][c]):
  117. self.nums_list[r][c].config(background = 'red')
  118.  
  119. def check_ans(self):
  120. pass
  121.  
  122.  
  123. root = Tk()
  124. root.geometry(GEOMETRY_STRING)
  125. # root.bind("<Key-S>", save_pickle)
  126. # root.bind("<Key-L>", load_pickle)
  127. app = Sudoku()
  128.  
  129. app.master.title('Sudoku')
  130. app.pack()
  131. while True:
  132. app.check_conf()
  133. time.sleep(0.25)
  134. app.update()
  135. app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement