Advertisement
uzunovz

Softuniada 2017 - 04. Snake

Jan 11th, 2017
161
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 KB | None
  1. # Get the size of the cube
  2. POINTS = 0
  3.  
  4.  
  5. def move_snake(command):
  6.  
  7.     move_points = 0
  8.     if snake["direction"] == "up":
  9.         for step in range(command[1]):
  10.             snake["floor"] -= 1
  11.             move_points += check_cell()
  12.             if snake["status"] == 0:
  13.                 break
  14.        
  15.     elif snake["direction"] == "down":
  16.         for step in range(command[1]):
  17.             snake["floor"] += 1
  18.             move_points += check_cell()
  19.             if snake["status"] == 0:
  20.                 break
  21.        
  22.     elif snake["direction"] == "forward":
  23.         for step in range(command[1]):
  24.             snake["row"] -= 1
  25.             move_points += check_cell()
  26.             if snake["status"] == 0:
  27.                 break
  28.    
  29.     elif snake["direction"] == "backward":
  30.         for step in range(command[1]):
  31.             snake["row"] += 1
  32.             move_points += check_cell()
  33.             if snake["status"] == 0:
  34.                 break
  35.    
  36.     elif snake["direction"] == "left":
  37.         for step in range(command[1]):
  38.             snake["col"] -= 1
  39.             move_points += check_cell()
  40.             if snake["status"] == 0:
  41.                 break
  42.    
  43.     elif snake["direction"] == "right":
  44.         for step in range(command[1]):
  45.             snake["col"] += 1
  46.             move_points += check_cell()
  47.             if snake["status"] == 0:
  48.                 break
  49.     snake["direction"] = command[0]
  50.  
  51.     return move_points
  52.  
  53.  
  54. def check_cell():
  55.     try:
  56.         cell_value = cube[snake["floor"]][snake["row"]][snake["col"]]
  57.         if cell_value == "a":
  58.             cube[snake["floor"]][snake["row"]][snake["col"]] = "o"
  59.             return 1
  60.         else:
  61.             return 0
  62.  
  63.     except:
  64.         snake["status"] = 0
  65.         return 0
  66.  
  67. if __name__ == '__main__':
  68.     size = int(input())
  69.  
  70.     # Fill the Cube
  71.     cube = [[] for x in range(size)]
  72.  
  73.     for matrix in range(size):
  74.         floor_rows = [[c for c in row] for row in (input().split(" | "))]
  75.         for floor in range(size):
  76.             cube[floor].append(floor_rows[floor])
  77.  
  78.     # Initiate snake
  79.     snake = {
  80.         'floor': 0,
  81.         'row': 0,
  82.         'col': 0,
  83.         'direction': "",
  84.         'status': 1
  85.     }
  86.  
  87.     # get initial direction
  88.     snake["direction"] = input()
  89.  
  90.     # get all commands
  91.     commands = []
  92.  
  93.     while True:
  94.         command = input().split(" ")
  95.         commands.append([command[0], int(command[2])])
  96.  
  97.         if command[0] == "end":
  98.             break
  99.  
  100.     for floor in range(size):
  101.         for row in range(size):
  102.             col = False
  103.             if "s" in cube[floor][row]:
  104.                 col = cube[floor][row].index("s")
  105.  
  106.             if col:
  107.                 snake["floor"] = floor
  108.                 snake["row"] = row
  109.                 snake["col"] = col
  110.  
  111.     for com in commands:
  112.         POINTS += move_snake(com)
  113.         if snake['status'] == 0:
  114.             break
  115.  
  116.     print("Points collected: {}".format(POINTS))
  117.     if snake['status'] == 0:
  118.         print("The snake dies.")
  119.  
  120.  
  121. # directions ["up", "down", "forward", "backward", "left", "right"]
  122. # states ["o", "s", "a"]
Advertisement
RAW Paste Data Copied
Advertisement