Guest User

Untitled

a guest
Jul 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. import random
  2. numbers = [1,2,3,4,5,6,7,8,9]
  3.  
  4. def reg():
  5. def makeBoard():
  6. board = None
  7. while board is None:
  8. board = attemptBoard()
  9. return board
  10.  
  11. def attemptBoard():
  12. board = [[None for _ in range(9)] for _ in range(9)]
  13. for i in range(9):
  14. for j in range(9):
  15. checking = numbers[:]
  16. random.shuffle(checking)
  17. x = -1
  18. loopStart = 0
  19. while board[i][j] is None:
  20. x += 1
  21. if x == 9:
  22.  
  23. return None
  24. checkMe = [checking[x],True]
  25. if checkMe in board[i]:
  26.  
  27. continue
  28. checkis = False
  29. for checkRow in board:
  30. if checkRow[j] == checkMe:
  31.  
  32. checkis = True
  33. if checkis: continue
  34.  
  35. if i % 3 == 1:
  36. if j % 3 == 0 and checkMe in (board[i-1][j+1],board[i-1][j+2]): continue
  37. elif j % 3 == 1 and checkMe in (board[i-1][j-1],board[i-1][j+1]): continue
  38. elif j % 3 == 2 and checkMe in (board[i-1][j-1],board[i-1][j-2]): continue
  39. elif i % 3 == 2:
  40. if j % 3 == 0 and checkMe in (board[i-1][j+1],board[i-1][j+2],board[i-2][j+1],board[i-2][j+2]): continue
  41. elif j % 3 == 1 and checkMe in (board[i-1][j-1],board[i-1][j+1],board[i-2][j-1],board[i-2][j+1]): continue
  42. elif j % 3 == 2 and checkMe in (board[i-1][j-1],board[i-1][j-2],board[i-2][j-1],board[i-2][j-2]): continue
  43.  
  44. board[i][j] = checkMe
  45. return board
  46. a=makeBoard()
  47.  
  48. from Tkinter import *
  49. import random
  50. numbers = [1,2,3,4,5,6,7,8,9]
  51. window=Tk()
  52. window.title("SUDOKO")
  53. def reg():
  54. def makeBoard():
  55. board = None
  56. while board is None:
  57. board = attemptBoard()
  58. return board
  59.  
  60. def attemptBoard():
  61. board = [[None for in range(9)] for in range(9)]
  62. for i in range(9):
  63. for j in range(9):
  64. checking = numbers[:]
  65. random.shuffle(checking)
  66. x = -1
  67. loopStart = 0
  68. while board[i][j] is None:
  69. x += 1
  70. if x == 9:
  71.  
  72. return None
  73. checkMe = [checking[x],True]
  74. if checkMe in board[i]:
  75.  
  76. continue
  77. checkis = False
  78. for checkRow in board:
  79. if checkRow[j] == checkMe:
  80.  
  81. checkis = True
  82. if checkis: continue
  83.  
  84. if i % 3 == 1:
  85. if j % 3 == 0 and checkMe in (board[i-1][j+1],board[i-1][j+2]): continue
  86. elif j % 3 == 1 and checkMe in (board[i-1][j-1],board[i-1][j+1]): continue
  87. elif j % 3 == 2 and checkMe in (board[i-1][j-1],board[i-1][j-2]): continue
  88. elif i % 3 == 2:
  89. if j % 3 == 0 and checkMe in (board[i-1][j+1],board[i-1][j+2],board[i-2][j+1],board[i-2][j+2]): continue
  90. elif j % 3 == 1 and checkMe in (board[i-1][j-1],board[i-1][j+1],board[i-2][j-1],board[i-2][j+1]): continue
  91. elif j % 3 == 2 and checkMe in (board[i-1][j-1],board[i-1][j-2],board[i-2][j-1],board[i-2][j-2]): continue
  92.  
  93. board[i][j] = checkMe
  94. return board
  95. a=makeBoard()
  96. rows = []
  97. for i in range(1,10):
  98. cols = []
  99. for j in range(1,10):
  100. e = Label(window,text=str(a[i-1][j-1][0]),width=6, relief="groove")
  101. e.grid(row=i, column=j, sticky=NSEW,)
  102. cols.append(e)
  103. rows.append(cols)
  104. l1=Label(window,text="*")
  105. l1.grid(row=0,column=0)
  106. l2=Label(window,text="9*9 SUDOKU Generator",height=3)
  107. l2.grid(row=0,column=3,columnspan=4)
  108.  
  109. b1=Button(window,text="Regenerate",width=6,command=reg)
  110. b1.grid(row=10,column=8)
  111. b6=Button(window,text="close",width=6,command=window.destroy)
  112. b6.grid(row=11,column=8)
Add Comment
Please, Sign In to add comment