Amazing-Python-Scripts
72 строки · 2.0 Кб
1import random2
3
4def create_grid(size):5return [[' ' for _ in range(size)] for _ in range(size)]6
7
8def place_treasure(grid, size):9row = random.randint(0, size - 1)10col = random.randint(0, size - 1)11grid[row][col] = 'T'12return row, col13
14
15def display_grid(grid):16size = len(grid)17for row in grid:18print(' | '.join(cell.center(3) for cell in row))19print('-' * (size * 5 - 1))20
21
22def move_explorer(grid, row, col, direction):23size = len(grid)24if direction == 'up' and row > 0:25row -= 126elif direction == 'down' and row < size - 1:27row += 128elif direction == 'left' and col > 0:29col -= 130elif direction == 'right' and col < size - 1:31col += 132return row, col33
34
35def grid_explorer(size):36grid = create_grid(size)37explorer_row, explorer_col = random.randint(380, size - 1), random.randint(0, size - 1)39treasure_row, treasure_col = place_treasure(grid, size)40
41print("Welcome to Grid Explorer!")42print("Find the treasure (T) on the grid by navigating in the up, down, left, or right direction.")43print("Enter 'quit' to exit the game.\n")44
45while True:46display_grid(grid)47print(f"Explorer position: ({explorer_row}, {explorer_col})")48move = input("Enter your move (up/down/left/right): ").lower()49
50if move == 'quit':51print("Exiting the game...")52break53
54if move not in ['up', 'down', 'left', 'right']:55print("Invalid move. Try again.")56continue57
58new_explorer_row, new_explorer_col = move_explorer(59grid, explorer_row, explorer_col, move)60
61if grid[new_explorer_row][new_explorer_col] == 'T':62display_grid(grid)63print("Congratulations! You found the treasure!")64break65else:66grid[explorer_row][explorer_col] = ' '67explorer_row, explorer_col = new_explorer_row, new_explorer_col68grid[explorer_row][explorer_col] = 'E'69
70
71if __name__ == "__main__":72grid_explorer(5)73