0

I am trying to make a Sudoku grid generator but have been stuck with this final bit for a while. I have successfully made every row a different number. But now I need every column number to be different and I cannot figure out how to do this.

I have already tried removing the possibility of the same number spawning at the top by removing that number from the list. But then for a different row I need that number again. (I tried .pop() and delete and remove)

import tkinter as tk
import random
buttonvalues = ["1","2","3","4","5","6","7","8","9"]
random.shuffle(buttonvalues)
root = tk.Tk()
root.title("Soduku")
i = 0
for x in range(9):
    for y in range(9):
        number = random.choice(buttonvalues)
        btn = tk.Button(text=number, bg="white", activebackground="black", width=2)
        btn.grid(row=y, column=x)
        i += 1
        print(number)
        if number == "1":
            buttonvalues.remove("1")  
        elif number == "2":
            buttonvalues.remove("2")
        elif number == "3":
            buttonvalues.remove("3")
        elif number == "4":
            buttonvalues.remove("4")
        elif number == "5":
            buttonvalues.remove("5")
        elif number == "6":
            buttonvalues.remove("6")
        elif number == "7":
            buttonvalues.remove("7")
        elif number == "8":
            buttonvalues.remove("8")
        elif number == "9":
            buttonvalues.remove("9")
        if not buttonvalues:
            buttonvalues = ["1","2","3","4","5","6","7","8","9"]



root.mainloop()

I do get my expected results a (different number for every row) but now I need every column number to be different including the ones in the rows. Can somebody please help?image

natgame
  • 19
  • 3
  • You have two identical arrays called `buttonvalues`. Is that what you intended to do? Call them by different names so it is clear which one you want to operate on. – rossum Apr 09 '19 at 09:07
  • @rossum that was not intentional – natgame Apr 09 '19 at 09:11
  • @natgame how about maintaining lists for every row/column? When adding numbers to a new row/column, ensure that the numbers present in the list are deleted from **buttonvalues** – FrainBr33z3 Apr 09 '19 at 09:28
  • You should probably separate the algorithm to generate sudoku boards from the GUI - to generate sudoku boards, look [here](https://stackoverflow.com/questions/6924216/how-to-generate-sudoku-boards-with-unique-solutions) – Reblochon Masque Apr 09 '19 at 10:32

1 Answers1

0

I would approach the problem differently. Start with a valid Sudoko grid:

1 2 3 | 4 5 6 | 7 8 9
4 5 6 | 7 8 9 | 1 2 3
7 8 9 | 1 2 3 | 4 5 6
------+-------+------
2 3 4 | 5 6 7 | 8 9 1
5 6 7 | 8 9 1 | 2 3 4
8 9 1 | 2 3 4 | 5 6 7
------+-------+------
3 4 5 | 6 7 8 | 9 1 2
6 7 8 | 9 1 2 | 3 4 5
9 1 2 | 3 4 5 | 6 7 8

Then shuffle the grid so the rules are always followed. You can swap any row of three 3x3 blocks with any other row. You can swap any column of three 3x3 blocks with another column. Within each block row or block column you can swap single rows and single columns. Finally you can permute the numbers so there are different numbers in the various positions, so long as the permutation is consistent across the whole board.

rossum
  • 15,344
  • 1
  • 24
  • 38