Advertisement
viligen

snake

Feb 6th, 2022
987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. def next_position(com, r, c):
  2.     if com == 'up':
  3.         r -= 1
  4.     elif com == 'down':
  5.         r += 1
  6.     elif com == 'left':
  7.         c -= 1
  8.     elif com == 'right':
  9.         c += 1
  10.     return r, c
  11.  
  12.  
  13. def is_inside(r, c, size_):
  14.     if 0 <= r < size_ and 0 <= c < size_:
  15.         return True
  16.     return False
  17.  
  18.  
  19. size = int(input())
  20. field = []
  21. snake_row, snake_col = None, None
  22. burrows = []
  23. food_eaten = 0
  24. for row in range(size):
  25.     field.append(list(input()))
  26.     for col in range(size):
  27.         if field[row][col] == 'S':
  28.             snake_row, snake_col = row, col
  29.         elif field[row][col] == 'B':
  30.             burrows.append((row, col))
  31.  
  32. while food_eaten < 10:
  33.     command = input()
  34.     next_row, next_col = next_position(command, snake_row, snake_col)
  35.     field[snake_row][snake_col] = '.'
  36.     if not is_inside(next_row, next_col, size):
  37.         print("Game over!")
  38.         break
  39.     snake_row, snake_col = next_row, next_col
  40.     if field[next_row][next_col] == '*':
  41.         food_eaten += 1
  42.  
  43.     elif field[next_row][next_col] == 'B':
  44.         field[next_row][next_col] = '.'
  45.         if (next_row, next_col) == burrows[0]:
  46.             snake_row, snake_col = burrows[1]
  47.         else:
  48.             snake_row, snake_col = burrows[0]
  49.     field[snake_row][snake_col] = "S"
  50.  
  51. if food_eaten >= 10:
  52.     print("You won! You fed the snake.")
  53. print(f"Food eaten: {food_eaten}")
  54. for row in field:
  55.     print(*row, sep='')
  56.  
  57.  
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement