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,0,0,0,2,0,9] ],
       
    [[0,3,0,0,0,0,1,0,0],#e006  
     [0,0,0,4,2,0,0,3,0],
     [8,0,9,0,0,7,0,0,0],
     [6,8,0,0,0,0,3,4,0],
     [9,0,0,6,8,5,0,0,2],
     [0,1,7,0,0,0,0,8,5],
     [0,9,0,2,0,0,5,0,4],
     [0,0,0,0,3,4,0,0,0],
     [0,0,8,0,0,0,0,6,0] ],
         
    [[0,4,0,0,2,0,9,0,7],#e007  
     [0,3,0,0,5,0,0,0,0],
     [9,0,0,3,0,0,1,0,0],
     [8,7,0,4,0,0,0,0,0],
     [0,0,4,7,0,8,6,0,0],
     [0,0,0,0,0,5,0,8,4],
     [0,0,1,0,0,6,0,0,9],
     [0,0,0,0,3,0,0,6,0],
     [7,0,6,0,4,0,0,1,0] ],                              
         
    [[0,2,0,0,6,0,4,0,1],#e008  
     [5,0,8,0,0,3,0,0,0],
     [0,3,0,2,0,0,0,0,9],
     [0,0,0,0,4,0,8,9,0],
     [8,0,0,7,5,2,0,0,4],
     [0,6,4,0,3,0,0,0,0],
     [1,0,0,0,0,7,0,5,0],
     [0,0,0,9,0,0,7,0,3],
     [4,0,5,0,8,0,0,1,0] ],
         
    [[2,0,0,6,0,7,0,0,8],#e009  
     [4,9,0,0,0,0,3,7,0],
     [8,0,0,0,4,0,5,0,0],
     [9,1,0,2,0,0,0,0,0],
     [0,0,3,4,0,1,6,0,0],
     [0,0,0,0,0,3,0,8,4],
     [0,0,9,0,6,0,0,0,5],
     [0,6,5,0,0,0,0,4,7],
     [7,0,0,3,0,2,0,0,6] ],
         
    [[0,0,0,0,0,0,0,0,0],#e010  
     [0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0] ]

    ]
