pablohf

Untitled

Mar 10th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. '''
  2. Created on 06/03/2013
  3.  
  4. @author: pablo
  5. '''
  6. from genericpath import exists
  7. def cria_matriz():
  8.     mfile = open('my_matrix.txt')
  9.     line = mfile.readline()
  10.     dimension = [ int(line.split(' ')[0]), int(line.split(' ')[1]) ]
  11.    
  12.     mat = []
  13.    
  14.     for i in range(dimension[0]):
  15.         line = mfile.readline()
  16.         mat.append( [ int(s) for s in line.split(' ') ] )
  17.    
  18.     mfile.close()
  19.     return mat
  20.  
  21. def final_matriz():
  22.     mat = [[1,2,3],
  23.            [8,0,4],
  24.            [7,6,5]]
  25.     return mat
  26.  
  27. def mostra_matriz(mat):
  28.     for i in range(len(mat)):
  29.         for j in range(len(mat[i])):
  30.             print mat[i][j],
  31.         print '\n',
  32.     print '\n',
  33.  
  34. def change_pos(mat, pos1, pos2):
  35.     mat[pos1[0]][pos1[1]] , mat[pos2[0]][pos2[1]] = mat[pos2[0]][pos2[1]] , mat[pos1[0]][pos1[1]]  
  36.    
  37. def search_zero(mat):
  38.     for i in range(len(mat)):
  39.         for j in range(len(mat[i])):
  40.             if mat[i][j] == 0:
  41.                 return i,j
  42.                
  43. def game(mat1, mat2):
  44.    
  45.    
  46.     while verifica_matriz(mat1, mat2) != 1:
  47.         i,j = search_zero(mat1)
  48.        
  49.         print mat1[i][j]
  50.         if mat1[i][j] == 0:
  51.             up_matriz(mat1, (i,j))
  52.             mostra_matriz(mat1)
  53.             i,j = search_zero(mat1)
  54.            
  55.             left_matriz(mat1, (i,j))
  56.             mostra_matriz(mat1)
  57.             i,j = search_zero(mat1)        
  58.            
  59.             rigth_matriz(mat1, (i,j))
  60.             mostra_matriz(mat1)
  61.             i,j = search_zero(mat1)
  62.            
  63.             down_matriz(mat1, (i, j))
  64.             mostra_matriz(mat1)
  65.            
  66.    
  67. def verifica_matriz(mat1,mat2):
  68.     if mat1 == mat2:
  69.         return 1
  70.     else:
  71.         return 0      
  72.                
  73. def up_matriz(mat, pos):
  74.     i,j = pos
  75.     if i != 0:
  76.         mat[i][j], mat[i-1][j] = mat[i-1][j], mat[i][j]
  77.     else:
  78.         print 'Nao existe posicao'
  79.    
  80. def left_matriz(mat, pos):
  81.     i,j = pos
  82.     if j != 0:
  83.         mat[i][j], mat[i][j-1] = mat[i][j-1], mat[i][j]
  84.     else:
  85.         print 'Nao existe posicao'  
  86.    
  87. def rigth_matriz(mat, pos):
  88.     i, j = pos
  89.     if j != 2:
  90.         mat[i][j], mat[i][j+1] = mat[i][j+1], mat[i][j]
  91.     else:
  92.         print 'Nao existe posicao'
  93.    
  94. def down_matriz(mat, pos):
  95.     i,j = pos
  96.     if i != 2:
  97.         mat[i][j], mat[i+1][j] = mat[i+1][j], mat[i][j]
  98.     else:
  99.         print 'Nao existe posicao'      
  100.      
  101.  
  102. mat1 = cria_matriz()
  103. mat2 = final_matriz()
  104. mostra_matriz(mat1)
  105. #change_pos(mat1,[2,0],[1,2])
  106. #up_matriz(mat1,(0,0))
  107. #left_matriz(mat1, (0,1))
  108. #rigth_matriz(mat1, (0,1))
  109. #down_matriz(mat1, (1,0))
  110. search_zero(mat1)
  111. game(mat1,mat2)
  112. verifica_matriz(mat1, mat2)
  113. mostra_matriz(mat1)
Advertisement
Add Comment
Please, Sign In to add comment