Welcome, guest | Sign In | My Account | Store | Cart
 #<><><><><><><><><><><><><><><><><><><><><><><><> sodoko puzzle <><><><><><><><><><><><><><><><><><><><><><><><>

#  By : Amir Naghvi #
#  Olempico@Gmail.com  #




global Platform
global fill
fill=False # to not printing type of mistake while asighning table automaticaly.
Platform=[[0 for i in  "."*9]for i in "."*9] # Main table to Handle Game



import random
class main(object):
    "------------- start --------------"
    def __init__(self):
        pass
    
    def gameloop(self): # ----------------GAME LOOP
        self.Fill()
        while True:
            o=raw_input('to show puzzle enter s to quit enter q: ')
            if o=="s":self.show()
            elif o=="q":return
            (x,y,num)=input('first X then Y then NUM like  X,Y,NUMBER:  ')
            if self.give(x,y,num):
                print "True"
            else:
                print "you entered incorrect";
            self.show()
            
    def show(self): # -------To Printing puzzle
        c1=0
        c2=0
        for i in Platform:
            if c1%3==0:print">>>"*30,
            print "     "
            c1+=1
            for j in i:
                c2+=1
                if j==0:
                    print " ","_ _"," ",
                    if c2%3==0:print "||",;
                else:
                    print " ",j," "," ",
                    if c2%3==0:print "||",;
            print "\n";
        print "\n\n";
            
    def give(self,x,y,NUM):  # ---for recieving number and it's position
        """ first checks if the position in not filled then change position's number
        then if the number is not valid changes the position number to 0 again and returns False"""
        
        if Platform[x-1][y-1]!=0:
            if not fill:
                print"\n_________Already Filled__________\n";
            return False
        Platform [x-1][y-1]=NUM
        if not self.control(x,y)==True:
            Platform [x-1][y-1]=0
            return False
        else:
            return True
            
    def control(slef,x,y):
        """There is Three checking stages:1st we find the number's row ,column and cube number then
        check if the row has not the same numbers  ,2nd we collect the number's colmn neigbours in 'colmns' list
        and simply do checking again 3rd: for checking the number in its cube area i explain below:"""
        row=x-1;#print row
        col=y-1;#print col
        
        # 1. row test >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.1
        rows=[Platform[row][i] for i in range(9)]
        #print rows
        for i in range(9):
            for j in range(9):
                if j!=i:
                    if rows[i]==rows[j] and rows[i]!=0 and rows[j]!=0:
                        if not fill:
                            print 'row'
                        return False
                        
        # 2. column test >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.2
        colmns=[Platform[i][col] for i in range(9)]
        for i in range(9):
            for j in range(9):
                if j!=i:
                    if colmns[i]==colmns[j] and colmns[j]!=0 and colmns[i]!=0:
                        if not fill:
                            print 'col'
                        return False
                    
        # 3. 3x3 Cubes >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.3
        """ 1.frst for 'first' cube number in X attitiude and sec second cube number in y ,for example centeral cube is (3,3)
        or first cube from bottom is (0,6) 2.then in 'cube' list i gather the cube numbers and 3.then simply check if there is a
        repeated numbers"""
        # 1.
        frst=-1
        sec=-1
        if row<3:frst=0
        elif row<6:frst=3
        elif row<9:frst=6
        if col<3:sec=0
        elif col<6:sec=3
        elif col<9:sec=6
        # 2.
        cube=[Platform[frst][sec+i]for i in range(3)] # creating cube
        cube.extend([Platform[frst+1][sec+i]for i in range(3)])
        cube.extend([Platform[frst+2][sec+i]for i in range(3)])
        #print cube
        # 3.
        for i in range(9):
            for j in range(9):
                if j!=i:
                    if cube[i]==cube[j] and cube[i]!=0 and cube[j]!=0:
                        if not fill:
                            print 'cube'
                        return False
        return True

            
    def give2(self,x,y,NUM):  # ---for recieving number and it's position
        """ first checks if the position in not filled then change position's number
        then if the number is not valid changes the position number to 0 again and returns False"""
        
        if Platform[x-1][y-1]!=0:return False
        Platform [x-1][y-1]=NUM
        if not self.control(x,y):
            Platform [x-1][y-1]=0
            return False
        else:
            return True
    
    def Fill(self):
        global fill
        fill=True
        c=0
        #limit=random.randint(14,30)
        limit=81
        while c<=limit:
            (x,y,num)=(random.randint(1,9),random.randint(1,9),random.randint(1,10))
            if self.give(x,y,num):
                c+=1
        self.show()
        fill=False
            
        