#============================
demo_2=[        
    [[0,8,0,0,0,0,0,3,4],#n001  
     [0,0,1,0,9,3,0,0,0],
     [0,3,0,0,0,0,1,5,0],
     [4,0,0,2,0,0,5,0,0],
     [1,0,0,7,0,4,0,0,2],
     [0,0,8,0,0,5,0,0,6],
     [0,1,4,0,0,0,0,9,0],
     [0,0,0,3,4,0,6,0,0],
     [5,2,0,0,0,0,0,4,0] ],
         
    [[3,8,0,1,0,0,0,0,0],#n002  
     [0,0,1,0,0,0,6,0,4],
     [0,4,0,9,3,0,0,0,0],
     [7,0,0,0,0,0,0,6,1],
     [0,2,0,5,0,4,0,9,0],
     [8,5,0,0,0,0,0,0,7],
     [0,0,0,0,6,3,0,2,0],
     [5,0,7,0,0,0,3,0,0],
     [0,0,0,0,0,9,0,1,6] ],                                
         
    [[0,0,0,7,0,0,2,8,0],#n003  
     [0,2,0,0,0,6,5,0,0],
     [5,7,0,0,0,0,0,0,4],
     [3,0,0,0,8,4,0,1,0],
     [7,0,0,0,0,0,0,0,3],
     [0,4,0,3,2,0,0,0,8],
     [8,0,0,0,0,0,0,4,1],
     [0,0,9,8,0,0,0,7,0],
     [0,3,4,0,0,9,0,0,0] ],
         
    [[0,4,6,0,0,7,0,0,2],#n004  
     [0,0,0,0,0,9,5,0,4],
     [8,7,0,0,3,0,0,0,0],
     [0,5,2,7,0,0,0,0,0],
     [0,0,8,0,6,0,9,0,0],
     [0,0,0,0,0,8,2,6,0],
     [0,0,0,0,2,0,0,5,1],
     [3,0,4,1,0,0,0,0,0],
     [5,0,0,9,0,0,6,4,0] ],
         
    [[0,1,3,0,0,5,0,0,0],#n005  
     [0,0,2,0,0,3,0,9,0],
     [9,0,0,0,0,0,4,0,8],
     [0,3,0,1,0,6,0,0,2],
     [5,0,0,0,3,0,0,0,9],
     [7,0,0,2,0,9,0,6,0],
     [1,0,4,0,0,0,0,0,5],
     [0,9,0,5,0,0,6,0,0],
     [0,0,0,8,0,0,1,2,0] ],
       
    [[0,6,8,0,3,0,0,0,0],#n006  
     [0,0,0,0,0,0,9,0,1],
     [0,0,0,7,0,6,3,0,2],
     [0,0,0,0,4,7,0,1,0],
     [5,4,0,0,0,0,0,9,6],
     [0,3,0,2,6,0,0,0,0],
     [8,0,6,5,0,4,0,0,0],
     [9,0,3,0,0,0,0,0,0],
     [0,0,0,0,9,0,1,5,0] ],
         
    [[9,2,0,3,0,8,0,0,0],#n007  
     [0,0,0,0,6,0,0,0,8],
     [0,8,3,7,0,0,0,0,0],
     [6,0,1,0,0,0,5,0,0],
     [0,4,0,5,0,3,0,9,0],
     [0,0,7,0,0,0,8,0,3],
     [0,0,0,0,0,6,2,5,0],
     [4,0,0,0,8,0,0,0,0],
     [0,0,0,4,0,1,0,8,6] ],                                    
         
    [[0,0,1,0,0,0,3,4,0],#n008  
     [0,4,0,0,8,0,0,0,9],
     [5,0,0,3,0,7,0,0,0],
     [4,0,6,2,0,0,0,0,0],
     [0,9,0,5,0,6,0,7,0],
     [0,0,0,0,0,1,6,0,5],
     [0,0,0,7,0,5,0,0,8],
     [2,0,0,0,1,0,0,9,0],
     [0,8,3,0,0,0,7,0,0] ],
         
    [[2,0,1,8,0,0,0,6,0],#n009  
     [0,0,0,9,0,0,0,2,4],
     [4,0,0,0,1,0,0,0,0],
     [0,0,2,1,5,0,9,0,0],
     [0,8,0,0,3,0,0,1,0],
     [0,0,9,0,6,7,5,0,0],
     [0,0,0,0,9,0,0,0,7],
     [9,3,0,0,0,6,0,0,0],
     [0,5,0,0,0,1,8,0,9] ],
         
    [[5,0,0,9,0,0,0,3,0],#n010  
     [0,0,0,0,7,4,6,0,0],
     [1,0,6,0,0,0,0,4,0],
     [0,0,0,0,0,1,3,7,0],
     [0,0,8,6,9,7,1,0,0],
     [0,7,1,3,0,0,0,0,0],
     [0,9,0,0,0,0,5,0,8],
     [0,0,4,8,1,0,0,0,0],
     [0,1,0,0,0,2,0,0,9] ]
    ]    
