Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # basic_valid_sudoku_gen2.py
- import random
- import time
- level = (36, 30, 27, 24, 21, 18) # (0 to 5) from easy to expert
- aaa='abcdefghxdgfxhabcehxecgbfdafedgxhabcxhgabcefdbcaefdxgheahbdgcxfgdxfcehabcfbhaxdeg'
- bbb='..X.X.X..X.X......X.........X..X..X........XX........X.X....X..X..X........X.....'
- grid=range(81)
- numbers=range(1,10)
- hints = level[5]
- def progress(z):
- for i in range(9):
- print
- for ii in range(9):
- print z[i*9+ii],
- print
- print
- def shfl(z):
- z=[[z[i*9+ii] for i in range(9)] for ii in range(9)]
- z=[z[0:3],z[3:6],z[6:9]]
- for i in range(3):
- random.shuffle(z[i])
- random.shuffle(z)
- return sum(sum(z,[]),[])
- def init():
- sq=grid
- random.shuffle(numbers) # for an added measure
- for i in range(numbers[-1]):
- sq=shfl(sq)
- a=b=''
- for i in sq:
- a+=aaa[i]
- b+=bbb[i]
- for i in range(9):
- tmp=str(numbers[i])
- a=a.replace(a[i],tmp)
- b=b.replace(a[i],tmp)
- random.shuffle(sq)
- b=list(b)
- for i in sq:
- if b[i] is 'X': b[i]=a[i]
- elif 81-b.count('.') < hints: b[i]=a[i]
- progress(a)
- progress(b)
- print 81-b.count('.'), 'hints'
- print
- print ''.join(b)
- #
- init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement