Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
928
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. area = [
  2.     ['[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]'],
  3.     ['[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]'],
  4.     ['[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]'],
  5.     ['[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]'],
  6.     ['[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]'],
  7.     ['[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]'],
  8.     ['[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]'],
  9.     ['[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]'],
  10.     ['[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]'],
  11.     ['[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]'],
  12.     ['[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]', '[ ]'],
  13. ]
  14.  
  15. class Ant:
  16.     def __init__(self, y, x, direction):
  17.         self.y = y
  18.         self.x = x
  19.         self.direction = direction
  20.  
  21.     def change_area(self):
  22.         if area[self.y][self.x] == '[ ]':
  23.             area[self.y][self.x] = '[#]'
  24.  
  25.         elif area[self.y][self.x] == '[#]':
  26.             area[self.y][self.x] = '[ ]'
  27.  
  28.     def change_location(self):
  29.         if self.direction == 1:
  30.             self.y -= 1
  31.         elif self.direction == 2:
  32.             self.x += 1
  33.         elif self.direction == 3:
  34.             self.y += 1
  35.         elif self.direction == 4:
  36.             self.x -= 1
  37.  
  38.     def choose_turn_angle(self):
  39.         if area[self.y][self.x] == '[ ]':
  40.             return True
  41.         else:
  42.             return False
  43.  
  44.     def right_turn(self):
  45.         self.direction += 1
  46.         if self.direction == 5:
  47.             self.direction = 1
  48.     def left_turn(self):
  49.         self.direction -= 1
  50.         if self.direction == 0:
  51.             self.direction = 4
  52.  
  53.     def show_area(self):
  54.         for row in area:
  55.             print(" ".join(map(str, row)))  # analyze
  56.     def show_testing_data(self):
  57.         print(f"y = {self.y} | x = {self.x} | kierunkowy = {self.direction}")
  58.  
  59.  
  60. ant = Ant(5, 5, 1)
  61.  
  62. timer = 1
  63. while timer<=200:
  64.    
  65.     ant.change_area()
  66.     ant.change_location()
  67.    
  68.     if ant.choose_turn_angle():
  69.         ant.right_turn()
  70.     else:
  71.         ant.left_turn()
  72.  
  73.     timer += 1
  74.  
  75.  
  76. ant.show_area()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement