Advertisement
dzocesrce

[VI] Pacman

Jan 21st, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.86 KB | None | 0 0
  1. class Pacman(Problem):
  2.     def __init__(self, initial, house=None):
  3.         super().__init__(initial, house)
  4.  
  5.     def goal_test(self, state):
  6.         return len(state)==0
  7.  
  8.     def successor(self, state):
  9.         succ = {}
  10.         pac_x=state[0]
  11.         pac_y=state[1]
  12.         nasoka=state[2]
  13.         foods=state[3]
  14.         new_foods=[]
  15.  
  16.         if nasoka=='istok' and self.check_valid(pac_x+1,pac_y):
  17.             if (pac_x+1,pac_y) in foods:
  18.                 for f in foods:
  19.                     if f[0]==pac_x+1 and f[1]==pac_y:
  20.                         continue
  21.                         new_foods.append(f)
  22.  
  23.         succ["ProdolzhiPravo"]= [(pac_x+1,pac_y),nasoka,new_foods]
  24.  
  25.         if nasoka=='zapad' and self.check_valid(pac_x-1,pac_y):
  26.             if (pac_x-1,pac_y) in foods:
  27.                 for f in foods:
  28.                     if f[0]==pac_x-1 and f[1]==pac_y:
  29.                         continue
  30.                         new_foods.append(f)
  31.  
  32.         succ["ProdolzhiPravo"]= [(pac_x-1,pac_y),nasoka,new_foods]
  33.  
  34.         if nasoka=='sever' and self.check_valid(pac_x,pac_y+1):
  35.             if (pac_x,pac_y+1) in foods:
  36.                 for f in foods:
  37.                     if f[0]==pac_x and f[1]==pac_y+1:
  38.                         continue
  39.                         new_foods.append(f)
  40.  
  41.         succ["ProdolzhiPravo"]= [(pac_x,pac_y+1),nasoka,new_foods]
  42.  
  43.         if nasoka=='jug' and self.check_valid(pac_x,pac_y-1):
  44.             if (pac_x,pac_y-1) in foods:
  45.                 for f in foods:
  46.                     if f[0]==pac_x and f[1]==pac_y-1:
  47.                         continue
  48.                         new_foods.append(f)
  49.  
  50.         succ["ProdolzhiPravo"]= [(pac_x,pac_y-1),nasoka,new_foods]
  51.  
  52.         if nasoka=='istok' and self.check_valid(pac_x-1,pac_y):
  53.             if (pac_x-1,pac_y) in foods:
  54.                 for f in foods:
  55.                     if f[0] == pac_x-1 and f[1] == pac_y:
  56.                         continue
  57.                         new_foods.append(f)
  58.  
  59.         succ["ProdolzhiNazad"] = [(pac_x-1, pac_y), 'zapad', new_foods]
  60.  
  61.  
  62.         if nasoka=='zapad' and self.check_valid(pac_x+1,pac_y):
  63.             if (pac_x+1,pac_y) in foods:
  64.                 for f in foods:
  65.                     if f[0] == pac_x+1 and f[1] == pac_y:
  66.                         continue
  67.                         new_foods.append(f)
  68.  
  69.         succ["ProdolzhiNazad"] = [(pac_x-1, pac_y), 'istok', new_foods]
  70.  
  71.         if nasoka=='sever' and self.check_valid(pac_x,pac_y-1):
  72.             if (pac_x,pac_y-1) in foods:
  73.                 for f in foods:
  74.                     if f[0] == pac_x and f[1] == pac_y-1:
  75.                         continue
  76.                         new_foods.append(f)
  77.  
  78.         succ["ProdolzhiNazad"] = [(pac_x, pac_y-1), 'jug', new_foods]
  79.  
  80.         if nasoka=='jug' and self.check_valid(pac_x,pac_y+1):
  81.             if (pac_x,pac_y+1) in foods:
  82.                 for f in foods:
  83.                     if f[0] == pac_x and f[1] == pac_y+1:
  84.                         continue
  85.                         new_foods.append(f)
  86.  
  87.         succ["ProdolzhiNazad"] = [(pac_x, pac_y+1), 'sever', new_foods]
  88.  
  89.         if nasoka=='istok' and self.check_valid(pac_x,pac_y+1):
  90.             if (pac_x,pac_y+1) in foods:
  91.                 for f in foods:
  92.                     if f[0] == pac_x and f[1] == pac_y+1:
  93.                         continue
  94.                         new_foods.append(f)
  95.  
  96.             succ["SvrtiLevo"] = [(pac_x, pac_y+1), 'sever', new_foods]
  97.  
  98.         if nasoka=='zapad' and self.check_valid(pac_x,pac_y-1):
  99.             if (pac_x,pac_y-1) in foods:
  100.                 for f in foods:
  101.                     if f[0] == pac_x and f[1] == pac_y-1:
  102.                         continue
  103.                         new_foods.append(f)
  104.  
  105.             succ["SvrtiLevo"] = [(pac_x, pac_y-1), 'jug', new_foods]
  106.  
  107.         if nasoka=='sever' and self.check_valid(pac_x-1,pac_y):
  108.             if (pac_x-1,pac_y) in foods:
  109.                 for f in foods:
  110.                     if f[0] == pac_x-1 and f[1] == pac_y:
  111.                         continue
  112.                         new_foods.append(f)
  113.  
  114.             succ["SvrtiLevo"] = [(pac_x-1, pac_y), 'zapad', new_foods]
  115.  
  116.         if nasoka=='jug' and self.check_valid(pac_x+1,pac_y):
  117.             if (pac_x+1,pac_y) in foods:
  118.                 for f in foods:
  119.                     if f[0] == pac_x+1 and f[1] == pac_y:
  120.                         continue
  121.                         new_foods.append(f)
  122.  
  123.             succ["SvrtiLevo"] = [(pac_x+1, pac_y), 'istok', new_foods]
  124.  
  125.         if nasoka=='istok' and self.check_valid(pac_x,pac_y-1):
  126.             if (pac_x,pac_y-1) in foods:
  127.                 for f in foods:
  128.                     if f[0]== pac_x and f[1] == pac[y]-1:
  129.                         continue
  130.                         new_foods.append(f)
  131.  
  132.             succ["SvrtiDesno"] = [(pac_x, pac_y-1), 'jug', new_foods]
  133.  
  134.         if nasoka == 'zapad' and self.check_valid(pac_x, pac_y + 1):
  135.             if (pac_x, pac_y + 1) in foods:
  136.                 for f in foods:
  137.                     if f[0] == pac_x and f[1] == pac[y] + 1:
  138.                         continue
  139.                         new_foods.append(f)
  140.  
  141.             succ["SvrtiDesno"] = [(pac_x, pac_y + 1), 'sever', new_foods]
  142.  
  143.         if nasoka == 'sever' and self.check_valid(pac_x+1,pac_y):
  144.             if (pac_x+1,pac_y) in foods:
  145.                 for f in foods:
  146.                     if f[0] == pac_x+1 and f[1] == pac[y]:
  147.                         continue
  148.                         new_foods.append(f)
  149.  
  150.             succ["SvrtiDesno"] = [(pac_x+1, pac_y), 'istok', new_foods]
  151.  
  152.         if nasoka == 'jug' and self.check_valid(pac_x - 1, pac_y):
  153.             if (pac_x - 1, pac_y) in foods:
  154.                 for f in foods:
  155.                     if f[0] == pac_x - 1 and f[1] == pac[y]:
  156.                         continue
  157.                         new_foods.append(f)
  158.  
  159.             succ["SvrtiDesno"] = [(pac_x - 1, pac_y), 'zapad', new_foods]
  160.  
  161.  
  162.  
  163.         return succ
  164.  
  165.     @staticmethod
  166.     def check_valid(state):
  167.         # print(state)
  168.         if state[0]<0 or state[0]>9 or state[1]<0 or state[1]>9:
  169.                 return False
  170.         if state in ((0,9),(1,9),(2,9),(3,9),(6,9),(0,8),(8,8),(9,8),(4,7),(8,7),(9,7),(0,6),(3,6),(4,6),(5,6),(4,5),(1,4),(8,4),(9,4),(1,3),(1,2),(6,2),(4,1),(5,1),(6,1),(8,1),(6,0)):
  171.             return False
  172.         return True
  173.     def actions(self, state):
  174.         return self.successor(state).keys()
  175.  
  176.     def result(self, state, action):
  177.         return self.successor(state)[action]
  178.  
  179. if __name__ == "__main__":
  180.  
  181.     pac_x= int(input())
  182.     pac_y= int(input())
  183.     nasoka= input()
  184.     n= int(input())
  185.     foods= []
  186.     for _ in range(n):
  187.         x, y = map(int, input().split(','))
  188.         foods.append((x, y))
  189.  
  190.     pacman= Pacman((pac_x,pac_y,nasoka,foods))
  191.     answer=breadth_first_graph_search(pacman)
  192.  
  193.     if answer is None:
  194.         print("None")
  195.     else:
  196.         print(answer.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement