From 065e0d925cd627e0b559ee7370ffa932ae2b8306 Mon Sep 17 00:00:00 2001 From: Shikhar9425 <72194627+Shikhar9425@users.noreply.github.com> Date: Sun, 6 Aug 2023 16:36:36 +0530 Subject: [PATCH 1/2] Grid_Explorer.py --- Grid Explorer/Grid_Explorer.py | 64 ++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Grid Explorer/Grid_Explorer.py diff --git a/Grid Explorer/Grid_Explorer.py b/Grid Explorer/Grid_Explorer.py new file mode 100644 index 0000000000..3e4a3d0cef --- /dev/null +++ b/Grid Explorer/Grid_Explorer.py @@ -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) From b3c523c59682a0933dcd3056782971bb134f365e Mon Sep 17 00:00:00 2001 From: Shikhar9425 <72194627+Shikhar9425@users.noreply.github.com> Date: Sun, 6 Aug 2023 16:40:00 +0530 Subject: [PATCH 2/2] README.md --- Grid Explorer/README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Grid Explorer/README.md diff --git a/Grid Explorer/README.md b/Grid Explorer/README.md new file mode 100644 index 0000000000..872703683a --- /dev/null +++ b/Grid Explorer/README.md @@ -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