Advertisement
dmveazey

Sudoku

Mar 22nd, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.92 KB | None | 0 0
  1. def quadfinder(find): # to figure out which quad to check for a position while solving
  2.     global quad_position
  3.     for quadcount in range(0, 9):
  4.         if find in quad_position[quadcount]:
  5.             return quadcount
  6.  
  7. print "Welcome to Daniel Veazey's Sudoku Solver 7."
  8. print "http://www.danielveazey.com"
  9. print "Please fill in the available numbers on the board,"
  10. print "starting on the top row (Row 1), going left to right."
  11. print "If a square is blank, put a 0 in its place."
  12. print "Separate each position with a comma and a space."
  13. print "Each position can be only one digit, 0 through 9."
  14. print "Example:"
  15. print "Row 1: 5, 6, 0, 0, 4, 1, 0, 0, 8"
  16. print "Row 2: 1, 0, 0, 0, 0, 9, 0, 7, 0"
  17. print "etc."
  18. print
  19. print "Let's get started."
  20. print
  21. print
  22.  
  23. ## ask the user to populate the board by rows
  24. row = [0, 1, 2, 3, 4, 5, 6, 7, 8]
  25. for x in range(0, 9):
  26.     row[x] = list(input("Row " + str(x+1) + ": "))
  27.  
  28. print
  29. print
  30. print "Original board:"
  31. for x in range (0, 9):
  32.     print row[x]
  33.  
  34. ## define and populate the columns by referring to the rows
  35. col = [0, 1, 2, 3, 4, 5, 6, 7, 8]
  36. for x in range (0, 9):
  37.     col[x] = [row[0][x], row[1][x], row[2][x], row[3][x], row[4][x], row[5][x], row[6][x], row[7][x], row[8][x]]
  38.  
  39. ## define and populate quads by referring to slices of rows
  40. quad = [0, 1, 2, 3, 4, 5, 6, 7, 8]
  41. quad[0] = row[0][0:3] + row[1][0:3] + row[2][0:3]
  42. quad[1] = row[0][3:6] + row[1][3:6] + row[2][3:6]
  43. quad[2] = row[0][6:] + row[1][6:] + row[2][6:]
  44. quad[3] = row[3][0:3] + row[4][0:3] + row[5][0:3]
  45. quad[4] = row[3][3:6] + row[4][3:6] + row[5][3:6]
  46. quad[5] = row[3][6:] + row[4][6:] + row[5][6:]
  47. quad[6] = row[6][0:3] + row[7][0:3] + row[8][0:3]
  48. quad[7] = row[6][3:6] + row[7][3:6] + row[8][3:6]
  49. quad[8] = row[6][6:] + row[7][6:] + row[8][6:]
  50.  
  51. ## populate list of single positions
  52. position = list()
  53. for z in range(0, 81):
  54.     for x in range(0, 9):
  55.         for y in range(0, 9):
  56.             position.append(row[x][y])
  57.  
  58. ## put list of possible answers in unsolved single positions
  59. for x in range(0, 81):
  60.     if position[x] == 0:
  61.         position[x] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  62.  
  63. ## define which positions are in which quads
  64. quad_position = [0, 1, 2, 3, 4, 5, 6, 7, 8]
  65. quad_position[0] = [0, 1, 2, 9, 10, 11, 18, 19, 20]
  66. quad_position[1] = [3, 4, 5, 12, 13, 14, 21, 22, 23]
  67. quad_position[2] = [6, 7, 8, 15, 16, 17, 24, 25, 26]
  68. quad_position[3] = [27, 28, 29, 36, 37, 38, 45, 46, 47]
  69. quad_position[4] = [30, 31, 32, 39, 40, 41, 48, 49, 50]
  70. quad_position[5] = [33, 34, 35, 42, 43, 44, 51, 52, 53]
  71. quad_position[6] = [54, 55, 56, 63, 64, 65, 72, 73, 74]
  72. quad_position[7] = [57, 58, 59, 66, 67, 68, 75, 76, 77]
  73. quad_position[8] = [60, 61, 62, 69, 70, 71, 78, 79, 80]
  74.  
  75. ################# SOLVING ######################################
  76. ## this program uses only a very simple method to solve the puzzle,
  77. ## just checking to see if a possible answer for a position
  78. ## is already in its row, column or quad. if so, the possible answer
  79. ## is eliminated from the list of possible answers.
  80.  
  81. did_change = True
  82. while did_change == True:
  83.     did_change = False
  84.     for x in range(0, 81):
  85.         if type(position[x]) == list:
  86.             y = 0
  87.             while type(position[x]) == list and y < len(position[x]): # to only keep checking if there are more items in the list of possible answers
  88.                 # check to see if the possible answer is already in a position in the row, column or quad
  89.                 if position[x][y] in row[x/9] or position[x][y] in col[x%9] or position[x][y] in quad[quadfinder(x)]: # quadfinder tells us which quad to check
  90.                     del position[x][y] ## delete the possible answer if it's found elsewhere
  91.                     did_change = True ## to keep the loop going
  92.                     if len(position[x]) == 1: ## if list of possible answers is only one item long,
  93.                         position[x] = position[x][0] ## remove the nested list
  94.                         row[x/9][x%9] = position[x] ## and copy the answer to the rows,
  95.                         col[x%9][x/9] = position[x] ## columns,
  96.                         for quad_count in range(0, 9): ## and quads
  97.                             if x in quad_position[quad_count]: ## similar to function quadfinder(), but used on its own here
  98.                                 for z in range(0, 9):
  99.                                     if x == quad_position[quad_count][z]:
  100.                                         quad[quad_count][z] = position[x]
  101.                 else: y = y + 1 # to check the next item in the list of possible answers
  102.  
  103. ## additional methods will go here in the future
  104.  
  105. ## find out if the puzzle was solved:
  106. solved = True
  107. for x in range(0, 81):
  108.     if type(position[x]) == list:
  109.         solved = False
  110.  
  111. if solved == False:
  112.     print
  113.     print
  114.     print "Here is a list of positions with either"
  115.     print "the correct answers or"
  116.     print "a list of possible correct answers:"
  117.     for x in range(0, 81):
  118.         print str(x+1) + ': ' + str(position[x])
  119.  
  120.     ## also print board by rows
  121.     print
  122.     print
  123.     print "This is as far as I got using the simple method Scroll up"
  124.     print "to see a list of possible answers for each position on the board."
  125.     for x in range (0, 9):
  126.         print row[x]
  127. else:
  128.     print
  129.     print
  130.     print "I SOLVED IT!!!"
  131.     for x in range (0, 9):
  132.         print row[x]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement