Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Sudoku
- import turtle
- import random
- #five unsolved 4x4 puzzles
- incompPuzzle1 = [[1,0,0,2],[0,3,0,0],[0,0,2,0],[3,0,0,4]]
- incompPuzzle2 = [[4,0,2,0],[0,1,0,0],[0,0,3,0],[0,4,0,2]]
- incompPuzzle3 = [[1,0,4,0],[0,2,0,0],[0,0,3,0],[0,1,0,4]]
- incompPuzzle4 = [[1,0,3,0],[0,0,0,2],[4,0,0,0],[0,1,0,3]]
- incompPuzzle5 = [[0,4,0,2],[0,0,3,0],[0,1,0,0],[4,0,2,0]]
- #solved 4x4s
- compPuzzle1 = [[1,4,3,2],[2,3,4,1],[4,1,2,3],[3,2,1,4]]
- compPuzzle2 = [[4,3,2,1],[2,1,4,3],[1,2,3,4],[3,4,1,2]]
- compPuzzle3 = [[1,3,4,2],[4,2,1,3],[2,4,3,1],[3,1,2,4]]
- compPuzzle4 = [[1,2,3,4],[3,4,1,2],[4,3,2,1],[2,1,4,3]]
- compPuzzle5 = [[3,4,1,2],[1,2,3,4],[2,1,4,3],[4,3,2,1]]
- def randomPuzzle():
- # generates a random int that corresponds with one of the incomplete puzzles above and then returns a global matrix out of that incomplete puzzle that will be where the values entered by the user can be stored without altering the original incomplete matrix
- x = random.randint(1,6)
- if x == 1:
- global workingMatrix = incompPuzzle1[:]
- return workingMatrix
- if x == 2:
- global workingMatrix = incompPuzzle2[:]
- return workingMatrix
- if x == 3:
- global workingMatrix = incompPuzzle3[:]
- return workingMatrix
- if x == 4:
- global workingMatrix = incompPuzzle1[:]
- return workingMatrix
- if x == 5:
- global workingMatrix = incompPuzzle1[:]
- return workingMatrix
- def drawGrid():
- x = 300
- y = 300
- l = 300
- turtle.right(90) #vertical lines drawn first so turtle must be turned to the right
- for i in xrange(5):
- for j in xrange(5):
- if j%2==0: # even numbered lines are thicker, so frame and subgrids are thicker
- turtle.pensize(10)
- turtle.penup()
- turtle.goto(x,y)
- turtle.down()
- turtle.forward(l)
- x = x - 0.2x
- else
- turtle.penup()
- turtle.goto(x,y)
- turtle.down()
- turtle.forward(l)
- x = x - 0.2x
- turtle.left(90) #returns to facing right from before
- if i%2 == 0: #thicker lines for even numbered iterations
- turtle.pensize(10)
- turtle.penup()
- turtle.goto(x,y)
- turtle.down()
- turtle.forward(l)
- y = y - 0.2y
- else
- turtle.penup()
- turtle.goto(x,y)
- turtle.down()
- turtle.forward(l)
- y = y - 0.2y
- turtle.goto()
- turtle.write(workingMatrix[i][j], move=False, align="left", font=("Arial", 28, "normal"))
- def fillKnownGrid():
- # take global matrix and use a for loop to populate grid with all non-zero elements
- for i in workingMatrix:
- for j in workingMatrix[i]:
- if workingMatrix[i][j] != 0:
- turtle.goto(37.5 + j*75, 262.5 - i*75)
- turtle.write(workingMatrix[i][j], move=False, align="left", font=("Arial", 28, "normal"))
- def getLocationInput():
- str userColumnEntry =
- def getNumInput():
- int userGuess = turtle.numiput("Sudoku", "Enter your guess: " minval = 1, maxval = 4);
- return userGuess
- def isValidInputrealtime():
- if getNumInput() > 4 or getNumInput() < 1:
- return false
- # checks row for duplicates
- for el in workingMatrix
- if getNumInput()
- def interface():
- drawGrid()
- randomPuzzle()
- fillKnownGrid()
- playSudoku()
- # prompt whether user wants to play in real time; if user wants to play in real time display rules and prompt
- str decide = turtle.textinput("Play in realtime?", "if you would like to play in real time, please press the 'y' key, if not press any other key:")
- if decide == y:
- else:
- interface()
- prompt whether user wants to play in real time
- if user wants to play in real time
- display rules and prompt
Add Comment
Please, Sign In to add comment