Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def find_sapper():
- global field
- for x in range(field_size):
- for y in range(field_size):
- if field[x][y] == 's':
- return x, y
- def is_valid(n):
- return 0 <= n < field_size
- DIRECTIONS = {
- 'up': (-1, 0),
- 'right': (0, +1),
- 'down': (+1, 0),
- 'left': (0, -1),
- }
- def next_position(r, c, cc):
- new_r = check_row(r + DIRECTIONS[cc][0])
- new_c = check_col(c + DIRECTIONS[cc][1])
- return new_r, new_c
- def check_row(r: int):
- global field_size
- if r < 0:
- r = 0
- elif r > field_size - 1:
- r = field_size - 1
- return r
- def check_col(c: int):
- global field_size
- if c < 0:
- c = 0
- elif c > field_size - 1:
- c = field_size - 1
- return c
- def find_bombs():
- global field, field_size
- bs = 0
- for x in range(field_size):
- for y in range(field_size):
- if field[x][y] == 'B':
- bs += 1
- return bs
- def move(f, c):
- global end_reached, bombs, sapper
- f[sapper[0]][sapper[1]] = '+'
- next_row, next_col = next_position(sapper[0], sapper[1], c)
- cell = f[next_row][next_col]
- if cell == 'B':
- f[next_row][next_col] = '+'
- bombs -= 1
- print('You found a bomb!')
- elif cell == 'e':
- end_reached = True
- return f
- sapper = next_row, next_col
- return f
- def output():
- global sapper, bombs, end_reached
- if bombs == 0:
- return f'Congratulations! You found all bombs!'
- elif end_reached:
- return f'END! {bombs} bombs left on the field'
- else:
- return f'{bombs} bombs left on the field. Sapper position: ({sapper[0]},{sapper[1]})'
- field_size = int(input())
- if field_size > 1:
- commands = input().split(',')
- field = [input().split() for _ in range(field_size)]
- sapper = find_sapper()
- bombs = find_bombs()
- end_reached = False
- for command in commands:
- field = move(field, command)
- if end_reached:
- break
- print(output())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement