Skip to content

Commit 203b991

Browse files
authored
Merge pull request #7485 from mrodara/mrodara/main
feat: #33 - Python
2 parents 30c958c + 3b8b703 commit 203b991

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
### RESCATANDO A MICKEY
2+
import random as rnd
3+
4+
def generate_maze() -> list :
5+
6+
maze = []
7+
empty_row = ['obstaculo', 'vacio', 'vacio', 'vacio', 'vacio', 'obstaculo']
8+
border_sup_inf = ['obstaculo','obstaculo','obstaculo','obstaculo','obstaculo','obstaculo']
9+
10+
# Generamos el maze vacío y con los obstáculos en los extremos.
11+
maze.append(border_sup_inf.copy())
12+
for i in range(1, 5):
13+
maze.append(empty_row.copy())
14+
maze.append(border_sup_inf.copy())
15+
16+
#Dejamos también vacías las posiciones (1,0) y (0,1) para poder movernos
17+
maze[1][0] = 'vacio'
18+
maze[0][1] = 'vacio'
19+
20+
# Elegimos de manera aleatoria donde vamos a colocar la salida
21+
row_out = 5 # Siempre tomaremos la última fila de momento.
22+
col_out = rnd.randint(1, 5)
23+
maze[row_out][col_out] = 'salida'
24+
25+
# Colocamos de manera aleatoria a Mickey dentro del tablero
26+
row_mickey = rnd.randint(1, 4)
27+
col_mickey = rnd.randint(1, 4)
28+
maze[row_mickey][col_mickey] = 'mickey'
29+
30+
# La posición 0,0 serla el punto de entrada
31+
maze[0][0] = 'vacio'
32+
33+
return maze
34+
35+
def check_obstacle(x: int, y: int, matrix: list) -> bool:
36+
if matrix[x][y] == 'obstaculo':
37+
return True
38+
return False
39+
40+
def check_mickey(x: int, y: int, matrix: list) -> bool:
41+
if matrix[x][y] == 'mickey':
42+
return True
43+
return False
44+
45+
maze = generate_maze()
46+
47+
for row in maze:
48+
print(row)
49+
50+
exit = False
51+
position = [0,0]
52+
founded = False
53+
54+
while not exit:
55+
# Preguntamos al usuario qué acción quiere realizar.
56+
print(f"Tu posición actual es [{position[0]}, {position[1]}]")
57+
action = input("Indica la acción a realizar (u - arriba, d - abajo, l - izquierda, r - derecha, e - exit): ")
58+
while action[0].lower() not in ['u', 'd', 'l', 'r', 'e']:
59+
print("Error: Introduce un valor correcto para el movimiento")
60+
action = input("Indica la acción a realizar (u - arriba, d - abajo, l - izquierda, r - derecha): ")
61+
62+
if action[0].lower() == 'e':
63+
exit = True
64+
break
65+
66+
match action[0].lower():
67+
case 'u':
68+
if check_mickey(x=position[0] - 1, y=position[1], matrix=maze):
69+
founded = True
70+
print('¡Mickey está aquí!')
71+
position[0] -= 1
72+
else:
73+
if not check_obstacle(x=position[0] - 1, y=position[1], matrix=maze):
74+
position[0] -= 1
75+
else:
76+
print('Hay un obstáculo, no te puedes mover ahí')
77+
case 'd':
78+
if check_mickey(x=position[0] + 1, y=position[1], matrix=maze):
79+
founded = True
80+
print('¡Mickey está aquí!')
81+
position[0] += 1
82+
else:
83+
if not check_obstacle(x=position[0] + 1, y=position[1], matrix=maze):
84+
position[0] += 1
85+
else:
86+
print('Hay un obstáculo, no te puedes mover ahí')
87+
case 'l':
88+
if check_mickey(x=position[0], y=position[1] - 1, matrix=maze):
89+
founded = True
90+
print('¡Mickey está aquí!')
91+
position[1] -= 1
92+
else:
93+
if not check_obstacle(x=position[0], y=position[1] - 1, matrix=maze):
94+
position[1] -= 1
95+
else:
96+
print('Hay un obstáculo, no te puedes mover ahí')
97+
case 'r':
98+
if check_mickey(x=position[0], y=position[1] + 1, matrix=maze):
99+
founded = True
100+
print('¡Mickey está aquí!')
101+
position[1] += 1
102+
else:
103+
if not check_obstacle(x=position[0], y=position[1] + 1, matrix=maze):
104+
position[1] += 1
105+
else:
106+
print('Hay un obstáculo, no te puedes mover ahí')
107+
108+
if founded:
109+
print(f'Enhorabuena, pudiste rescatar a Mickey!!!')
110+
print('Fin del juego')
111+
### FIN RESCATANDO A MICKEY

0 commit comments

Comments
 (0)