11import random
22
3+
34def create_grid (size ):
45 return [[' ' for _ in range (size )] for _ in range (size )]
56
7+
68def place_treasure (grid , size ):
79 row = random .randint (0 , size - 1 )
810 col = random .randint (0 , size - 1 )
911 grid [row ][col ] = 'T'
1012 return row , col
1113
14+
1215def display_grid (grid ):
1316 size = len (grid )
1417 for row in grid :
1518 print (' | ' .join (cell .center (3 ) for cell in row ))
1619 print ('-' * (size * 5 - 1 ))
1720
21+
1822def move_explorer (grid , row , col , direction ):
1923 size = len (grid )
2024 if direction == 'up' and row > 0 :
@@ -27,9 +31,11 @@ def move_explorer(grid, row, col, direction):
2731 col += 1
2832 return row , col
2933
34+
3035def grid_explorer (size ):
3136 grid = create_grid (size )
32- explorer_row , explorer_col = random .randint (0 , size - 1 ), random .randint (0 , size - 1 )
37+ explorer_row , explorer_col = random .randint (
38+ 0 , size - 1 ), random .randint (0 , size - 1 )
3339 treasure_row , treasure_col = place_treasure (grid , size )
3440
3541 print ("Welcome to Grid Explorer!" )
@@ -49,7 +55,8 @@ def grid_explorer(size):
4955 print ("Invalid move. Try again." )
5056 continue
5157
52- new_explorer_row , new_explorer_col = move_explorer (grid , explorer_row , explorer_col , move )
58+ new_explorer_row , new_explorer_col = move_explorer (
59+ grid , explorer_row , explorer_col , move )
5360
5461 if grid [new_explorer_row ][new_explorer_col ] == 'T' :
5562 display_grid (grid )
@@ -60,5 +67,6 @@ def grid_explorer(size):
6067 explorer_row , explorer_col = new_explorer_row , new_explorer_col
6168 grid [explorer_row ][explorer_col ] = 'E'
6269
70+
6371if __name__ == "__main__" :
6472 grid_explorer (5 )
0 commit comments