Skip to content

Grid_Explorer.py #2666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions Grid Explorer/Grid_Explorer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import random

def create_grid(size):
return [[' ' for _ in range(size)] for _ in range(size)]

def place_treasure(grid, size):
row = random.randint(0, size - 1)
col = random.randint(0, size - 1)
grid[row][col] = 'T'
return row, col

def display_grid(grid):
size = len(grid)
for row in grid:
print(' | '.join(cell.center(3) for cell in row))
print('-' * (size * 5 - 1))

def move_explorer(grid, row, col, direction):
size = len(grid)
if direction == 'up' and row > 0:
row -= 1
elif direction == 'down' and row < size - 1:
row += 1
elif direction == 'left' and col > 0:
col -= 1
elif direction == 'right' and col < size - 1:
col += 1
return row, col

def grid_explorer(size):
grid = create_grid(size)
explorer_row, explorer_col = random.randint(0, size - 1), random.randint(0, size - 1)
treasure_row, treasure_col = place_treasure(grid, size)

print("Welcome to Grid Explorer!")
print("Find the treasure (T) on the grid by navigating in the up, down, left, or right direction.")
print("Enter 'quit' to exit the game.\n")

while True:
display_grid(grid)
print(f"Explorer position: ({explorer_row}, {explorer_col})")
move = input("Enter your move (up/down/left/right): ").lower()

if move == 'quit':
print("Exiting the game...")
break

if move not in ['up', 'down', 'left', 'right']:
print("Invalid move. Try again.")
continue

new_explorer_row, new_explorer_col = move_explorer(grid, explorer_row, explorer_col, move)

if grid[new_explorer_row][new_explorer_col] == 'T':
display_grid(grid)
print("Congratulations! You found the treasure!")
break
else:
grid[explorer_row][explorer_col] = ' '
explorer_row, explorer_col = new_explorer_row, new_explorer_col
grid[explorer_row][explorer_col] = 'E'

if __name__ == "__main__":
grid_explorer(5)
35 changes: 35 additions & 0 deletions Grid Explorer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Package/Script Name: Grid Explorer Game

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.

Functionalities:

Create a grid of a specified size with an explorer ('E') and a hidden treasure ('T').
Display the current state of the grid in the terminal with the explorer's position.
Allow the player to make valid moves by navigating the explorer in up, down, left, or right directions.
Inform the player when they find the treasure ('T').
Allow the player to quit the game at any time.
Setup Instructions:

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/
Copy and paste the provided script into a new file named grid_explorer.py or any desired name.
Save the file in the desired directory.
How to Run the Script:

Open a terminal or command prompt.
Navigate to the directory where you saved the grid_explorer.py file using the cd command.
Run the script by entering the following command:
Copy code
python grid_explorer.py
Detailed Explanation:
The script defines several functions for creating the grid, placing the treasure randomly, displaying the grid, moving the explorer, and managing the game loop.

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.

Output:
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.

As this is a text-based game, there are no images, gifs, or videos to display for the output.

Author:
Shikhar9425