#============================
demo_3=[        
    [[0,0,8,0,0,0,0,5,1],#h001  
     [0,6,0,0,0,5,7,0,0],
     [0,0,1,0,8,0,0,0,0],
     [0,2,0,0,0,0,0,0,6],
     [5,0,0,7,0,1,0,0,4],
     [3,0,0,0,0,0,0,2,0],
     [0,0,0,0,5,0,9,0,0],
     [0,0,3,8,0,0,0,1,0],
     [4,9,0,0,0,0,8,0,0] ],
         
    [[0,0,4,8,0,0,0,1,0],#h002  
     [0,0,7,0,9,0,0,0,4],
     [0,6,0,0,3,0,2,0,0],
     [8,0,0,0,0,1,0,0,0],
     [0,0,0,3,0,2,0,0,0],
     [0,0,0,7,0,0,0,0,1],
     [0,0,2,0,4,0,0,5,0],
     [4,0,0,0,2,0,6,0,0],
     [0,9,0,0,0,7,4,0,0] ],                                
         
    [[0,0,8,0,3,0,0,6,0],#h003  
     [5,0,0,1,0,0,0,0,0],
     [0,0,0,0,0,9,1,7,0],
     [0,0,0,0,0,5,9,4,0],
     [2,0,0,0,0,0,0,0,8],
     [0,3,5,4,0,0,0,0,0],
     [0,2,6,7,0,0,0,0,0],
     [0,0,0,0,0,8,0,0,4],
     [0,5,0,0,4,0,7,0,0] ],
         
    [[0,0,1,0,0,0,0,0,8],#h004  
     [8,0,0,5,0,1,0,0,0],
     [0,5,0,0,3,0,1,0,0],
     [0,1,0,0,0,6,0,4,0],
     [0,0,6,0,0,0,7,0,0],
     [0,9,0,3,0,0,0,2,0],
     [0,0,5,0,7,0,0,3,0],
     [0,0,0,2,0,8,0,0,5],
     [4,0,0,0,0,0,8,0,0] ],
         
    [[1,0,0,0,7,0,0,9,0],#h005  
     [0,5,3,0,0,0,0,0,4],
     [2,0,0,0,0,3,0,0,0],
     [0,2,0,0,0,5,8,0,0],
     [0,0,0,6,0,8,0,0,0],
     [0,0,5,1,0,0,0,2,0],
     [0,0,0,2,0,0,0,0,5],
     [4,0,0,0,0,0,3,6,0],
     [0,8,0,0,9,0,0,0,7] ],
       
    [[0,0,5,0,0,9,0,2,0],#h006  
     [0,7,0,0,5,0,0,0,3],
     [4,0,0,0,1,0,0,0,0],
     [0,4,0,6,0,0,0,0,0],
     [0,0,6,9,0,7,2,0,0],
     [0,0,0,0,0,3,0,7,0],
     [0,0,0,0,6,0,0,0,8],
     [6,0,0,0,3,0,0,1,0],
     [0,5,0,8,0,0,6,0,0] ],
         
    [[0,0,0,0,0,3,9,0,8],#h007  
     [0,0,1,0,0,9,0,0,2],
     [2,3,0,0,4,0,0,0,0],
     [0,7,0,0,0,6,0,0,0],
     [0,0,4,0,0,0,5,0,0],
     [0,0,0,4,0,0,0,3,0],
     [0,0,0,0,7,0,0,8,9],
     [4,0,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])

=======================================================

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()


=======================================================

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]
=========================

2016年5月18日 星期三

Python OO copy and deepcopy 9x9 Pad by SUDO test code

import copy

class suitem:
    def __init__(self):
        dd = [1,2,3,4,5,6,7,8,9]
        ddx = []
        self.set = ['x']*9*9
#        self.data = copy.deepcopy(dd)*9*9
        for i in range(9):
            for j in range(9):
                ddx.append(copy.deepcopy(dd))
        self.data = ddx

    def showset(self):
        for i in range(9):
            print ">>[",
            for j in range(9):
                print self.set[i*9+j],
            print "]<<"

    def showdata(self):
        for i in range(9):
            print ">>[",
            for j in range(9):
                print self.data[i*9+j],
            print "]<<"
           
if __name__ == '__main__':
    sett = suitem()


    a = copy.copy(sett)
    b = copy.deepcopy(sett)

   
    sett.set[0] ='1'
    print "=== other ==="
    sett.showset()
    print "=== other ==="
    a.showset()
    print "=== other ==="
    b.showset()
    print "=== end   ==="
   

2016年5月15日 星期日

python 2.7 and 3.3 install easy_install , pip

yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel


1
2
3
4
5
6
7
8
9
10
11
12
13
# Python 2.7.6:
tar xf Python-2.7.6.tar.xz
cd Python-2.7.6
./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall

# Python 3.3.5:
tar xf Python-3.3.5.tar.xz
cd Python-3.3.5
./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall


# Then install it for Python 2.7 and/or Python 3.3:
python2.7 ez_setup.py
python3.3 ez_setup.py

# Now install pip using the newly installed setuptools:
easy_install-2.7 pip
easy_install-3.3 pip

# With pip installed you can now do things like this:
pip2.7 install [packagename]
pip2.7 install --upgrade [packagename]
pip2.7 uninstall [packagename]





