Advertisement
Sax

Untitled

Sax
May 23rd, 2012
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.08 KB | None | 0 0
  1. import random
  2.  
  3. def creaMatriz(n):
  4.     """ Crea una matriz de n x n llena de x """
  5.  
  6.     m = []
  7.     for row in range(n):
  8.         cel = []
  9.         m.append(cel)
  10.         for col in range(n):
  11.              cel.append(' ')
  12.     return m
  13.  
  14. def imprimeMatriz(matriz):
  15.     """ Imprime la matriz dada de manera ORDENADA """
  16.  
  17.     for fil in range(len(matriz)):
  18.             print str(matriz[fil])
  19.  
  20. def manhattan(p1, p2):
  21.     """ Regresa la distancia Manhattan entre dos puntos """
  22.  
  23.     x1 = p1[0]
  24.     y1 = p1[1]
  25.     x2 = p2[0]
  26.     y2 = p2[1]
  27.  
  28.     distance = abs(x1-x2) + abs(y1-y2)
  29.     return distance
  30.  
  31.    
  32. def checkGuard(p):
  33.     """ Revisa si es guard """
  34.     if p == 'G':
  35.         result = 1
  36.     else:
  37.         result = 0
  38.  
  39.     return result
  40.  
  41. def validatePoint(j,i):
  42.     """ Revisa si el punto es esquina, o extremo """
  43.     global cells
  44.     n, ne, e, se, s, sw, w, nw = 0, 0, 0, 0, 0, 0, 0, 0
  45.    
  46.     #Esquina superior izquierda
  47.     if j == 0 and i == 0:
  48.         e = checkGuard(matrix[j][i+1])
  49.         se = checkGuard(matrix[j+1][i+1])
  50.         s = checkGuard(matrix[j+1][i])
  51.     #Borde superior
  52.     elif j == 0 and i != 0 and i != cells-1:
  53.         e = checkGuard(matrix[j][i+1])
  54.         se = checkGuard(matrix[j+1][i+1])
  55.         s = checkGuard(matrix[j+1][i])
  56.         sw = checkGuard(matrix[j+1][i-1])
  57.         w = checkGuard(matrix[j][i-1])
  58.     #Esquina superior derecha
  59.     elif j ==  0 and i == cells-1:
  60.         s = checkGuard(matrix[j+1][i])
  61.         sw = checkGuard(matrix[j+1][i-1])
  62.         w = checkGuard(matrix[j][i-1])
  63.     #Borde derecho
  64.     elif j!= 0 and j!= cells-1 and i ==cells-1:
  65.         n = checkGuard(matrix[j-1][i])
  66.         s = checkGuard(matrix[j+1][i])
  67.         sw = checkGuard(matrix[j+1][i-1])
  68.         w = checkGuard(matrix[j][i-1])
  69.         nw = checkGuard(matrix[j-1][i-1])
  70.     #Esquina inferior derecha
  71.     elif j == cells-1 and i == cells-1:
  72.         n = checkGuard(matrix[j-1][i])
  73.         w = checkGuard(matrix[j][i-1])
  74.         nw = checkGuard(matrix[j-1][i-1])
  75.     #Borde inferior
  76.     elif j == cells-1 and i != 0 and i!= cells-1:
  77.         n = checkGuard(matrix[j-1][i])
  78.         ne = checkGuard(matrix[j-1][i+1])
  79.         e = checkGuard(matrix[j][i+1])
  80.         w = checkGuard(matrix[j][i-1])
  81.         nw = checkGuard(matrix[j-1][i-1])
  82.     #Esquina inferior izquierda        
  83.     elif j == cells-1 and i == 0:
  84.         n = checkGuard(matrix[j-1][i])
  85.         ne = checkGuard(matrix[j-1][i+1])
  86.         e = checkGuard(matrix[j][i+1])
  87.     #Borde izquierdo
  88.     elif j != 0 and j != cells-1 and i == 0:
  89.         n = checkGuard(matrix[j-1][i])
  90.         ne = checkGuard(matrix[j-1][i+1])
  91.         e = checkGuard(matrix[j][i+1])
  92.         se = checkGuard(matrix[j+1][i+1])
  93.         s = checkGuard(matrix[j+1][i])
  94.     #Las demas casillas
  95.     else:
  96.         n = checkGuard(matrix[j-1][i])
  97.         ne = checkGuard(matrix[j-1][i+1])
  98.         e = checkGuard(matrix[j][i+1])
  99.         se = checkGuard(matrix[j+1][i+1])
  100.         s = checkGuard(matrix[j+1][i])
  101.         sw = checkGuard(matrix[j+1][i-1])
  102.         w = checkGuard(matrix[j][i-1])
  103.         nw = checkGuard(matrix[j-1][i-1])
  104.        
  105.  
  106.     total = n + ne + e + se + s + sw + w + nw
  107.  
  108.     return total
  109.    
  110.  
  111. def run():
  112.     """ Regresa el numero de guardias alrededor del punto """
  113.     global cells
  114.  
  115.     global lulz
  116.     risk = []
  117.     positions = []
  118.     distances = []
  119.     decisions = []
  120.  
  121.     for j in range(cells):
  122.         for i in range (cells):
  123.             if(matrix[j][i] == 'A'):
  124.                 A = [j,i]
  125.    
  126.     for j in range(cells):
  127.         for i in range(cells):
  128.             if(matrix[j][i] == 'T'):
  129.                 print "El target en " + str(j+1) + "," + str(i+1) + " tiene " + str(validatePoint(j,i)) + " guardianes cerca"
  130.                 risk.append(validatePoint(j,i))
  131.                 positions.append([j,i])
  132.                 print "La distancia entre Talon y el target " + str(j+1) + "," + str(i+1) + " es de " + str(manhattan(A, [j,i]))
  133.                 distances.append(manhattan(A, [j,i]))
  134.  
  135.     for i in range(len(risk)):
  136.         decisions.append(risk[i] + distances[i])
  137.  
  138.     chosen = decisions.index(min(decisions)) #Esto es un index
  139.  
  140.     print "\n\nTalon preferiria matar al target de " + str(positions[chosen][0]+1) + "," + str(positions[chosen][1]+1)
  141.  
  142.    
  143.            
  144.    
  145.  
  146. #-------------------------------------------------------------------------------
  147. # Main~~
  148. #-------------------------------------------------------------------------------
  149. cells = 4
  150. lulz = {}
  151. matrix = creaMatriz(cells)
  152. t1 = 'T'
  153. t2 = 'T'
  154. t3 = 'T'
  155.  
  156. g1 = 'G'
  157. g2 = 'G'
  158. g3 = 'G'
  159. g4 = 'G'
  160. g5 = 'G'
  161. g6 = 'G'
  162.  
  163. a = 'A'
  164.  
  165. matrix[random.randrange(cells)][random.randrange(cells)] = t2
  166. matrix[random.randrange(cells)][random.randrange(cells)] = g1
  167. matrix[random.randrange(cells)][random.randrange(cells)] = g2
  168. matrix[random.randrange(cells)][random.randrange(cells)] = g3
  169. matrix[random.randrange(cells)][random.randrange(cells)] = t1
  170. matrix[random.randrange(cells)][random.randrange(cells)] = g4
  171. matrix[random.randrange(cells)][random.randrange(cells)] = g5
  172. matrix[random.randrange(cells)][random.randrange(cells)] = g6
  173. matrix[random.randrange(cells)][random.randrange(cells)] = t3
  174. matrix[random.randrange(cells)][random.randrange(cells)] = a
  175.  
  176. imprimeMatriz(matrix)
  177. print "\n\n"
  178. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement