Advertisement
dzocesrce

[VI] Football

Jan 21st, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.94 KB | None | 0 0
  1. class Football(Problem):
  2.     def __init__(self, initial, goal=None):
  3.         super().__init__(initial, goal)
  4.  
  5.     def actions(self, state):
  6.         return self.successor(state).keys()
  7.  
  8.     def result(self, state, action):
  9.         return self.successor(state)[action]
  10.  
  11.     def goal_test(self, state):
  12.         ball_pos = state[1]
  13.         return ball_pos in self.goal
  14.  
  15.     @staticmethod
  16.     def check_valid(state, opponents):
  17.         man_pos = state[0]
  18.         ball_pos = state[1]
  19.         #print(ball_pos)
  20.         return man_pos[0] >= 0 and man_pos[0] < 8 and \
  21.             man_pos[1] >= 0 and man_pos[1] < 6 and \
  22.             ball_pos[0] >= 0 and ball_pos[0] < 8 and \
  23.             ball_pos[1] >= 0 and ball_pos[1] < 6 and ball_pos not in \
  24.             ((2, 2), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4), (4, 2), (4, 3), (4, 4), (3, 5), (3, 6), (4, 5), (4, 6),(5, 4), (5, 5), (5, 6))
  25.  
  26.     def successor(self, state):
  27.         successors = dict()
  28.  
  29.         man_pos = state[0]
  30.         ball_pos = state[1]
  31.         opp_pos = state[2]
  32.  
  33.         if self.check_valid(((man_pos[0], man_pos[1]+1), ball_pos), opp_pos):
  34.             successors["Pomesti coveche gore"] = ((man_pos[0], man_pos[1]+1), ball_pos, opp_pos)
  35.  
  36.         if self.check_valid(((man_pos[0], man_pos[1]-1), ball_pos), opp_pos):
  37.             successors["Pomesti coveche dolu"] = ((man_pos[0], man_pos[1]-1), ball_pos, opp_pos)
  38.  
  39.         if self.check_valid(((man_pos[0] + 1, man_pos[1]), ball_pos), opp_pos):
  40.             successors["Pomesti coveche desno"] = ((man_pos[0] + 1, man_pos[1]), ball_pos, opp_pos)
  41.  
  42.         if self.check_valid(((man_pos[0] + 1, man_pos[1] + 1), ball_pos), opp_pos):
  43.             successors["Pomesti coveche gore-desno"] = ((man_pos[0] + 1, man_pos[1] + 1), ball_pos, opp_pos)
  44.  
  45.         if self.check_valid(((man_pos[0]+1, man_pos[1]-1), ball_pos), opp_pos):
  46.             successors["Pomesti coveche dolu-desno"] = ((man_pos[0] + 1, man_pos[1] - 1), ball_pos, opp_pos)
  47.  
  48.         if man_pos[0] == ball_pos[0] and man_pos[1] == ball_pos[1] - 1 and self.check_valid(
  49.                 ((man_pos[0], man_pos[1] + 1), (ball_pos[0], ball_pos[1] + 1)), opp_pos):
  50.             successors["Turni topka gore"] = ((man_pos[0], man_pos[1] + 1), (ball_pos[0], ball_pos[1] + 1), opp_pos)
  51.  
  52.         if man_pos[0] == ball_pos[0] and man_pos[1] == ball_pos[1] + 1 and self.check_valid(
  53.                 ((man_pos[0], man_pos[1] - 1), (ball_pos[0], ball_pos[1] - 1)), opp_pos):
  54.             successors["Turni topka dolu"] = ((man_pos[0], man_pos[1] - 1), (ball_pos[0], ball_pos[1] - 1), opp_pos)
  55.  
  56.         if man_pos[1] == ball_pos[1] and man_pos[0] == ball_pos[0] - 1 and self.check_valid(
  57.                 ((man_pos[0] + 1, man_pos[1]), (ball_pos[0] + 1, ball_pos[1])), opp_pos):
  58.             successors["Turni topka desno"] = ((man_pos[0] + 1, man_pos[1]), (ball_pos[0] + 1, ball_pos[1]), opp_pos)
  59.  
  60.         if man_pos[1] == ball_pos[1] - 1 and man_pos[0] == ball_pos[0] - 1 and self.check_valid(
  61.                 ((man_pos[0] + 1, man_pos[1] + 1), (ball_pos[0] + 1, ball_pos[1] + 1)), opp_pos):
  62.             successors["Turni topka gore-desno"] = ((man_pos[0] + 1, man_pos[1] + 1), (ball_pos[0] + 1, ball_pos[1] + 1), opp_pos)
  63.  
  64.         if man_pos[1] == ball_pos[1] + 1 and man_pos[0] == ball_pos[0] - 1 and self.check_valid(
  65.                 ((man_pos[0] + 1, man_pos[1] - 1), (ball_pos[0] + 1, ball_pos[1] - 1)), opp_pos):
  66.             successors["Turni topka dolu-desno"] = ((man_pos[0] + 1, man_pos[1] - 1), (ball_pos[0] + 1, ball_pos[1] - 1), opp_pos)
  67.  
  68.         return successors
  69.  
  70.  
  71. if __name__ == '__main__':
  72.     man_pos = tuple(map(int, input().split(',')))
  73.     ball_pos = tuple(map(int, input().split(',')))
  74.  
  75.     opponents = ((3, 3), (5, 4))
  76.  
  77.     goal_state = ((7, 2), (7, 3))
  78.  
  79.     football = Football((man_pos, ball_pos, opponents), goal_state)
  80.  
  81.     answer = breadth_first_graph_search(football)
  82.     if answer is None:
  83.         print("None")
  84.     else:
  85.         print(answer.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement