This code is developed for the beginners in python.when compared with other languages python is most flexible.When we code the Sudoku we need to be consider three things.
1.nine 3 X 3 matrices
2.nine column
3.nine rows.
Sudoku game will contain a total of 81 terms,and the terms should be in the range of 1 to 9 .There is no repetition allowed in the 3 above mentioned areas. The code given below is to generate
a random Sudoku.it will satisfies the 1 and 3 terms and partially the 2.You need to be edit the code to obtain the 2 term.If you need any help comment below.
CODE
import random //include random functionalities
list1=[] //dynamic declaration of list 'list 1
list2=[]
list3=[]
arg=[]
arga=[]
arg3=[]
arg4=[]
arg5=[]
arg6=[]
arg7=[]
arg8=[]
arg9=[]
c1=[];c2=[];c3=[];c4=[];c5=[];c6=[];c7=[];c8=[];c9=[] //can use semicolon to bind two lines in a single line
r=random.randrange(1,10,1) //which will generate a random no from 1(first arg) to 9(second arg)
def asad(arg1,arg2) : //function definition
a=arg1;b=arg2
r=random.randrange(1,10,1)
flag=2
while flag==1 or flag ==2 :
if r in list1 :
flag=1
r=random.randrange(1,10,1)
else :
flag=0
if b==1 :
flag3=2
while flag3==1 or flag3 ==2 :
if r in c1 :
flag3=1
r=random.randrange(1,10,1)
else :
flag3=0
c1.append(r) // to append variable 'r' to a list 'c1'
list1.append(r)
if a==1 :
arg.append(r)
elif a==2 :
arga.append(r)
elif a==3 :
arg3.append(r)
elif a==4 :
arg4.append(r)
elif a==5 :
arg5.append(r)
elif a==6 :
arg6.append(r)
elif a==7 :
arg7.append(r)
elif a==8 :
arg8.append(r)
elif a==9 :
arg9.append(r)
if b==2 :
flag3=2
while flag3==1 or flag3 ==2 :
if r in c2 :
flag3=1
r=random.randrange(1,10,1)
else :
flag3=0
c2.append(r)
list1.append(r)
if a==1 :
arg.append(r)
elif a==2 :
arga.append(r)
elif a==3 :
arg3.append(r)
elif a==4 :
arg4.append(r)
elif a==5 :
arg5.append(r)
elif a==6 :
arg6.append(r)
elif a==7 :
arg7.append(r)
elif a==8 :
arg8.append(r)
elif a==9 :
arg9.append(r)
if b==3 :
flag3=2
while flag3==1 or flag3 ==2 :
if r in c3 :
flag3=1
r=random.randrange(1,10,1)
else :
flag3=0
c3.append(r)
list1.append(r)
if a==1 :
arg.append(r)
elif a==2 :
arga.append(r)
elif a==3 :
arg3.append(r)
elif a==4 :
arg4.append(r)
elif a==5 :
arg5.append(r)
elif a==6 :
arg6.append(r)
elif a==7 :
arg7.append(r)
elif a==8 :
arg8.append(r)
elif a==9 :
arg9.append(r)
if b==4 :
flag3=2
while flag3==1 or flag3 ==2 :
if r in c4 :
flag3=1
r=random.randrange(1,10,1)
else :
flag3=0
c4.append(r)
list1.append(r)
if a==1 :
arg.append(r)
elif a==2 :
arga.append(r)
elif a==3 :
arg3.append(r)
elif a==4 :
arg4.append(r)
elif a==5 :
arg5.append(r)
elif a==6 :
arg6.append(r)
elif a==7 :
arg7.append(r)
elif a==8 :
arg8.append(r)
elif a==9 :
arg9.append(r)
def asad1(arg1,arg2) :
a=arg1;b=arg2
r=random.randrange(1,10,1)
flag=2
while flag==1 or flag ==2 :
if r in list2 :
flag=1
r=random.randrange(1,10,1)
else :
flag=0
if a==1 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arg :
flag1=1
r=random.randrange(1,10,1)
else :
list2.append(r)
arg.append(r)
flag1=0
elif a==2 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arga :
flag1=1
r=random.randrange(1,10,1)
else :
list2.append(r)
arga.append(r)
flag1=0
elif a==3 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arg3 :
flag1=1
r=random.randrange(1,10,1)
else :
list2.append(r)
arg3.append(r)
flag1=0
elif a==4 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arg4 :
flag1=1
r=random.randrange(1,10,1)
else :
list2.append(r)
arg4.append(r)
flag1=0
elif a==5 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arg5 :
flag1=1
r=random.randrange(1,10,1)
else :
list2.append(r)
arg5.append(r)
flag1=0
elif a==6 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arg6 :
flag1=1
r=random.randrange(1,10,1)
else :
list2.append(r)
arg6.append(r)
flag1=0
elif a==7 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arg7 :
flag1=1
r=random.randrange(1,10,1)
else :
list2.append(r)
arg7.append(r)
flag1=0
elif a==8 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arg8 :
flag1=1
r=random.randrange(1,10,1)
else :
list2.append(r)
arg8.append(r)
flag1=0
elif a==9 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arg9 :
flag1=1
r=random.randrange(1,10,1)
else :
list2.append(r)
arg9.append(r)
flag1=0
def asad2(arg1,arg2) :
r=random.randrange(1,10,1)
a=arg1
flag=2
while flag==1 or flag ==2 :
if r in list3 :
flag=1
r=random.randrange(1,10,1)
else :
flag=0
if a==1 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arg :
flag1=1
r=random.randrange(1,10,1)
else :
list3.append(r)
arg.append(r)
flag1=0
elif a==2 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arga :
flag1=1
r=random.randrange(1,10,1)
else :
list3.append(r)
arga.append(r)
flag1=0
elif a==3 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arg3 :
flag1=1
r=random.randrange(1,10,1)
else :
list3.append(r)
arg3.append(r)
flag1=0
elif a==4 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arg4 :
flag1=1
r=random.randrange(1,10,1)
else :
list3.append(r)
arg4.append(r)
flag1=0
elif a==5 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arg5 :
flag1=1
r=random.randrange(1,10,1)
else :
list3.append(r)
arg5.append(r)
flag1=0
elif a==6 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arg6 :
flag1=1
r=random.randrange(1,10,1)
else :
list3.append(r)
arg6.append(r)
flag1=0
elif a==7 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arg7 :
flag1=1
r=random.randrange(1,10,1)
else :
list3.append(r)
arg7.append(r)
flag1=0
elif a==8 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arg8 :
flag1=1
r=random.randrange(1,10,1)
else :
list3.append(r)
arg8.append(r)
flag1=0
elif a==9 :
flag1=2
while flag1==1 or flag1 ==2 :
if r in arg9 :
flag1=1
r=random.randrange(1,10,1)
else :
list3.append(r)
arg9.append(r)
flag1=0
i=1;j=1
for i in range(1,4) :
for j in range(1,4) :
asad(i,j)
for i in range(1,4) :
for j in range(4,7) :
asad1(i,j)
for i in range(1,4) :
for j in range(7,10) :
asad2(i,j)
print list1[0:3],"\t",list2[0:3],"\t",list3[0:3]
print list1[3:6],"\t",list2[3:6],"\t",list3[3:6]
print list1[6:9],"\t",list2[6:9],"\t",list3[6:9]
print "\n"
del list1[:]
del list2[:]
del list3[:]
for i in range(4,7) :
for j in range(1,4) :
asad(i,j)
for i in range(4,7) :
for j in range(4,7) :
asad1(i,j)
for i in range(4,7) :
for j in range(7,10) :
asad2(i,j)
print list1[0:3],"\t",list2[0:3],"\t",list3[0:3]
print list1[3:6],"\t",list2[3:6],"\t",list3[3:6]
print list1[6:9],"\t",list2[6:9],"\t",list3[6:9]
print "\n"
del list1[:]
del list2[:]
del list3[:]
for i in range(7,10) :
for j in range(1,4) :
asad(i,j)
for i in range(7,10) :
for j in range(4,7) :
asad1(i,j)
for i in range(7,10) :
for j in range(7,10) :
asad2(i,j)
print list1[0:3],"\t",list2[0:3],"\t",list3[0:3]
print list1[3:6],"\t",list2[3:6],"\t",list3[3:6]
print list1[6:9],"\t",list2[6:9],"\t",list3[6:9]
print "\n"
In Linux save the code with an extension .py .can run with the following syntax python program name.py
HAPPY CODING
No comments:
Post a Comment