Advertisement
here2share

# basic_valid_sudoku_gen2.py

Nov 25th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. # basic_valid_sudoku_gen2.py
  2.  
  3. import random
  4. import time
  5.  
  6. level = (36, 30, 27, 24, 21, 18) # (0 to 5) from easy to expert
  7.      
  8. aaa='abcdefghxdgfxhabcehxecgbfdafedgxhabcxhgabcefdbcaefdxgheahbdgcxfgdxfcehabcfbhaxdeg'
  9. bbb='..X.X.X..X.X......X.........X..X..X........XX........X.X....X..X..X........X.....'
  10. grid=range(81)
  11. numbers=range(1,10)
  12.  
  13. hints = level[5]
  14. def progress(z):
  15.     for i in range(9):
  16.         print
  17.         for ii in range(9):
  18.             print z[i*9+ii],
  19.     print
  20.     print
  21. def shfl(z):
  22.     z=[[z[i*9+ii] for i in range(9)] for ii in range(9)]
  23.     z=[z[0:3],z[3:6],z[6:9]]
  24.     for i in range(3):
  25.         random.shuffle(z[i])
  26.     random.shuffle(z)
  27.     return sum(sum(z,[]),[])
  28. def init():
  29.     sq=grid
  30.     random.shuffle(numbers) # for an added measure
  31.     for i in range(numbers[-1]):
  32.         sq=shfl(sq)
  33.     a=b=''
  34.     for i in sq:
  35.         a+=aaa[i]
  36.         b+=bbb[i]
  37.     for i in range(9):
  38.         tmp=str(numbers[i])
  39.         a=a.replace(a[i],tmp)
  40.         b=b.replace(a[i],tmp)
  41.     random.shuffle(sq)
  42.     b=list(b)
  43.     for i in sq:
  44.         if b[i] is 'X': b[i]=a[i]
  45.         elif 81-b.count('.') < hints: b[i]=a[i]
  46.     progress(a)
  47.     progress(b)
  48.     print 81-b.count('.'), 'hints'
  49.     print
  50.     print ''.join(b)
  51. #
  52. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement