Ver Mensaje Individual
  #3 (permalink)  
Antiguo 05/10/2011, 08:55
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 9 meses
Puntos: 1360
Respuesta: Buscaminas megabasico

Código Python:
Ver original
  1. #Buscaminas.py
  2. from random import sample
  3. from itertools import product
  4.  
  5. def makeMap(rows, cols, char):
  6.     map = [[char] * cols for _ in xrange(rows)]
  7.     return map
  8.  
  9. def makeMines(mines, rows, cols):
  10.     return sample(list(product(range(rows), range(cols))), mines)
  11.  
  12. def putMines(map, minePoints):
  13.     for x, y in minePoints:
  14.         map[x][y] = '*'
  15.  
  16. def isInsideMap(rows, cols, i, j):
  17.     return (i >= 0 and i < rows) and (j >= 0 and j < cols)
  18.  
  19. def makeNumbers(map, rows, cols):
  20.     def change(map, i, j, rows, cols):
  21.         if isInsideMap(rows, cols, i, j) and map[i][j] != '*':
  22.             if not map[i][j].isdigit():
  23.                 map[i][j] = '1'
  24.             else:
  25.                 map[i][j] = str(int(map[i][j]) + 1)
  26.            
  27.     for i in xrange(rows):
  28.         for j in xrange(cols):
  29.             if map[i][j] == '*':
  30.                 for x, y in product([0, 1, -1], [0, 1, -1]):
  31.                     if x or y:
  32.                         change(map, i+x, j+y, rows, cols)
  33.  
  34. def printMap(map):
  35.     for row in map:
  36.         print ''.join(row)
  37.        
  38. def floodFill(map, rows, cols, i, j, target, replace):
  39.     if (not isInsideMap(rows, cols, i, j)) or map[i][j] != target:
  40.         return
  41.     map[i][j] = replace
  42.     floodFill(map, rows, cols, i+1, j, target, replace)
  43.     floodFill(map, rows, cols, i, j+1, target, replace)
  44.     floodFill(map, rows, cols, i-1, j, target, replace)
  45.     floodFill(map, rows, cols, i, j-1, target, replace)
  46.    
  47. def copyChars(map1, map2, rows, cols, char):
  48.     for i in xrange(rows):
  49.         for j in xrange(cols):
  50.             if map1[i][j] == char:
  51.                 map2[i][j] = char
  52.                
  53. def replaceChars(map, rows, cols, target, replace):
  54.     for i in xrange(rows):
  55.         for j in xrange(cols):
  56.             if map[i][j] == target:
  57.                 map[i][j] = replace
  58.                
  59. def isWin(map, mapPlayer, rows, cols):
  60.     m = '\n'.join(''.join(row) for row in map)
  61.     n = '\n'.join(''.join(row) for row in mapPlayer)
  62.     m = m.replace("*", "X")
  63.     n = n.replace(".", "X")
  64.     return m == n

Código Python:
Ver original
  1. from buscaminas import *
  2.  
  3. rows = 2
  4. cols = 2
  5. mines = 1
  6.  
  7. map = makeMap(rows, cols, '.')
  8. playerMap = makeMap(rows, cols, '.')
  9.  
  10. minePoints = makeMines(mines, rows, cols)
  11. putMines(map, minePoints)
  12.  
  13. makeNumbers(map, rows, cols)
  14.  
  15. while True:
  16.     printMap(playerMap)
  17.     print "Dame un punto"
  18.     x = int(raw_input()) - 1
  19.     y = int(raw_input()) - 1
  20.    
  21.     if not isInsideMap(rows, cols, x, y):
  22.         continue
  23.    
  24.     if map[x][y] == '*':
  25.         replaceChars(map, rows, cols, '.', '#')
  26.         printMap(map)
  27.         print "Perdiste"
  28.         break
  29.     elif map[x][y] == '.':
  30.         floodFill(map, rows, cols, x, y, '.', '#')
  31.         copyChars(map, playerMap, rows, cols, '#')
  32.     else:
  33.         playerMap[x][y] = map[x][y]
  34.        
  35.     if isWin(map, playerMap, rows, cols):
  36.         printMap(map)
  37.         print "Ganaste"
  38.         break

Hace tiempo que ya había hecho un buscaminas pero nunca lo termine, cuando vi tu post me puse a terminarlo.