# ---------------------------------------------------------------------
# ---------------------------------------------------------------------
if __name__=="__main__":
    o=main()
    o.gameloop()

Diff to Previous Revision

--- revision 4 2011-05-23 08:51:15
+++ revision 5 2011-05-23 14:44:50
@@ -7,16 +7,20 @@
 
 
 global Platform
-automatically.
+global fill
+fill=False # to not printing type of mistake while asighning table automaticaly.
 Platform=[[0 for i in  "."*9]for i in "."*9] # Main table to Handle Game
 
 
+
+import random
 class main(object):
     "------------- start --------------"
     def __init__(self):
         pass
     
     def gameloop(self): # ----------------GAME LOOP
+        self.Fill()
         while True:
             o=raw_input('to show puzzle enter s to quit enter q: ')
             if o=="s":self.show()
@@ -50,28 +54,33 @@
         """ first checks if the position in not filled then change position's number
         then if the number is not valid changes the position number to 0 again and returns False"""
         
-        if Platform[x-1][y-1]!=0:print"\n_________Already Filled__________\n";return False
+        if Platform[x-1][y-1]!=0:
+            if not fill:
+                print"\n_________Already Filled__________\n";
+            return False
         Platform [x-1][y-1]=NUM
-        if not self.control(x,y):
+        if not self.control(x,y)==True:
             Platform [x-1][y-1]=0
             return False
-        return True
+        else:
+            return True
             
     def control(slef,x,y):
         """There is Three checking stages:1st we find the number's row ,column and cube number then
         check if the row has not the same numbers  ,2nd we collect the number's colmn neigbours in 'colmns' list
         and simply do checking again 3rd: for checking the number in its cube area i explain below:"""
-        row=x-1/10
-        col=(y%10)-1
-        colmns=[]
+        row=x-1;#print row
+        col=y-1;#print col
         
         # 1. row test >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.1
         rows=[Platform[row][i] for i in range(9)]
+        #print rows
         for i in range(9):
             for j in range(9):
                 if j!=i:
-                    if rows[i]==rows[j] and rows[i]!=0:
-                        print 'row'
+                    if rows[i]==rows[j] and rows[i]!=0 and rows[j]!=0:
+                        if not fill:
+                            print 'row'
                         return False
                         
         # 2. column test >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.2
@@ -79,8 +88,9 @@
         for i in range(9):
             for j in range(9):
                 if j!=i:
-                    if colmns[i]==colmns[j] and colmns[j]!=0:
-                        print 'col'
+                    if colmns[i]==colmns[j] and colmns[j]!=0 and colmns[i]!=0:
+                        if not fill:
+                            print 'col'
                         return False
                     
         # 3. 3x3 Cubes >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.3
@@ -90,12 +100,12 @@
         # 1.
         frst=-1
         sec=-1
-        if col<4:frst=0
-        elif col<7:frst=3
-        elif col<10:frst=6
-        if col<4:sec=0
-        elif col<7:sec=3
-        elif col<10:sec=6
+        if row<3:frst=0
+        elif row<6:frst=3
+        elif row<9:frst=6
+        if col<3:sec=0
+        elif col<6:sec=3
+        elif col<9:sec=6
         # 2.
         cube=[Platform[frst][sec+i]for i in range(3)] # creating cube
         cube.extend([Platform[frst+1][sec+i]for i in range(3)])
@@ -105,14 +115,42 @@
         for i in range(9):
             for j in range(9):
                 if j!=i:
-                    if cube[i]==cube[j] and cube[i]!=0:
-                        print 'cube'
+                    if cube[i]==cube[j] and cube[i]!=0 and cube[j]!=0:
+                        if not fill:
+                            print 'cube'
                         return False
         return True
 
-
+            
+    def give2(self,x,y,NUM):  # ---for recieving number and it's position
+        """ first checks if the position in not filled then change position's number
+        then if the number is not valid changes the position number to 0 again and returns False"""
+        
+        if Platform[x-1][y-1]!=0:return False
+        Platform [x-1][y-1]=NUM
+        if not self.control(x,y):
+            Platform [x-1][y-1]=0
+            return False
+        else:
+            return True
+    
+    def Fill(self):
+        global fill
+        fill=True
+        c=0
+        #limit=random.randint(14,30)
+        limit=81
+        while c<=limit:
+            (x,y,num)=(random.randint(1,9),random.randint(1,9),random.randint(1,10))
+            if self.give(x,y,num):
+                c+=1
+        self.show()
+        fill=False
+            
+        
 
 # ---------------------------------------------------------------------
 # ---------------------------------------------------------------------
-o=main()
-o.gameloop()
+if __name__=="__main__":
+    o=main()
+    o.gameloop()

History