I'm trying to write a sudoku program. It works, it draws the board and you can fill in the missing numbers yourself by clicking on the screen, selecting a box (A2 or C7 or G4 etc.) and then entering a number from 1 to 9. I also added a reset button that clears all the numbers you added. But now I also wanted to make it so that it checks whether the sudoku is correct or not. But I can't think of a way to do that. I tried it with the:
#checks for a win
if a2==1 and a5==7 and a8==9 and a9==2 and b3==9 and b4==1 and b5==3 and b6==4 and b7==7 and b8==6 and b9==8 and c1==4 and c4==6 and c5==2 and c6==9 and c7==5 and d1==2 and d2==6 and d4==4 and d6==5 and d7==9 and d9==7 and e2==7 and e3==4 and e7==1 and e8==2 and f1==8 and f3==1 and f4==7 and f6==2 and f8==4 and f9==3 and g3==8 and g4==9 and g5==4 and g6==7 and g9==6 and h1==6 and h2==9 and h3==2 and h4==3 and h5==5 and h6==1 and h7==8 and i1==7 and i2==4 and i5==8 and i8==1 and i9==9:
win.goto(0,0)
win.color("green")
win.write("Correct", font=("Arial", 64, "bold"), align="center")
else:
win.clear()
pass
part, but then I get the error whenever I enter a number. I can still keep playing tho, it just gives me an error everytime I enter a number:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Applications/Thonny.app/Contents/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/Applications/Thonny.app/Contents/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 675, in eventfun
fun(x, y)
File "/Users/xxx/Library/CloudStorage/OneDrive-xxx/python/sudoku 4 .py", line 480, in play
if a2==1 and a5==7 and a8==9 and a9==2 and b3==9 and b4==1 and b5==3 and b6==4 and b7==7 and b8==6 and b9==8 and c1==4 and c4==6 and c5==2 and c6==9 and c7==5 and d1==2 and d2==6 and d4==4 and d6==5 and d7==9 and d9==7 and e2==7 and e3==4 and e7==1 and e8==2 and f1==8 and f3==1 and f4==7 and f6==2 and f8==4 and f9==3 and g3==8 and g4==9 and g5==4 and g6==7 and g9==6 and h1==6 and h2==9 and h3==2 and h4==3 and h5==5 and h6==1 and h7==8 and i1==7 and i2==4 and i5==8 and i8==1 and i9==9:
NameError: name 'a5' is not defined
But the variables are defined right? I add them when you enter a number. So what I did was, every empty box has a variable and everytime you enter a number in an empty box, it changes that variable to the number you put in. And then when all the numbers are correct, it tells you so. This is the whole code, sorry if it's too long or a bit messy, I'm working on that:
import turtle
tur = turtle.Turtle() #turtle for writing missing numbers
tur.speed(0)
tur.up()
tur.ht()
pen = turtle.Turtle() #turtle for drawing the whole board and known numbers
pen.speed(0)
pen.up()
pen.ht()
win = turtle.Turtle() #turtle for when you have it correct
win.ht()
win.speed(0)
win.up()
screen = turtle.Screen()
screen.setup(800,800)
screen.tracer(0)
LENGTH_BOARD = 600
fontBoard = ("Arial", 14,)
fontNumbers = ("Arial", 36)
#this is function for writing the known numbers
def sudokuSetup(x,y,height,length,text,color):
pen.goto(x,y)
pen.color(color)
pen.right(90)
pen.forward(height)
pen.left(90)
pen.backward(LENGTH_BOARD/19)
pen.forward(length)
pen.write(text, font=fontNumbers, align="center")
#this is function for writing the missing numbers put in by the user
def sudokuPlay(x,y,height,length,text):
#drawing white square/erase previous number
tur.goto(x,y)
tur.color("white")
tur.fillcolor("white")
tur.right(90)
tur.forward(height-50)
tur.left(90)
tur.backward(LENGTH_BOARD/19)
tur.forward(length-30)
tur.down()
tur.begin_fill()
for i in range(3):
tur.forward(60)
tur.right(90)
tur.forward(60)
tur.end_fill()
tur.up()
#going to right position and writing the number
tur.goto(x,y)
tur.color("dark blue")
tur.right(180)
tur.forward(height)
tur.left(90)
tur.backward(LENGTH_BOARD/19)
tur.forward(length)
tur.write(text, font=fontNumbers, align="center")
#letters on left side
def letters(text,length):
pen.forward(length/9)
pen.write(text, font=fontBoard, align="center")
#the board itself
def board(x,y,length):
pen.goto(x,y)
pen.down()
#border
for i in range(4):
pen.width(2)
pen.forward(length)
pen.right(90)
#lines vertical
for i in range(3):
for i in range(2):
pen.width(1)
pen.forward(length/9)
pen.right(90)
pen.forward(length)
pen.right(180)
pen.forward(length)
pen.right(90)
pen.forward(length/9)
pen.right(90)
pen.width(2)
pen.forward(length)
pen.right(180)
pen.forward(length)
pen.right(90)
pen.up()
pen.width(1)
pen.goto(x,y)
pen.right(90)
pen.down()
#horizontal lines
for i in range(3):
for i in range(2):
pen.width(1)
pen.forward(length/9)
pen.left(90)
pen.forward(length)
pen.left(180)
pen.forward(length)
pen.left(90)
pen.forward(length/9)
pen.left(90)
pen.width(2)
pen.forward(length)
pen.left(180)
pen.forward(length)
pen.left(90)
#numbers on top
numb = 1
pen.up()
pen.goto(x,y)
pen.left(180)
pen.forward(20)
pen.right(90)
pen.backward(length/18)
for i in range(9):
pen.forward(length/9)
pen.write(f"{numb}", font=fontBoard, align="center")
numb += 1
#letters on left side
pen.goto(x,y)
pen.left(180)
pen.forward(20)
pen.left(90)
pen.backward(length/20)
letters("A",length)
letters("B",length)
letters("C",length)
letters("D",length)
letters("E",length)
letters("F",length)
letters("G",length)
letters("H",length)
letters("I",length)
pen.left(90)
#these are the known numbers
sudokuSetup(-300,300,((LENGTH_BOARD/9)*1)-12, (LENGTH_BOARD/9)*1, "3","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*1)-12, (LENGTH_BOARD/9)*3, "6","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*1)-12, (LENGTH_BOARD/9)*4, "5","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*1)-12, (LENGTH_BOARD/9)*6, "8","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*1)-12, (LENGTH_BOARD/9)*7, "4","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*2)-12, (LENGTH_BOARD/9)*1, "5","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*2)-12, (LENGTH_BOARD/9)*2, "2","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*3)-12, (LENGTH_BOARD/9)*2, "8","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*3)-12, (LENGTH_BOARD/9)*3, "7","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*3)-12, (LENGTH_BOARD/9)*8, "3","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*3)-12, (LENGTH_BOARD/9)*9, "1","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*4)-12, (LENGTH_BOARD/9)*3, "3","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*4)-12, (LENGTH_BOARD/9)*5, "1","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*4)-12, (LENGTH_BOARD/9)*8, "8","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*5)-12, (LENGTH_BOARD/9)*1, "9","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*5)-12, (LENGTH_BOARD/9)*4, "8","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*5)-12, (LENGTH_BOARD/9)*5, "6","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*5)-12, (LENGTH_BOARD/9)*6, "3","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*5)-12, (LENGTH_BOARD/9)*9, "5","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*6)-12, (LENGTH_BOARD/9)*2, "5","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*6)-12, (LENGTH_BOARD/9)*5, "9","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*6)-12, (LENGTH_BOARD/9)*7, "6","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*7)-12, (LENGTH_BOARD/9)*1, "1","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*7)-12, (LENGTH_BOARD/9)*2, "3","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*7)-12, (LENGTH_BOARD/9)*7, "2","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*7)-12, (LENGTH_BOARD/9)*8, "5","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*8)-12, (LENGTH_BOARD/9)*8, "7","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*8)-12, (LENGTH_BOARD/9)*9, "4","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*9)-12, (LENGTH_BOARD/9)*3, "5","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*9)-12, (LENGTH_BOARD/9)*4, "2","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*9)-12, (LENGTH_BOARD/9)*6, "6","black")
sudokuSetup(-300,300,((LENGTH_BOARD/9)*9)-12, (LENGTH_BOARD/9)*7, "3","black")
#the reset button
def reset_button():
pen.goto(-100,-320)
pen.down()
for i in range(2):
pen.forward(200)
pen.right(90)
pen.forward(65)
pen.right(90)
pen.up()
pen.goto(0,-360)
pen.write("Reset board", font= ("Arial", 20, "normal"), align="center")
#draws everything
def draw_board():
board(-300,300,LENGTH_BOARD)
reset_button()
screen.update()
#the game
def play(x,y):
if x > -300 and x < 300 and y > -300 and y < 300: #if you click on the board
which_one = turtle.textinput("Welk vakje?" , "In welk vakje wil je een nummer plaatsen?") #it asks for the box
what_number = turtle.numinput("Welk nummer?", "Welk nummer wil je invoeren?", minval=1, maxval=9) #and then for a number 1-9
if which_one == "A2" or which_one == "a2": #when you choose box a2
a2 = 0 #variable a2 is set to 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*1)-12, (LENGTH_BOARD/9)*2, int(what_number)) #fills in the number in the right box
a2 += what_number #changes the a2 variable to the number you entered
elif which_one == "B3" or which_one == "b3": #then it repeats for all the empty boxes
b3 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*2)-12, (LENGTH_BOARD/9)*3, int(what_number))
b3 += what_number
elif which_one == "C1" or which_one == "c1":
c1 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*3)-12, (LENGTH_BOARD/9)*1, int(what_number))
c1 += what_number
elif which_one == "A5" or which_one == "a5":
a5 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*1)-12, (LENGTH_BOARD/9)*5, int(what_number))
a5 += what_number
elif which_one == "B4" or which_one == "b4":
b4 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*2)-12, (LENGTH_BOARD/9)*4, int(what_number))
b4 += what_number
elif which_one == "B5" or which_one == "b5":
b5 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*2)-12, (LENGTH_BOARD/9)*5, int(what_number))
b5 += what_number
elif which_one == "B6" or which_one == "b6":
b6 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*2)-12, (LENGTH_BOARD/9)*6, int(what_number))
b6 += what_number
elif which_one == "C4" or which_one == "c4":
c4 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*3)-12, (LENGTH_BOARD/9)*4, int(what_number))
c4 += what_number
elif which_one == "C5" or which_one == "c5":
c5 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*3)-12, (LENGTH_BOARD/9)*5, int(what_number))
c5 += what_number
elif which_one == "C6" or which_one == "c6":
c6 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*3)-12, (LENGTH_BOARD/9)*6, int(what_number))
c6 += what_number
elif which_one == "A8" or which_one == "a8":
a8 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*1)-12, (LENGTH_BOARD/9)*8, int(what_number))
a8 += what_number
elif which_one == "A9" or which_one == "a9":
a9 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*1)-12, (LENGTH_BOARD/9)*9, int(what_number))
a9 += what_number
elif which_one == "B7" or which_one == "b7":
b7 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*2)-12, (LENGTH_BOARD/9)*7, int(what_number))
b7 += what_number
elif which_one == "B8" or which_one == "b8":
b8 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*2)-12, (LENGTH_BOARD/9)*8, int(what_number))
b8 += what_number
elif which_one == "B9" or which_one == "b9":
b9 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*2)-12, (LENGTH_BOARD/9)*9, int(what_number))
b9 += what_number
elif which_one == "C7" or which_one == "c7":
c7 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*3)-12, (LENGTH_BOARD/9)*7, int(what_number))
c7 += what_number
elif which_one == "D1" or which_one == "d1":
d1 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*4)-12, (LENGTH_BOARD/9)*1, int(what_number))
d1 += what_number
elif which_one == "D2" or which_one == "d2":
d2 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*4)-12, (LENGTH_BOARD/9)*2, int(what_number))
d2 += what_number
elif which_one == "E2" or which_one == "e2":
e2 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*5)-12, (LENGTH_BOARD/9)*2, int(what_number))
e2 += what_number
elif which_one == "E3" or which_one == "e3":
e3 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*5)-12, (LENGTH_BOARD/9)*3, int(what_number))
e3 += what_number
elif which_one == "F1" or which_one == "f1":
f1 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*6)-12, (LENGTH_BOARD/9)*1, int(what_number))
f1 += what_number
elif which_one == "F3" or which_one == "f3":
f3 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*6)-12, (LENGTH_BOARD/9)*3, int(what_number))
f3 += what_number
elif which_one == "D4" or which_one == "d4":
d4 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*4)-12, (LENGTH_BOARD/9)*4, int(what_number))
d4 += what_number
elif which_one == "D6" or which_one == "d6":
d6 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*4)-12, (LENGTH_BOARD/9)*6, int(what_number))
d6 += what_number
elif which_one == "F4" or which_one == "f4":
f4 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*6)-12, (LENGTH_BOARD/9)*4, int(what_number))
f4 += what_number
elif which_one == "F6" or which_one == "f6":
f6 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*6)-12, (LENGTH_BOARD/9)*6, int(what_number))
f6 += what_number
elif which_one == "D7" or which_one == "d7":
d7 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*4)-12, (LENGTH_BOARD/9)*7, int(what_number))
d7 += what_number
elif which_one == "D9" or which_one == "d9":
d9 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*4)-12, (LENGTH_BOARD/9)*9, int(what_number))
d9 += what_number
elif which_one == "E7" or which_one == "e7":
e7 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*5)-12, (LENGTH_BOARD/9)*7, int(what_number))
e7 += what_number
elif which_one == "E8" or which_one == "e8":
e8 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*5)-12, (LENGTH_BOARD/9)*8, int(what_number))
e8 += what_number
elif which_one == "F8" or which_one == "f8":
f8 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*6)-12, (LENGTH_BOARD/9)*8, int(what_number))
f8 += what_number
elif which_one == "F9" or which_one == "f9":
f9 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*6)-12, (LENGTH_BOARD/9)*9, int(what_number))
f9 += what_number
elif which_one == "G3" or which_one == "g3":
g3 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*7)-12, (LENGTH_BOARD/9)*3, int(what_number))
g3 += what_number
elif which_one == "H1" or which_one == "h1":
h1 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*8)-12, (LENGTH_BOARD/9)*1, int(what_number))
h1 += what_number
elif which_one == "H2" or which_one == "h2":
h2 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*8)-12, (LENGTH_BOARD/9)*2, int(what_number))
h2 += what_number
elif which_one == "H3" or which_one == "h3":
h3 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*8)-12, (LENGTH_BOARD/9)*3, int(what_number))
h3 += what_number
elif which_one == "I1" or which_one == "i1":
i1 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*9)-12, (LENGTH_BOARD/9)*1, int(what_number))
i1 += what_number
elif which_one == "I2" or which_one == "i2":
i2 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*9)-12, (LENGTH_BOARD/9)*2, int(what_number))
i2 += what_number
elif which_one == "G4" or which_one == "g4":
g4 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*7)-12, (LENGTH_BOARD/9)*4, int(what_number))
g4 += what_number
elif which_one == "G5" or which_one == "g5":
g5 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*7)-12, (LENGTH_BOARD/9)*5, int(what_number))
g5 += what_number
elif which_one == "G6" or which_one == "g6":
g6 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*7)-12, (LENGTH_BOARD/9)*6, int(what_number))
g6 += what_number
elif which_one == "H4" or which_one == "h4":
h4 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*8)-12, (LENGTH_BOARD/9)*4, int(what_number))
h4 += what_number
elif which_one == "H5" or which_one == "h5":
h5 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*8)-12, (LENGTH_BOARD/9)*5, int(what_number))
h5 += what_number
elif which_one == "H6" or which_one == "h6":
h6 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*8)-12, (LENGTH_BOARD/9)*6, int(what_number))
h6 += what_number
elif which_one == "I5" or which_one == "i5":
i5 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*9)-12, (LENGTH_BOARD/9)*5, int(what_number))
i5 += what_number
elif which_one == "G9" or which_one == "g9":
g9 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*7)-12, (LENGTH_BOARD/9)*9, int(what_number))
g9 += what_number
elif which_one == "H7" or which_one == "h7":
h7 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*8)-12, (LENGTH_BOARD/9)*7, int(what_number))
h7 += what_number
elif which_one == "I8" or which_one == "i8":
i8 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*9)-12, (LENGTH_BOARD/9)*8, int(what_number))
i8 += what_number
elif which_one == "I9" or which_one == "i9":
i9 = 0
sudokuPlay(-300,300,((LENGTH_BOARD/9)*9)-12, (LENGTH_BOARD/9)*9, int(what_number))
i9 += what_number
else: #when you fill in anything else
pass
#this is the reset button
elif x > -100 and x < 100 and y > -400 and y < -320: #if you click on the reset button
tur.clear() #it resets the numbers you put in
win.clear() #it resets the win turtle if you won and want to do it again for some reason :p
#checks for a win
if a2==1 and a5==7 and a8==9 and a9==2 and b3==9 and b4==1 and b5==3 and b6==4 and b7==7 and b8==6 and b9==8 and c1==4 and c4==6 and c5==2 and c6==9 and c7==5 and d1==2 and d2==6 and d4==4 and d6==5 and d7==9 and d9==7 and e2==7 and e3==4 and e7==1 and e8==2 and f1==8 and f3==1 and f4==7 and f6==2 and f8==4 and f9==3 and g3==8 and g4==9 and g5==4 and g6==7 and g9==6 and h1==6 and h2==9 and h3==2 and h4==3 and h5==5 and h6==1 and h7==8 and i1==7 and i2==4 and i5==8 and i8==1 and i9==9:
#if all the variables have the right number
win.goto(0,0)
win.color("green")
win.write("Correct", font=("Arial", 64, "bold"), align="center") #it writes "Correct" on screen
else: #if the variables dont have the right number
win.clear() #it clears win turtle just in case
pass #and it passes so you can keep playing
draw_board() #draws everything
turtle.onscreenclick(play, 1, True) #makes sure you can click
turtle.done()
I hope it is clear what I'm doing in my code! If not, please tell me, then I can change it or explain better. :)