Skip to content

Commit 065e0d9

Browse files
authored
Grid_Explorer.py
1 parent c0b68a7 commit 065e0d9

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

Grid Explorer/Grid_Explorer.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import random
2+
3+
def create_grid(size):
4+
return [[' ' for _ in range(size)] for _ in range(size)]
5+
6+
def place_treasure(grid, size):
7+
row = random.randint(0, size - 1)
8+
col = random.randint(0, size - 1)
9+
grid[row][col] = 'T'
10+
return row, col
11+
12+
def display_grid(grid):
13+
size = len(grid)
14+
for row in grid:
15+
print(' | '.join(cell.center(3) for cell in row))
16+
print('-' * (size * 5 - 1))
17+
18+
def move_explorer(grid, row, col, direction):
19+
size = len(grid)
20+
if direction == 'up' and row > 0:
21+
row -= 1
22+
elif direction == 'down' and row < size - 1:
23+
row += 1
24+
elif direction == 'left' and col > 0:
25+
col -= 1
26+
elif direction == 'right' and col < size - 1:
27+
col += 1
28+
return row, col
29+
30+
def grid_explorer(size):
31+
grid = create_grid(size)
32+
explorer_row, explorer_col = random.randint(0, size - 1), random.randint(0, size - 1)
33+
treasure_row, treasure_col = place_treasure(grid, size)
34+
35+
print("Welcome to Grid Explorer!")
36+
print("Find the treasure (T) on the grid by navigating in the up, down, left, or right direction.")
37+
print("Enter 'quit' to exit the game.\n")
38+
39+
while True:
40+
display_grid(grid)
41+
print(f"Explorer position: ({explorer_row}, {explorer_col})")
42+
move = input("Enter your move (up/down/left/right): ").lower()
43+
44+
if move == 'quit':
45+
print("Exiting the game...")
46+
break
47+
48+
if move not in ['up', 'down', 'left', 'right']:
49+
print("Invalid move. Try again.")
50+
continue
51+
52+
new_explorer_row, new_explorer_col = move_explorer(grid, explorer_row, explorer_col, move)
53+
54+
if grid[new_explorer_row][new_explorer_col] == 'T':
55+
display_grid(grid)
56+
print("Congratulations! You found the treasure!")
57+
break
58+
else:
59+
grid[explorer_row][explorer_col] = ' '
60+
explorer_row, explorer_col = new_explorer_row, new_explorer_col
61+
grid[explorer_row][explorer_col] = 'E'
62+
63+
if __name__ == "__main__":
64+
grid_explorer(5)

0 commit comments

Comments
 (0)