SHOW:
|
|
- or go back to the newest paste.
1 | #!/usr/bin/env python | |
2 | # -*- coding: utf-8 -*- | |
3 | # | |
4 | # Buscaminas.py | |
5 | # Autor: Roberto Díaz Pérez | |
6 | ||
7 | import random, os | |
8 | ||
9 | def cls(): | |
10 | """Para limpiar la pantalla, detecta el sistema operativo y usa el | |
11 | comando adecuado""" | |
12 | if os.name == "nt": | |
13 | os.system("cls") | |
14 | elif os.name == "posix": | |
15 | os.system("clear") | |
16 | ||
17 | def matriz(i,j,s): | |
18 | """Devuelve una matriz de dimensiones i * j rellena de s""" | |
19 | m=[] | |
20 | for a in range(i): | |
21 | m.append([]) | |
22 | for b in range(j): | |
23 | m[a].append(s) | |
24 | return(m) | |
25 | ||
26 | def cont(m,b): | |
27 | """Cuenta la cantidad de veces que se repite b en la matriz m""" | |
28 | c=0 | |
29 | for a in range(len(m)): | |
30 | c=c+m[a].count(b) | |
31 | return(c) | |
32 | ||
33 | def MinSweep(i,j,mines): | |
34 | """Función principal""" | |
35 | campo=matriz(i,j,0) | |
36 | a=0 | |
37 | #Este while rellena la matriz de minas "*". | |
38 | while cont(campo,"*") < mines: | |
39 | campo[random.randint(0,i-1)][random.randint(0,j-1)] = "*" | |
40 | a += 1 | |
41 | #Este if sirve para que si se pusieron más minas que cuadrados | |
42 | #disponibles, te lo diga y se reinicie el código. | |
43 | if mines > (i*j): | |
44 | print("Has puesto más minas que cuadrados disponibles") | |
45 | raw_input() | |
46 | cls() | |
47 | MinSweep(input("¿Columnas?: "), input("¿Filas?: "), input("¿Minas?: ")) | |
48 | #Esta serie de for pone en cada posición de la matriz que no sea una | |
49 | #mina el numero de minas que hay alrededor. | |
50 | for x in range(i): | |
51 | for y in range(j): | |
52 | if campo[x][y] != "*": | |
53 | count=0 | |
54 | for a in [1,0,-1]: | |
55 | for b in [1,0,-1]: | |
56 | try: | |
57 | if campo[x+a][y+b]=="*": | |
58 | if (x+a>-1 and y+b > -1): | |
59 | count += 1 | |
60 | except: | |
61 | pass | |
62 | campo[x][y]=count | |
63 | #Se crea la matriz que se le enseña al usuario | |
64 | campoShow=matriz(i,j,"#") | |
65 | #A partir de este while, es la "interfaz", aquello que se le muestra | |
66 | #al usuario. | |
67 | while True: | |
68 | try: | |
69 | printmin(campo,i,j) #Enseña la matriz resuelta, es algo que | |
70 | #tuve que hacer para que estuviera en el mismo formato que la | |
71 | #matriz campoShow, dado que la función printmin además de imprimir | |
72 | #la matriz que se le manda, tambien la modifica. | |
73 | minasdesc=cont(campoShow,"* ") | |
74 | cls() #Borramos pantalla | |
75 | printmin(campoShow,i,j) | |
76 | #Si las minas restantes por descubrir, y las descubiertas son | |
77 | #iguales y además, no queda ningún "# " en campoShow, se termina | |
78 | #el juego | |
79 | if mines-minasdesc == 0 and campoShow.count("# ") == 0: | |
80 | break | |
81 | print("Quedan %i minas" %(mines-minasdesc)) | |
82 | i1=int(raw_input("Fila: ")) | |
83 | j1=int(raw_input("Columna: ")) | |
84 | campoShow=clic(campo,campoShow,i1,j1 ,raw_input("¿Mina? (s/n): ")) #Si el raw_input recibe una "s" | |
85 | #hará el efecto del click derecho (marcar que ahí hay una mina), en caso contrario, hace click | |
86 | #izquierdo (arriesgarte a ver si hay mina). | |
87 | if campoShow[i1-1][j1-1]=="0 ": | |
88 | #Si el punto en el que aplicamos la función click, es un 0 | |
89 | #automáticamente se aplica click alrededor de ese punto. | |
90 | for a in [1,0,-1]: | |
91 | for b in [1,0,-1]: | |
92 | try: | |
93 | print((i1+a-1>-1 and j1+b-1 > 0)) | |
94 | if (i1+a-1>-1 and j1+b-1 > 0): | |
95 | campoShow=clic(campo,campoShow,i1+a,j1+b ,"n") | |
96 | except: | |
97 | pass | |
98 | except: | |
99 | print("¡Pon bien los datos!") | |
100 | raw_input() | |
101 | cls() | |
102 | printmin(campo,i,j) | |
103 | print(""" GANASTE!!!!""") | |
104 | raw_input() | |
105 | main() | |
106 | ||
107 | def clic(campo,campoShow,i=1,j=1,mina="n"): | |
108 | """Función que simula un click""" | |
109 | i=int(i)-1 | |
110 | j=int(j)-1 | |
111 | if mina=="s": | |
112 | campoShow[i][j]="*" | |
113 | else: | |
114 | if campo[i][j] == "*" or campo[i][j] =="* ": | |
115 | cls() | |
116 | print("BOOM!") | |
117 | printmin(campo,len(campo),len(campo[0])) | |
118 | raw_input() | |
119 | main() | |
120 | elif campoShow[i][j] != "0 ": | |
121 | campoShow[i][j]=campo[i][j] | |
122 | if campoShow[i][j]=="0 ": | |
123 | for a in [1,0,-1]: | |
124 | for b in [1,0,-1]: | |
125 | try: | |
126 | if (i+a>-1 and j+b > -1): | |
127 | campoShow=clic(campo,campoShow,i+1+a,j+1+b ,"n") | |
128 | except: | |
129 | pass | |
130 | return(campoShow) | |
131 | ||
132 | ||
133 | def printmin(campo,i,j): | |
134 | """Función que imprime la matriz en un formato agradable para el usuario""" | |
135 | campoprint=list(campo) | |
136 | x=range(i+1) | |
137 | for j in range(i+1): | |
138 | x[j]=str(x[j])+" " | |
139 | print "".join(x) | |
140 | for p in range(len(campoprint)): | |
141 | for t in range(len(campoprint[0])): | |
142 | if len(str(campoprint[p][t])) == 1: | |
143 | campoprint[p][t]=str(campoprint[p][t])+" " | |
144 | a=[] | |
145 | print str(p+1)+ " " + "".join(campoprint[p]) | |
146 | ||
147 | def main(): | |
148 | cls() | |
149 | - | MinSweep(input("¿Columnas?: "), input("¿Filas?: "), input("¿Minas?: ")) |
149 | + | try: |
150 | MinSweep(int(raw_input("¿Columnas?: ")), int(raw_input("¿Filas?: ")), int(raw_input("¿Minas?: "))) | |
151 | except: | |
152 | print("Error al introducir los datos") | |
153 | raw_input() | |
154 | main() | |
155 | ||
156 | main() |