welcom ! Handel home
2016年7月24日 星期日
python sudoku plan # [sudoku_tablex1.py]
=======================================================
[sudoku_tablex1.py] --題庫(demo_1)Easy ,(demo_2)Normal,(demo_3)Hard
#============================
demo_1=[
[[7,0,1,0,2,0,0,0,0],#e001
[0,0,0,0,5,3,9,0,1],
[9,3,5,1,7,6,0,2,4],
[1,6,0,9,8,0,3,0,0],
[3,2,9,0,4,0,5,6,8],
[0,0,4,0,3,2,0,9,7],
[4,9,0,3,1,7,2,8,5],
[5,0,8,2,9,0,0,0,0],
[0,0,0,0,6,0,4,0,9] ],
[[0,0,5,0,9,0,7,0,4],#e002
[0,7,4,3,0,1,6,0,0],
[0,8,0,7,0,4,5,0,2],
[7,4,0,9,3,0,2,0,1],
[9,5,2,0,0,0,4,6,3],
[3,0,1,0,4,2,0,7,8],
[4,0,3,8,0,5,0,2,0],
[0,0,7,6,0,9,3,4,0],
[5,0,6,0,1,0,8,0,0] ],
[[6,0,0,2,9,0,0,0,1],#e003
[3,0,0,0,0,6,9,0,0],
[9,1,0,0,3,0,4,0,0],
[0,7,0,9,0,0,6,0,0],
[0,2,0,7,0,3,0,4,0],
[0,0,9,0,0,2,0,5,0],
[0,0,8,0,5,0,0,9,4],
[0,0,5,6,0,0,0,0,8],
[1,0,0,0,8,7,0,0,5] ],
[[0,4,0,0,0,0,7,0,1],#e004
[0,0,8,0,0,2,3,0,0],
[0,6,1,9,4,0,0,0,0],
[0,0,0,5,0,0,2,3,0],
[8,0,0,0,9,0,0,0,4],
[0,2,5,0,0,1,0,0,0],
[0,0,0,0,5,7,9,6,0],
[0,0,4,3,0,0,1,0,0],
[6,0,9,0,0,0,0,5,0] ],
[[1,0,5,0,0,0,0,0,0],#e005
[0,0,0,2,0,0,0,6,8],
[0,0,8,0,3,0,0,4,0],
[2,5,0,0,8,1,0,0,0],
[4,0,6,0,0,0,8,0,2],
[0,0,0,6,4,0,0,5,3],
[0,7,0,0,5,0,1,0,0],
[6,1,0,0,0,7,0,0,0],
[0,0,0,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0,3,0,0,7,0,0],
[7,0,6,5,0,0,0,0,0] ],
[[2,0,0,9,6,0,0,1,0],#h008
[0,3,1,0,0,0,0,0,0],
[0,0,0,0,3,0,0,0,5],
[0,5,0,0,0,0,6,0,0],
[0,0,9,3,0,7,2,0,0],
[0,0,3,0,0,0,0,4,0],
[3,0,0,0,9,0,0,0,0],
[0,0,0,0,0,0,8,2,0],
[0,8,0,0,4,1,0,0,7] ],
[[8,0,0,0,0,7,0,0,9],#h009
[4,0,6,0,2,0,0,0,0],
[0,5,0,3,0,0,2,0,0],
[0,0,7,2,0,0,0,0,0],
[0,6,0,0,0,0,0,9,0],
[0,0,0,0,0,1,3,0,0],
[0,0,8,0,0,6,0,4,0],
[0,0,0,0,5,0,1,0,8],
[6,0,0,8,0,0,0,0,5] ],
[[0,0,0,0,6,0,0,3,0],#h010
[0,4,5,0,0,9,0,0,8],
[0,0,0,0,0,1,0,0,6],
[0,0,2,5,0,0,0,8,0],
[0,0,9,0,0,0,6,0,0],
[0,6,0,0,0,7,1,0,0],
[4,0,0,1,0,0,0,0,0],
[9,0,0,6,0,0,3,4,0],
[0,1,0,0,7,0,0,0,0] ]
]
=======================================================
python sudoku plan [sudoku_publib.py]
=======================================================
[sudoku_publib.py] --class Num and Class sudoku_data array
# coding=utf-8
#=== pyt268104 home work ===
# 20160627
#import math
#import numpy as np
#v1.1
class Num:
arealoop=[[1,3,1,3],[1,3,4,6],[1,3,7,9],
[4,6,1,3],[4,6,4,6],[4,6,7,9],
[7,9,1,3],[7,9,4,6],[7,9,7,9]
]
areacode=0
chkno=[]
no=0
pos=00
status = 0 #0:user edit ,1:is mask can no edit
chkfg=0
def __init__(self, pos, no, status):
self.pos=pos
self.no=no
self.status = status
self.chkfg=0
self.chkno=[1,2,3,4,5,6,7,8,9]
def setpos(self,pos):
px=eval(str(pos)[1])
py=eval(str(pos)[0])
if 1<= px <=9:
if 1<= py <=9:
#self.pos = pos
if 1<=py<=3:
if 1<=px<=3:
self.areacode=1
elif 4<=px<=6:
self.areacode=2
elif 7<=px<=9:
self.areacode=3
elif 4<=py<=6:
if 1<=px<=3:
self.areacode=4
elif 4<=px<=6:
self.areacode=5
elif 7<=px<=9:
self.areacode=6
elif 7<=py<=9:
if 1<=px<=3:
self.areacode=7
elif 4<=px<=6:
self.areacode=8
elif 7<=px<=9:
self.areacode=9
def maskno(self,no):
if self.status==1:#mask no
self.no=no
def setno(self,no):
if self.status==0:#user edit no
self.no = no
def scanchk(self,xx):
self.chkfg=0
fg1=0
fg2=0
fg3=0
fg1=self.area_scanchk(xx)
fg2=self.col_scanchk(xx)
fg3=self.row_scanchk(xx)
self.chkfg|=fg1 or fg2 or fg3
pass
def area_scanchk(self,xx):
fg=0
#py=int(self.pos/10)
#px=self.pos%10
self.setpos(self.pos)
xacode=self.areacode
y1,y2,x1,x2 = self.arealoop[self.areacode-1]
y,x = (y1-1),(x1-1)
for y in range(y1-1,y2):
for x in range(x1-1,x2):
tt=(y+1)*10+x+1
if tt!=self.pos:
nn = xx[y][x].no
if nn in self.chkno:
self.chkno.remove(nn)
if nn!=0:
if nn==self.no:
fg=1# fthe game check fail
return fg
pass
def col_scanchk(self,xx):#col y 列
fg=0
#px=eval(str(pos)[1])#row
py=eval(str(self.pos)[0])#col
y=py-1
for x in range(1-1,9):
tt=(y+1)*10+x+1
if tt!=self.pos:
nn = xx[y][x].no
if nn in self.chkno:
self.chkno.remove(nn)
if nn!=0:
if nn==self.no:
fg=1# fthe game check fail
return fg
pass
def row_scanchk(self,xx):#row x 行
fg=0
px=eval(str(self.pos)[1])#row
#py=eval(str(pos)[0])#col
x=px-1
#print("x=",x)
for y in range(1-1,9):
#print("y=",y)
tt=(y+1)*10+x+1
if tt!=self.pos:
nn = xx[y][x].no
if nn in self.chkno:
self.chkno.remove(nn)
if nn!=0:
if nn==self.no:
fg=1# fthe game check fail
return fg
pass
class sudoku_dataarray:
gname=""
sudokudat = []
chkfg=0
runfg=0
def __init__(self,name):
self.gname = name
self.sudokudat = [[0 for i in range(9)] for j in range(9)]
for i in range(9):
for j in range(9):
pos = (i+1)*10+(j+1)
self.sudokudat[i][j] = Num(pos,0,0)
self.chkfg=0
self.runfg=0
pass
def showsudoku(self):
#self.chkfg=0
#print()
print("="*15,"sudoku data","="*15)
for i in range(9):
for j in range(9):
if self.sudokudat[i][j].status==1:
print("($%1s)"%(self.sudokudat[i][j].no),end="")#mask
else:
print("[%2s]"%(self.sudokudat[i][j].no),end="")#free edit
#self.sudokudat[i][j].scanchk(self.sudokudat)
#self.chkfg+=self.sudokudat[i][j].chkfg
if j in (2,5):
print("||",end="")
print()
if i in (2,5):
print(("="*12)+"||"+("="*12)+"||"+("="*12))
print("="*42)
pass
def checksudoku(self):
self.chkfg=0
self.runfg=0
for i in range(9):
for j in range(9):
if self.sudokudat[i][j].status==0:
self.sudokudat[i][j].scanchk(self.sudokudat)
self.chkfg|=self.sudokudat[i][j].chkfg
if self.sudokudat[i][j].no==0:
self.runfg+=1
#return self.chkfg
def setsudoku_col(self,nn,ll):
tt=0
for j in range(9):
self.sudokudat[nn-1][j].no=ll[tt]
if ll[tt]!=0:
self.sudokudat[nn-1][j].status=1 # mask data
else:
self.sudokudat[nn-1][j].status=0 # user data
tt+=1
pass
def setsudoku_plan(self,tab):
tt=0
for i in range(9):
for j in range(9):
self.sudokudat[i][j].no = tab[i][j]
nn = tab[i][j]
if self.sudokudat[i][j].no!=0:
self.sudokudat[i][j].status=1 # mask data
else:
self.sudokudat[i][j].status=0 # user data
stt=self.sudokudat[i][j].status
self.sudokudat[i][j].chkno=[1,2,3,4,5,6,7,8,9]
pp = (i+1)*10+(j+1)
self.sudokudat[i][j].setpos(pp)
#xx = [[0 for i in range(9)] for j in range(9)]
#split the pos by col and row
#k1=eval(str(s)[0])
#k2=eval(str(s)[1])
=======================================================
[sudoku_publib.py] --class Num and Class sudoku_data array
# coding=utf-8
#=== pyt268104 home work ===
# 20160627
#import math
#import numpy as np
#v1.1
class Num:
arealoop=[[1,3,1,3],[1,3,4,6],[1,3,7,9],
[4,6,1,3],[4,6,4,6],[4,6,7,9],
[7,9,1,3],[7,9,4,6],[7,9,7,9]
]
areacode=0
chkno=[]
no=0
pos=00
status = 0 #0:user edit ,1:is mask can no edit
chkfg=0
def __init__(self, pos, no, status):
self.pos=pos
self.no=no
self.status = status
self.chkfg=0
self.chkno=[1,2,3,4,5,6,7,8,9]
def setpos(self,pos):
px=eval(str(pos)[1])
py=eval(str(pos)[0])
if 1<= px <=9:
if 1<= py <=9:
#self.pos = pos
if 1<=py<=3:
if 1<=px<=3:
self.areacode=1
elif 4<=px<=6:
self.areacode=2
elif 7<=px<=9:
self.areacode=3
elif 4<=py<=6:
if 1<=px<=3:
self.areacode=4
elif 4<=px<=6:
self.areacode=5
elif 7<=px<=9:
self.areacode=6
elif 7<=py<=9:
if 1<=px<=3:
self.areacode=7
elif 4<=px<=6:
self.areacode=8
elif 7<=px<=9:
self.areacode=9
def maskno(self,no):
if self.status==1:#mask no
self.no=no
def setno(self,no):
if self.status==0:#user edit no
self.no = no
def scanchk(self,xx):
self.chkfg=0
fg1=0
fg2=0
fg3=0
fg1=self.area_scanchk(xx)
fg2=self.col_scanchk(xx)
fg3=self.row_scanchk(xx)
self.chkfg|=fg1 or fg2 or fg3
pass
def area_scanchk(self,xx):
fg=0
#py=int(self.pos/10)
#px=self.pos%10
self.setpos(self.pos)
xacode=self.areacode
y1,y2,x1,x2 = self.arealoop[self.areacode-1]
y,x = (y1-1),(x1-1)
for y in range(y1-1,y2):
for x in range(x1-1,x2):
tt=(y+1)*10+x+1
if tt!=self.pos:
nn = xx[y][x].no
if nn in self.chkno:
self.chkno.remove(nn)
if nn!=0:
if nn==self.no:
fg=1# fthe game check fail
return fg
pass
def col_scanchk(self,xx):#col y 列
fg=0
#px=eval(str(pos)[1])#row
py=eval(str(self.pos)[0])#col
y=py-1
for x in range(1-1,9):
tt=(y+1)*10+x+1
if tt!=self.pos:
nn = xx[y][x].no
if nn in self.chkno:
self.chkno.remove(nn)
if nn!=0:
if nn==self.no:
fg=1# fthe game check fail
return fg
pass
def row_scanchk(self,xx):#row x 行
fg=0
px=eval(str(self.pos)[1])#row
#py=eval(str(pos)[0])#col
x=px-1
#print("x=",x)
for y in range(1-1,9):
#print("y=",y)
tt=(y+1)*10+x+1
if tt!=self.pos:
nn = xx[y][x].no
if nn in self.chkno:
self.chkno.remove(nn)
if nn!=0:
if nn==self.no:
fg=1# fthe game check fail
return fg
pass
class sudoku_dataarray:
gname=""
sudokudat = []
chkfg=0
runfg=0
def __init__(self,name):
self.gname = name
self.sudokudat = [[0 for i in range(9)] for j in range(9)]
for i in range(9):
for j in range(9):
pos = (i+1)*10+(j+1)
self.sudokudat[i][j] = Num(pos,0,0)
self.chkfg=0
self.runfg=0
pass
def showsudoku(self):
#self.chkfg=0
#print()
print("="*15,"sudoku data","="*15)
for i in range(9):
for j in range(9):
if self.sudokudat[i][j].status==1:
print("($%1s)"%(self.sudokudat[i][j].no),end="")#mask
else:
print("[%2s]"%(self.sudokudat[i][j].no),end="")#free edit
#self.sudokudat[i][j].scanchk(self.sudokudat)
#self.chkfg+=self.sudokudat[i][j].chkfg
if j in (2,5):
print("||",end="")
print()
if i in (2,5):
print(("="*12)+"||"+("="*12)+"||"+("="*12))
print("="*42)
pass
def checksudoku(self):
self.chkfg=0
self.runfg=0
for i in range(9):
for j in range(9):
if self.sudokudat[i][j].status==0:
self.sudokudat[i][j].scanchk(self.sudokudat)
self.chkfg|=self.sudokudat[i][j].chkfg
if self.sudokudat[i][j].no==0:
self.runfg+=1
#return self.chkfg
def setsudoku_col(self,nn,ll):
tt=0
for j in range(9):
self.sudokudat[nn-1][j].no=ll[tt]
if ll[tt]!=0:
self.sudokudat[nn-1][j].status=1 # mask data
else:
self.sudokudat[nn-1][j].status=0 # user data
tt+=1
pass
def setsudoku_plan(self,tab):
tt=0
for i in range(9):
for j in range(9):
self.sudokudat[i][j].no = tab[i][j]
nn = tab[i][j]
if self.sudokudat[i][j].no!=0:
self.sudokudat[i][j].status=1 # mask data
else:
self.sudokudat[i][j].status=0 # user data
stt=self.sudokudat[i][j].status
self.sudokudat[i][j].chkno=[1,2,3,4,5,6,7,8,9]
pp = (i+1)*10+(j+1)
self.sudokudat[i][j].setpos(pp)
#xx = [[0 for i in range(9)] for j in range(9)]
#split the pos by col and row
#k1=eval(str(s)[0])
#k2=eval(str(s)[1])
python sudoku plan # [ pyt268104_sudoku.py] --main code
由三個檔案組成
=======================================================[ pyt268104_sudoku.py] --主程式
# coding=utf-8
#=== pyt268104 home work ===
# 20160627
#import math
#import numpy as np
#def fun(a1,a2):
# c = np.dot(a1,a2)
# return c
from class_hw import sudoku_tablex1
from class_hw import sudoku_publib
level = 1
gcode = 1
pysudokuver = '''
This is Sudoku Game
Desiger: Handel Liao pyt268104
Codeing by Python3.5
Ver : 1.2 20160725
'''
lev=["Easy","Normal","Hard"]
mtxt = '''
=== Easy Sudoku Game V1.2 pyt268104 Handel ===
*** System Command ***
[1]Level Select (L1:Easy,L2:Normal,L3:Hard)
[2]Game code by LEVEL(1..10)
[3]Show Game Buffer and Status
[4]select play Game
[?]List Menu
[H]Help info show
[Q]quit
*** Game Command ***
[N]Add a New Game
[D]Delete a Game
[L]Load Game Data[from level,gcode]
[P]Print Game data array
[K]key in a data to Game
[C]check the Game by Pass
'''
def getsudoku():
xx = [[0 for i in range(9)] for j in range(9)]
return xx
def showsudoku(xx):
print()
print("="*15,"sudoku data","="*15)
for i in range(9):
for j in range(9):
print("[%2s]"%(xx[i][j]),end="")
if j in (2,5):
print("||",end="")
print()
if i in (2,5):
print(("="*12)+"||"+("="*12)+"||"+("="*12))
def setsudoku_area(xx,nn,ll):
tt=0
for j in range(9):
xx[nn-1][j]=ll[tt]
tt+=1
def setsudoku_plan(xx,tab):
tt=0
for i in range(9):
xx[i]=tab[i]
def scanok(yy):#c1: area ,col,raw must be 1 scan
for i in range(9):
for j in range(9):
if yy.sudokudat[i][j].status == 0:
yy.sudokudat[i][j].scanchk(yy.sudokudat)
runok=0
runfail=0
for i in range(9):
for j in range(9):
ll=len(yy.sudokudat[i][j].chkno)
print("%d%d = %s # %d"%(i+1,j+1,yy.sudokudat[i][j].chkno,ll))
if yy.sudokudat[i][j].no==0:
if ll==1:
yy.sudokudat[i][j].no = yy.sudokudat[i][j].chkno[0]
else:
runok+=1
if ll==0:
runfail+=1
return runok,runfail
def scanok2(yy):#c2: area mabe only 1 scan
pass
def main():
game=[]
level = 1
gcode = 1
ss="demo"+",("+lev[level-1]+"_"+str(gcode)+")"
game.append(sudoku_publib.sudoku_dataarray(ss))
game[0].setsudoku_plan(sudoku_tablex1.demo_1[gcode])
level = 2
gcode = 1
ss="demo1"+",("+lev[level-1]+"_"+str(gcode)+")"
game.append(sudoku_publib.sudoku_dataarray(ss))
game[1].setsudoku_plan(sudoku_tablex1.demo_2[gcode])
level = 3
gcode = 1
ss="demo2"+",("+lev[level-1]+"_"+str(gcode)+")"
game.append(sudoku_publib.sudoku_dataarray(ss))
game[2].setsudoku_plan(sudoku_tablex1.demo_3[gcode])
level = 1
gcode = 1
playindex = 0
cmm ='?'
while 1:
if cmm =='?':
print(mtxt)
elif cmm =='h':
print(pysudokuver)
elif cmm == 'q':
print()
print("************************")
print("** Thank You By By **")
print("************************")
print()
break
elif cmm == 'n':#add a new Game
ss = input("input the gmae name:")
ss+=",("+lev[level-1]+"_"+str(gcode)+")"
game.append(sudoku_publib.sudoku_dataarray(ss))
tt=0
for i in game:
print("[%d]: %s "%(tt,i.gname))
tt+=1
print("Add ok !")
print()
elif cmm == 'd': #delete
print("==================")
tt=0
for i in game:
print("[%d]: %s "%(tt,i.gname))
tt+=1
nn = eval(input("input index by Delete:"))
if nn>len(game)-1:
print("Sorry ! index out of range ")
print()
else:
del game[nn]
tt=0
for i in game:
print("[%d]: %s "%(tt,i.gname))
tt+=1
if playindex>=len(game):
playindex=len(game)-1
print("delete ok !")
print()
elif cmm == 'l':#load data to array
if level==1:
game[playindex].setsudoku_plan(sudoku_tablex1.demo_1[gcode])
elif level==2:
game[playindex].setsudoku_plan(sudoku_tablex1.demo_2[gcode])
elif level==3:
game[playindex].setsudoku_plan(sudoku_tablex1.demo_3[gcode])
ss1 = game[playindex].gname
ss = ss1.split(",")[0]
ss += ",("+lev[level-1]+"_"+str(gcode)+")"
game[playindex].gname = ss
print("===%d is [ %s ] ==="%(playindex,game[playindex].gname))
game[playindex].showsudoku()
elif cmm == "p":#show data array
print()
print("===%d is [ %s ] ==="%(playindex,game[playindex].gname))
game[playindex].showsudoku()
elif cmm == "k":#keyin data to array
cl=2
while 1:
xin= input("input game pos(11..99),Num:")
ll=xin.split(",")
if len(ll)==2:
pos=eval(xin.split(",")[0])
nn =eval(xin.split(",")[1])
i = int(pos/10)
j = pos%10
game[playindex].sudokudat[i-1][j-1].setno(nn)
game[playindex].showsudoku()
else:
print("Err input stop keyin ...")
break
elif cmm =='1':
level = eval(input("(1:Easy,2:Normal,3:Hard)Input Level:"))
if 0 print("Level:%d , Gcode:%d "%(level,gcode))
else:
print("Error input level Out of range")
elif cmm =='2':
gcode = eval(input("input gcode:"))
if 0 print("Level:%d , Gcode:%d "%(level,gcode))
else:
print("Error input gcode Out of range")
elif cmm =='4':
tt=0
for i in game:
print("[%d]: %s "%(tt,i.gname))
tt+=1
xcm = input("input playindex:")
if xcm.isdigit():
nn = eval(xcm)
else:
print("Error input must is Number ... ")
nn = playindex
if nn < len(game):
playindex = nn
print("You select game [ %s ]"%(game[playindex].gname))
else:
print("Error input index Out of range")
elif cmm =='3':
print()
print("====Game buffer show list ==========")
print("Level:%d , Gcode:%d "%(level,gcode))
tt=0
for i in game:
print("[%d]: %s "%(tt,i.gname))
tt+=1
print("Game index = %d is run [ %s ]"%(playindex,game[playindex].gname))
print()
elif cmm == "c":#check game pass or fail
print()
print("===check the %d is [ %s ] ==="%(playindex,game[playindex].gname))
game[playindex].showsudoku()
game[playindex].checksudoku()
if game[playindex].runfg>0:
print("Some Number must to edit ...")
else:
if game[playindex].chkfg==0:
print("************************")
print("** This Game Pass ... **")
print("************************")
else:
print("This Game Fail !")
print("Try Again ...")
elif cmm == 'c1':
runck,runfail = scanok(game[playindex])
print("have %d will be edit %d is err ... "%(runck,runfail))
elif cmm == 'c2':
pass
cmm = input("Play (game[%d]-> %s) help(?)Input cmm="%(playindex,game[playindex].gname))
if __name__ == '__main__':
main()
=======================================================
=======================================================[ pyt268104_sudoku.py] --主程式
# coding=utf-8
#=== pyt268104 home work ===
# 20160627
#import math
#import numpy as np
#def fun(a1,a2):
# c = np.dot(a1,a2)
# return c
from class_hw import sudoku_tablex1
from class_hw import sudoku_publib
level = 1
gcode = 1
pysudokuver = '''
This is Sudoku Game
Desiger: Handel Liao pyt268104
Codeing by Python3.5
Ver : 1.2 20160725
'''
lev=["Easy","Normal","Hard"]
mtxt = '''
=== Easy Sudoku Game V1.2 pyt268104 Handel ===
*** System Command ***
[1]Level Select (L1:Easy,L2:Normal,L3:Hard)
[2]Game code by LEVEL(1..10)
[3]Show Game Buffer and Status
[4]select play Game
[?]List Menu
[H]Help info show
[Q]quit
*** Game Command ***
[N]Add a New Game
[D]Delete a Game
[L]Load Game Data[from level,gcode]
[P]Print Game data array
[K]key in a data to Game
[C]check the Game by Pass
'''
def getsudoku():
xx = [[0 for i in range(9)] for j in range(9)]
return xx
def showsudoku(xx):
print()
print("="*15,"sudoku data","="*15)
for i in range(9):
for j in range(9):
print("[%2s]"%(xx[i][j]),end="")
if j in (2,5):
print("||",end="")
print()
if i in (2,5):
print(("="*12)+"||"+("="*12)+"||"+("="*12))
def setsudoku_area(xx,nn,ll):
tt=0
for j in range(9):
xx[nn-1][j]=ll[tt]
tt+=1
def setsudoku_plan(xx,tab):
tt=0
for i in range(9):
xx[i]=tab[i]
def scanok(yy):#c1: area ,col,raw must be 1 scan
for i in range(9):
for j in range(9):
if yy.sudokudat[i][j].status == 0:
yy.sudokudat[i][j].scanchk(yy.sudokudat)
runok=0
runfail=0
for i in range(9):
for j in range(9):
ll=len(yy.sudokudat[i][j].chkno)
print("%d%d = %s # %d"%(i+1,j+1,yy.sudokudat[i][j].chkno,ll))
if yy.sudokudat[i][j].no==0:
if ll==1:
yy.sudokudat[i][j].no = yy.sudokudat[i][j].chkno[0]
else:
runok+=1
if ll==0:
runfail+=1
return runok,runfail
def scanok2(yy):#c2: area mabe only 1 scan
pass
def main():
game=[]
level = 1
gcode = 1
ss="demo"+",("+lev[level-1]+"_"+str(gcode)+")"
game.append(sudoku_publib.sudoku_dataarray(ss))
game[0].setsudoku_plan(sudoku_tablex1.demo_1[gcode])
level = 2
gcode = 1
ss="demo1"+",("+lev[level-1]+"_"+str(gcode)+")"
game.append(sudoku_publib.sudoku_dataarray(ss))
game[1].setsudoku_plan(sudoku_tablex1.demo_2[gcode])
level = 3
gcode = 1
ss="demo2"+",("+lev[level-1]+"_"+str(gcode)+")"
game.append(sudoku_publib.sudoku_dataarray(ss))
game[2].setsudoku_plan(sudoku_tablex1.demo_3[gcode])
level = 1
gcode = 1
playindex = 0
cmm ='?'
while 1:
if cmm =='?':
print(mtxt)
elif cmm =='h':
print(pysudokuver)
elif cmm == 'q':
print()
print("************************")
print("** Thank You By By **")
print("************************")
print()
break
elif cmm == 'n':#add a new Game
ss = input("input the gmae name:")
ss+=",("+lev[level-1]+"_"+str(gcode)+")"
game.append(sudoku_publib.sudoku_dataarray(ss))
tt=0
for i in game:
print("[%d]: %s "%(tt,i.gname))
tt+=1
print("Add ok !")
print()
elif cmm == 'd': #delete
print("==================")
tt=0
for i in game:
print("[%d]: %s "%(tt,i.gname))
tt+=1
nn = eval(input("input index by Delete:"))
if nn>len(game)-1:
print("Sorry ! index out of range ")
print()
else:
del game[nn]
tt=0
for i in game:
print("[%d]: %s "%(tt,i.gname))
tt+=1
if playindex>=len(game):
playindex=len(game)-1
print("delete ok !")
print()
elif cmm == 'l':#load data to array
if level==1:
game[playindex].setsudoku_plan(sudoku_tablex1.demo_1[gcode])
elif level==2:
game[playindex].setsudoku_plan(sudoku_tablex1.demo_2[gcode])
elif level==3:
game[playindex].setsudoku_plan(sudoku_tablex1.demo_3[gcode])
ss1 = game[playindex].gname
ss = ss1.split(",")[0]
ss += ",("+lev[level-1]+"_"+str(gcode)+")"
game[playindex].gname = ss
print("===%d is [ %s ] ==="%(playindex,game[playindex].gname))
game[playindex].showsudoku()
elif cmm == "p":#show data array
print()
print("===%d is [ %s ] ==="%(playindex,game[playindex].gname))
game[playindex].showsudoku()
elif cmm == "k":#keyin data to array
cl=2
while 1:
xin= input("input game pos(11..99),Num:")
ll=xin.split(",")
if len(ll)==2:
pos=eval(xin.split(",")[0])
nn =eval(xin.split(",")[1])
i = int(pos/10)
j = pos%10
game[playindex].sudokudat[i-1][j-1].setno(nn)
game[playindex].showsudoku()
else:
print("Err input stop keyin ...")
break
elif cmm =='1':
level = eval(input("(1:Easy,2:Normal,3:Hard)Input Level:"))
if 0
else:
print("Error input level Out of range")
elif cmm =='2':
gcode = eval(input("input gcode:"))
if 0
else:
print("Error input gcode Out of range")
elif cmm =='4':
tt=0
for i in game:
print("[%d]: %s "%(tt,i.gname))
tt+=1
xcm = input("input playindex:")
if xcm.isdigit():
nn = eval(xcm)
else:
print("Error input must is Number ... ")
nn = playindex
if nn < len(game):
playindex = nn
print("You select game [ %s ]"%(game[playindex].gname))
else:
print("Error input index Out of range")
elif cmm =='3':
print()
print("====Game buffer show list ==========")
print("Level:%d , Gcode:%d "%(level,gcode))
tt=0
for i in game:
print("[%d]: %s "%(tt,i.gname))
tt+=1
print("Game index = %d is run [ %s ]"%(playindex,game[playindex].gname))
print()
elif cmm == "c":#check game pass or fail
print()
print("===check the %d is [ %s ] ==="%(playindex,game[playindex].gname))
game[playindex].showsudoku()
game[playindex].checksudoku()
if game[playindex].runfg>0:
print("Some Number must to edit ...")
else:
if game[playindex].chkfg==0:
print("************************")
print("** This Game Pass ... **")
print("************************")
else:
print("This Game Fail !")
print("Try Again ...")
elif cmm == 'c1':
runck,runfail = scanok(game[playindex])
print("have %d will be edit %d is err ... "%(runck,runfail))
elif cmm == 'c2':
pass
cmm = input("Play (game[%d]-> %s) help(?)Input cmm="%(playindex,game[playindex].gname))
if __name__ == '__main__':
main()
python sudoku plan
主題: 數獨
1說明:
專題由三個檔案組成
pyt268104_sudoku.py --主程式
sudoku_publib.py --class Num and Class sudoku_dataarray
sudoku_tablex1.py --題庫(demo_1)Easy ,(demo_2)Normal,(demo_3)Hard
需置於同一目錄下
2執行啟動:
python3 pyt268104_sudoku.py
啟動後 預置 demo,demo1,demo2 3個遊戲庫資料(keyin 3 whill list)
3操作:
=== Easy Sudoku Game V1.0 pyt268104 Handel ===
*** System Command ***
[1]Level Select (L1:Easy,L2:Normal,L3:Hard) : 設定 Level
[2]Game code by LEVEL(1..10) : 設定題目序號
[3]Show Game Buffer and Status : 顯示目前開啟的遊戲數急狀態名稱
[4]select play Game : 選擇緩衝區中的數獨題目
[?]List Menu : 顯示指令提示
[H]Help info show : 顯示 版本紀錄
[Q]quit : 結束離開
*** Game Command ***
[N]Add a New Game : 增加 遊戲庫資料
[D]Delete a Game : 刪除 遊戲庫資料
[L]Load Game Data[from level,gcode] : 於選定的遊戲庫資料載入指定的題目
[P]Print Game data array : 顯示遊戲庫的資料現況
[K]key in a data to Game : 輸入 (pos,Num)
[C]check the Game by Pass : 檢查遊戲狀態 (playing , Fail , Pass)
範例:
===0 is [ demo,(Easy_1) ] === ##顯示 (buffer index) [名稱,(等級_題目序號)]
======= sudoku data =========
[ 0][ 0]($9)||[ 0]($4)[ 0]||($1)($7)[ 0] ## ($ Num) 為題目不可更改
[ 0][ 0]($1)||[ 0]($2)[ 0]||[ 0]($3)[ 0] ## [ Num] 為user 寫入欄位
[ 0]($4)($6)||[ 0][ 0]($5)||($8)[ 0][ 0]
========||========||========
($6)[ 0]($3)||($7)[ 0][ 0]||[ 0][ 0][ 0]
($8)($2)[ 0]||[ 0][ 0][ 0]||[ 0][ 0]($4)
[ 0][ 0][ 0]||($5)($8)[ 0]||($6)[ 0]($3)
========||=======||=========
($5)($7)[ 0]||[ 0][ 0]($8)||($9)[ 0][ 0]
[ 0]($1)[ 0]||($2)[ 0][ 0]||[ 0]($8)($5)
($3)[ 0][ 0]||[ 0]($5)($1)||[ 0][ 0][ 0]
=========================
1說明:
專題由三個檔案組成
pyt268104_sudoku.py --主程式
sudoku_publib.py --class Num and Class sudoku_dataarray
sudoku_tablex1.py --題庫(demo_1)Easy ,(demo_2)Normal,(demo_3)Hard
需置於同一目錄下
2執行啟動:
python3 pyt268104_sudoku.py
啟動後 預置 demo,demo1,demo2 3個遊戲庫資料(keyin 3 whill list)
3操作:
=== Easy Sudoku Game V1.0 pyt268104 Handel ===
*** System Command ***
[1]Level Select (L1:Easy,L2:Normal,L3:Hard) : 設定 Level
[2]Game code by LEVEL(1..10) : 設定題目序號
[3]Show Game Buffer and Status : 顯示目前開啟的遊戲數急狀態名稱
[4]select play Game : 選擇緩衝區中的數獨題目
[?]List Menu : 顯示指令提示
[H]Help info show : 顯示 版本紀錄
[Q]quit : 結束離開
*** Game Command ***
[N]Add a New Game : 增加 遊戲庫資料
[D]Delete a Game : 刪除 遊戲庫資料
[L]Load Game Data[from level,gcode] : 於選定的遊戲庫資料載入指定的題目
[P]Print Game data array : 顯示遊戲庫的資料現況
[K]key in a data to Game : 輸入 (pos,Num)
[C]check the Game by Pass : 檢查遊戲狀態 (playing , Fail , Pass)
範例:
===0 is [ demo,(Easy_1) ] === ##顯示 (buffer index) [名稱,(等級_題目序號)]
======= sudoku data =========
[ 0][ 0]($9)||[ 0]($4)[ 0]||($1)($7)[ 0] ## ($ Num) 為題目不可更改
[ 0][ 0]($1)||[ 0]($2)[ 0]||[ 0]($3)[ 0] ## [ Num] 為user 寫入欄位
[ 0]($4)($6)||[ 0][ 0]($5)||($8)[ 0][ 0]
========||========||========
($6)[ 0]($3)||($7)[ 0][ 0]||[ 0][ 0][ 0]
($8)($2)[ 0]||[ 0][ 0][ 0]||[ 0][ 0]($4)
[ 0][ 0][ 0]||($5)($8)[ 0]||($6)[ 0]($3)
========||=======||=========
($5)($7)[ 0]||[ 0][ 0]($8)||($9)[ 0][ 0]
[ 0]($1)[ 0]||($2)[ 0][ 0]||[ 0]($8)($5)
($3)[ 0][ 0]||[ 0]($5)($1)||[ 0][ 0][ 0]
=========================
訂閱:
文章 (Atom)