Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def next_position(com, r, c):
- if com == 'up':
- r -= 1
- elif com == 'down':
- r += 1
- elif com == 'left':
- c -= 1
- elif com == 'right':
- c += 1
- return r, c
- def is_inside(r, c, size_):
- if 0 <= r < size_ and 0 <= c < size_:
- return True
- return False
- size = int(input())
- field = []
- snake_row, snake_col = None, None
- burrows = []
- food_eaten = 0
- for row in range(size):
- field.append(list(input()))
- for col in range(size):
- if field[row][col] == 'S':
- snake_row, snake_col = row, col
- elif field[row][col] == 'B':
- burrows.append((row, col))
- while food_eaten < 10:
- command = input()
- next_row, next_col = next_position(command, snake_row, snake_col)
- field[snake_row][snake_col] = '.'
- if not is_inside(next_row, next_col, size):
- print("Game over!")
- break
- snake_row, snake_col = next_row, next_col
- if field[next_row][next_col] == '*':
- food_eaten += 1
- elif field[next_row][next_col] == 'B':
- field[next_row][next_col] = '.'
- if (next_row, next_col) == burrows[0]:
- snake_row, snake_col = burrows[1]
- else:
- snake_row, snake_col = burrows[0]
- field[snake_row][snake_col] = "S"
- if food_eaten >= 10:
- print("You won! You fed the snake.")
- print(f"Food eaten: {food_eaten}")
- for row in field:
- print(*row, sep='')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement