Código Python:
Ver original#Buscaminas.py
from random import sample
from itertools import product
def makeMap(rows, cols, char):
map = [[char] * cols for _ in xrange(rows)]
return map
def makeMines(mines, rows, cols):
return sample(list(product(range(rows), range(cols))), mines)
def putMines(map, minePoints):
for x, y in minePoints:
map[x][y] = '*'
def isInsideMap(rows, cols, i, j):
return (i >= 0 and i < rows) and (j >= 0 and j < cols)
def makeNumbers(map, rows, cols):
def change(map, i, j, rows, cols):
if isInsideMap(rows, cols, i, j) and map[i][j] != '*':
if not map[i][j].isdigit():
map[i][j] = '1'
else:
map[i][j] = str(int(map[i][j]) + 1)
for i in xrange(rows):
for j in xrange(cols):
if map[i][j] == '*':
for x, y in product([0, 1, -1], [0, 1, -1]):
if x or y:
change(map, i+x, j+y, rows, cols)
def printMap(map):
for row in map:
print ''.join(row)
def floodFill(map, rows, cols, i, j, target, replace):
if (not isInsideMap(rows, cols, i, j)) or map[i][j] != target:
return
map[i][j] = replace
floodFill(map, rows, cols, i+1, j, target, replace)
floodFill(map, rows, cols, i, j+1, target, replace)
floodFill(map, rows, cols, i-1, j, target, replace)
floodFill(map, rows, cols, i, j-1, target, replace)
def copyChars(map1, map2, rows, cols, char):
for i in xrange(rows):
for j in xrange(cols):
if map1[i][j] == char:
map2[i][j] = char
def replaceChars(map, rows, cols, target, replace):
for i in xrange(rows):
for j in xrange(cols):
if map[i][j] == target:
map[i][j] = replace
def isWin(map, mapPlayer, rows, cols):
m = '\n'.join(''.join(row) for row in map)
n = '\n'.join(''.join(row) for row in mapPlayer)
m = m.replace("*", "X")
n = n.replace(".", "X")
return m == n
Código Python:
Ver originalfrom buscaminas import *
rows = 2
cols = 2
mines = 1
map = makeMap(rows, cols, '.')
playerMap = makeMap(rows, cols, '.')
minePoints = makeMines(mines, rows, cols)
putMines(map, minePoints)
makeNumbers(map, rows, cols)
while True:
printMap(playerMap)
print "Dame un punto"
x = int(raw_input()) - 1
y = int(raw_input()) - 1
if not isInsideMap(rows, cols, x, y):
continue
if map[x][y] == '*':
replaceChars(map, rows, cols, '.', '#')
printMap(map)
print "Perdiste"
break
elif map[x][y] == '.':
floodFill(map, rows, cols, x, y, '.', '#')
copyChars(map, playerMap, rows, cols, '#')
else:
playerMap[x][y] = map[x][y]
if isWin(map, playerMap, rows, cols):
printMap(map)
print "Ganaste"
break
Hace tiempo que ya había hecho un buscaminas pero nunca lo termine, cuando vi tu post me puse a terminarlo.