Skip to content

Commit 4242935

Browse files
Merge pull request #2666 from Shikhar9425/master-6
Grid_Explorer.py
2 parents 3c8dcf3 + b3c523c commit 4242935

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-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)

Grid Explorer/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Package/Script Name: Grid Explorer Game
2+
3+
Short Description: This is a simple Python script for playing the "Grid Explorer" game in a text-based format. The game simulates an explorer navigating through a grid in search of a hidden treasure represented by 'T'. The explorer can move in four directions (up, down, left, right) and needs to find the treasure by making valid moves.
4+
5+
Functionalities:
6+
7+
Create a grid of a specified size with an explorer ('E') and a hidden treasure ('T').
8+
Display the current state of the grid in the terminal with the explorer's position.
9+
Allow the player to make valid moves by navigating the explorer in up, down, left, or right directions.
10+
Inform the player when they find the treasure ('T').
11+
Allow the player to quit the game at any time.
12+
Setup Instructions:
13+
14+
Make sure you have Python installed on your system. If you don't have it, you can download it from the official website: https://www.python.org/downloads/
15+
Copy and paste the provided script into a new file named grid_explorer.py or any desired name.
16+
Save the file in the desired directory.
17+
How to Run the Script:
18+
19+
Open a terminal or command prompt.
20+
Navigate to the directory where you saved the grid_explorer.py file using the cd command.
21+
Run the script by entering the following command:
22+
Copy code
23+
python grid_explorer.py
24+
Detailed Explanation:
25+
The script defines several functions for creating the grid, placing the treasure randomly, displaying the grid, moving the explorer, and managing the game loop.
26+
27+
The main grid_explorer function initializes the grid, places the explorer and treasure randomly, and enters the game loop. The player is prompted to enter the direction (up, down, left, right) to move the explorer. The script validates the input, checks if the explorer found the treasure, and continues the game until the player finds the treasure or quits.
28+
29+
Output:
30+
When running the script, the output will be text-based, displaying the current state of the grid with the explorer represented by 'E', the treasure represented by 'T', and messages for the player's moves and the game's progress. The script will print the grid after each player's move.
31+
32+
As this is a text-based game, there are no images, gifs, or videos to display for the output.
33+
34+
Author:
35+
Shikhar9425

0 commit comments

Comments
 (0)