Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

Sudoku.py

By: agnishom on Jul 29th, 2012  |  syntax: Python  |  size: 6.05 KB  |  views: 148  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2. def initiate():
  3.     box.append([0, 1, 2, 9, 10, 11, 18, 19, 20])
  4.     box.append([3, 4, 5, 12, 13, 14, 21, 22, 23])
  5.     box.append([6, 7, 8, 15, 16, 17, 24, 25, 26])
  6.     box.append([27, 28, 29, 36, 37, 38, 45, 46, 47])
  7.     box.append([30, 31, 32, 39, 40, 41, 48, 49, 50])
  8.     box.append([33, 34, 35, 42, 43, 44, 51, 52, 53])
  9.     box.append([54, 55, 56, 63, 64, 65, 72, 73, 74])
  10.     box.append([57, 58, 59, 66, 67, 68, 75, 76, 77])
  11.     box.append([60, 61, 62, 69, 70, 71, 78, 79, 80])
  12.     for i in range(0, 81, 9):
  13.         row.append(range(i, i+9))
  14.     for i in range(9):
  15.         column.append(range(i, 80+i, 9))
  16.  
  17. def valid(n, pos):
  18.     current_row = pos/9
  19.     current_col = pos%9
  20.     current_box = (current_row/3)*3 + (current_col/3)
  21.     for i in row[current_row]:
  22.         if (grid[i] == n):
  23.             return False
  24.     for i in column[current_col]:
  25.         if (grid[i] == n):
  26.             return False
  27.     for i in box[current_box]:
  28.         if (grid[i] == n):
  29.             return False
  30.     return True
  31.  
  32. def solve():
  33.     i = 0
  34.     proceed = 1
  35.     diff = 0.0
  36.     while(i < 81):
  37.         if given[i]:
  38.             if proceed:
  39.                     i += 1
  40.             else:
  41.                 i -= 1
  42.         else:
  43.             n = grid[i]
  44.             prev = grid[i]
  45.             while(n < 9):
  46.               if (n < 9):
  47.                   n += 1
  48.               if valid(n, i):
  49.                   grid[i] = n
  50.                   proceed = 1
  51.                   break
  52.             if (grid[i] == prev):
  53.                grid[i] = 0
  54.                proceed = 0
  55.             if proceed:
  56.                i += 1
  57.             else:
  58.                i -=1
  59.                diff = diff + 1.0
  60.     return diff
  61. def gotonext():
  62.         global now_selected
  63.         now_selected = (now_selected+1) % 81
  64. def checkall():
  65.         for i in xrange(81):
  66.                 if not valid(grid[i],i):
  67.                         return False
  68.         return True
  69.  
  70. grid = [0]*81
  71. given = [False]*81
  72. box = []
  73. row = []
  74. column = []
  75. initiate()
  76. diff = 0.0
  77. import pygame
  78. import sys
  79. pygame.init()
  80. size = 600, 732
  81. black = 0, 0, 0
  82. screen = pygame.display.set_mode(size)
  83. pygame.display.set_caption('Sudoku Solver')
  84. template = pygame.image.load("SUDOKU.jpg")
  85. rect = template.get_rect()
  86. font = pygame.font.Font('Font.ttf', 48)
  87. now_selected = 0
  88. running = True
  89. while running:
  90.     screen.blit(template, rect)
  91.     for i in range(81):
  92.         if grid[i]:
  93.                 if given[i]:
  94.                         color = (0,0,255)
  95.                 else:
  96.                         color = (0,0,0)
  97.                 text = font.render(str(grid[i]), 1, color)
  98.                 ro = i/9
  99.                 col = i%9
  100.                 textpos = text.get_rect(centerx = col*66 + 33, centery = ro*66 + 33)
  101.                 screen.blit(text, textpos)
  102.     try:
  103.                 screen.blit(font.render('_', 1, (150,150,150)), font.render('O', 1, (100,100,100)).get_rect(centerx = (now_selected%9)*66 + 33, centery = (now_selected/9)*66 + 33))
  104.     except:
  105.                 pass
  106.     for event in pygame.event.get():
  107.         if event.type == pygame.QUIT:
  108.             pygame.quit()
  109.         elif event.type == pygame.MOUSEBUTTONDOWN:
  110.                 x,y = pygame.mouse.get_pos()
  111.                 col_edit = x/66
  112.                 ro_edit = y/66
  113.                 last_selected = now_selected
  114.                 now_selected = ro_edit*9 + col_edit
  115.                 if (now_selected > 80):
  116.                         gotonext()
  117.         elif event.type == pygame.KEYDOWN and (event.key == pygame.K_1 or event.key == pygame.K_KP1) :
  118.             if valid(1,now_selected): grid[now_selected] = 1;given[now_selected]=True;gotonext()
  119.         elif event.type == pygame.KEYDOWN and (event.key == pygame.K_2 or event.key == pygame.K_KP2) :
  120.             if valid(2,now_selected): grid[now_selected] = 2;given[now_selected]=True;gotonext()
  121.         elif event.type == pygame.KEYDOWN and (event.key == pygame.K_3 or event.key == pygame.K_KP3) :            
  122.             if valid(3,now_selected): grid[now_selected] = 3;given[now_selected]=True;gotonext()            
  123.         elif event.type == pygame.KEYDOWN and (event.key == pygame.K_4 or event.key == pygame.K_KP4)  :
  124.             if valid(4,now_selected): grid[now_selected] = 4;given[now_selected]=True;gotonext()          
  125.         elif event.type == pygame.KEYDOWN and (event.key == pygame.K_5 or event.key == pygame.K_KP5) :
  126.             if valid(5,now_selected): grid[now_selected] = 5;given[now_selected]=True;gotonext()          
  127.         elif event.type == pygame.KEYDOWN and (event.key == pygame.K_6 or event.key == pygame.K_KP6) :
  128.             if valid(6,now_selected): grid[now_selected] = 6;given[now_selected]=True;gotonext()          
  129.         elif event.type == pygame.KEYDOWN and (event.key == pygame.K_7 or event.key == pygame.K_KP7) :          
  130.             if valid(7,now_selected): grid[now_selected] = 7;given[now_selected]=True;gotonext()        
  131.         elif event.type == pygame.KEYDOWN and (event.key == pygame.K_8 or event.key == pygame.K_KP8) :
  132.             if valid(8,now_selected): grid[now_selected] = 8;given[now_selected]=True;gotonext()          
  133.         elif event.type == pygame.KEYDOWN and (event.key == pygame.K_9 or event.key == pygame.K_KP9) :
  134.             if valid(9,now_selected): grid[now_selected] = 9;given[now_selected]=True;gotonext()
  135.         elif event.type == pygame.KEYDOWN and (event.key == pygame.K_0 or event.key == pygame.K_BACKSPACE) :
  136.             grid[now_selected] = 0;given[now_selected]=False;
  137.         elif event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
  138.                 now_selected = (now_selected-9) % 81
  139.         elif event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
  140.                 now_selected = (now_selected+9) % 81
  141.         elif event.type == pygame.KEYDOWN and (event.key == pygame.K_RIGHT or event.key == pygame.K_TAB):
  142.                 now_selected = (now_selected+1) % 81
  143.         elif event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
  144.                 now_selected = (now_selected-1) % 81
  145.         elif event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
  146.                 for i in xrange(81):
  147.                         if not given[i]:
  148.                                 grid[i]=0
  149.                 solve()
  150.         elif event.type == pygame.KEYDOWN and event.key == pygame.K_DELETE:
  151.                 grid = [0]*81
  152.                 given = [False]*81
  153.         elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
  154.                 running = False
  155.     try:
  156.         pygame.display.update()
  157.     except:
  158.         pass
  159. pygame.quit()