2016年5月11日 星期三

python read the direct [dir ] [file] list

[Python] 取得路徑下檔案
'ss = os.listdir(" ")      
'if  os.path.isfile(ss[0]) :
'if  os.path.isdir(ss[0]):

如何利用python取得特定路徑下的檔案名稱呢? 

其實說起來也相當簡單,os module裡已經定義好了。

先從os.listdir本身會回傳該資料夾下的所有檔案名稱,以list回傳。

再用一個 for 迴圈遍訪每個檔名,對其用os.path.isfile()判斷即可(若要取資料夾使用os.path.isdir())。

以下為範例程式碼。


import os
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
        print f
上面是個簡潔有力的寫法,他的效果等義於下列程式碼:

import os
files = []

for f in os.listdir('.'):
        if os.path.isfile(f):
                files.append(f)
for f in files:
        print f



2016年5月10日 星期二

pip auto update the package tool Demo


PIP command example : 操作命令

  • pip list # 看目前系統有安裝哪些套件
  • pip search mysql # 搜尋相關套件
  • pip install package # 安裝套件
  • pip uninstall package # 移除套件
  • pip show --files package # 秀套件檔案列表
  • pip list --outdated # 列出過期套件
  • pip install --upgrade package # 升級

>> save next code to pipupv27.py
>> python2 pipupv27.py
then will auto scan the pip upgrade package list and  type y/a/n to select the function


#====================================================
import time
import sys
import os

os.system("pip2.7 list --outdated > chklist.txt")
f = open("chklist.txt",'r')
ss = f.read()
f.close()
kss = ss.split("\n")
#print ss

for i in kss:
    print (i)

kk='n'
kk2='n'
kk = raw_input("\nare you want to upgrate(y:1by1/a:all/n) : ")
if kk == 'n':
    print "\nby by ..."
    exit()

if kk == 'a':
        for i in kss:
                if i.split(" ")[0] != "numpy":
                    os.system("pip2.7 install --upgrade "+i.split(" ")[0])
else:
        for i in kss:
                ss2 = i.split(" ")[0]
                print (ss2)
                kk2 = raw_input("\n update it (y n):")
                if kk2 == 'y':
                    if i.split(" ")[0] != "numpy":
                        os.system("pip2.7 install --upgrade "+i.split(" ")[0])
                else:
                    print ("\npass to next")

#========================================================


2016年5月9日 星期一

python use dict function run ( by switch case example code )

# coding=utf-8

def key1(ss):
    print ("\n run %s ok !" % ss)
    pass
         
def key2(ss):
    print ("\n run %s ok !" % ss)
    pass
           
def key3(ss):
    print ("\n run %s ok !" % ss)
    pass
 
def key4(ss):
    print ("\n run %s ok !" % ss)
    pass

caseloop = {
    "k1":key1,
    "k2":key2,
    "k3":key3,
    "k4":key4
 }    
     
if __name__ == '__main__':
    while 1:
        kk = input("\n type key:")
        caseloop[kk](kk)

2016年5月8日 星期日

python Functin set array call Example

# Function Set call by index    
def aa():
  print ("is aa run !\n")
 
def bb():
  print ("is bb run !\n")  

def cc():
  print ("is cc run !\n")

def dd():
  print ("is dd run !\n")

workp = {0:aa,1:bb,2:cc,3:dd}  

if __name__ == '__main__':
  print ("hello dir call test ! \n ================== \n")
  aa(),bb(),cc(),dd()
  print ("hello set call test ! \n ================== \n")
  workp[0]()                                              
  workp[1]()
  workp[2]()
  workp[3]()


run Ex:
hello dir call test !
 ==================
is aa run !
is bb run !
is cc run !
is dd run !
hello set call test !
 ==================
is aa run !
is bb run !
is cc run !
is dd run !

================================================
EXample 2
def plus_1(x):
    return x + 1

def minus_1(x):
    return x - 1

func_map = {'+' : plus_1, '-' : minus_1}

print (func_map['+'](3))  # returns plus_1(3) ==> 4
print (func_map['-'](3))  # returns minus_1(3) ==> 2