Advertisement
nikminer4sv

Untitled

Nov 24th, 2022
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.15 KB | None | 0 0
  1. import os
  2. from enum import Enum
  3. import time
  4.  
  5. def cls():
  6.     os.system('cls' if os.name=='nt' else 'clear')
  7.  
  8. class Field:
  9.     def __init__(self, n):
  10.         self.player_x = (int)((n - 3) / 2 + 3)
  11.         self.player_y = 1
  12.         self.endpoint_x = (int)(n / 2)
  13.         self.endpoint_y = (int)(n / 2)
  14.         self.player_buffer_letter = "*"
  15.  
  16.         self._initializeFieldStructure(n)
  17.         self._initializeHorizontalAxis(n)
  18.         self._initializeVerticalAxis(n)
  19.         self._initalizePath(n)
  20.         self._initializeCenterLines(n)
  21.         self._initalizeEndPoint(self.endpoint_x, self.endpoint_y)
  22.         self._initializePlayer(self.player_x, self.player_y)
  23.        
  24.        
  25.     def _initializeFieldStructure(self, n):
  26.         if (n % 2 != 0):
  27.             raise Exception("n can't be even!")
  28.         self.field = ['*'] * n
  29.         for i in range(n):
  30.             self.field[i] = [" "] * n
  31.    
  32.     def _initializeHorizontalAxis(self, n):
  33.         for i in range(1, n):
  34.             self.field[0][i] = (i - 1) % 10
  35.  
  36.     def _initializeVerticalAxis(self, n):
  37.         for i in range(1, n):
  38.             self.field[i][0] = (i - 1) % 10
  39.  
  40.     def _initalizeEndPoint(self, x, y):
  41.         self.field[y][x] = "X"
  42.  
  43.     def _initializePlayer(self, x, y):
  44.         self.field[y][x] = "A"
  45.  
  46.     def _initalizePath(self, n):
  47.         for i in range(1, n):
  48.             for j in range(3):
  49.                 self.field[i][(int)((n - 4) / 2) + j + 1] = "*"
  50.                 self.field[(int)((n - 4) / 2) + j + 1][i] = "*"
  51.  
  52.     def _initializeCenterLines(self, n):
  53.         for i in range(2, n - 1):
  54.             for j in range(2, n - 1):
  55.                 self.field[i][(int)(n / 2)] = "D"
  56.                 self.field[(int)(n / 2)][i] = "D"
  57.  
  58.  
  59.  
  60. class Application:
  61.  
  62.     class MoveDirections(Enum):
  63.         TOP = 1,
  64.         RIGHT = 2,
  65.         DOWN = 3,
  66.         LEFT = 4
  67.  
  68.     def __init__(self, n):
  69.         self.n = n
  70.         self.field = Field(n)
  71.         self.movePlan = [self.MoveDirections.DOWN, self.MoveDirections.RIGHT,
  72.                          self.MoveDirections.DOWN, self.MoveDirections.LEFT,
  73.                          self.MoveDirections.DOWN, self.MoveDirections.LEFT,
  74.                          self.MoveDirections.TOP, self.MoveDirections.LEFT,
  75.                          self.MoveDirections.TOP, self.MoveDirections.RIGHT,
  76.                          self.MoveDirections.TOP]
  77.    
  78.         self.defaultLongPartLength = (n - 2) / 2
  79.         self.defaultShortPartLength = 3
  80.         self.currentPartLength = 0
  81.         self.currentMovePlanIndex = 0
  82.         self.partsCounter = 0
  83.         self.isLongPart = True
  84.         self.endMovedRight = False
  85.         self.appEnd = False
  86.  
  87.     def start(self, n):
  88.         pass
  89.    
  90.     def printField(self):
  91.         cls()
  92.         for i in range(self.n):
  93.             for j in range(self.n):
  94.                 print(self.field.field[i][j], end=" ")
  95.             print()
  96.  
  97.     def move(self, l):
  98.         for i in range(l):
  99.             if (self.currentMovePlanIndex == len(self.movePlan)):
  100.                 if (self.endMovedRight == False):
  101.                     self._move(self.MoveDirections.RIGHT)
  102.                     self.endMovedRight = True
  103.                 else:
  104.                     if (self.appEnd != True):
  105.                         self._move(self.MoveDirections.DOWN)
  106.             else:
  107.                 self._move(self.movePlan[self.currentMovePlanIndex])
  108.  
  109.     def _move(self, direction):
  110.         if (direction == self.MoveDirections.DOWN):
  111.             self.field.field[self.field.player_y][self.field.player_x] = self.field.player_buffer_letter
  112.             self.field.player_buffer_letter = self.field.field[self.field.player_y + 1][self.field.player_x]
  113.             self.field.field[self.field.player_y + 1][self.field.player_x] = "A"
  114.             self.field.player_y += 1
  115.         elif (direction == self.MoveDirections.RIGHT):
  116.             self.field.field[self.field.player_y][self.field.player_x] = self.field.player_buffer_letter
  117.             self.field.player_buffer_letter = self.field.field[self.field.player_y][self.field.player_x + 1]
  118.             self.field.field[self.field.player_y][self.field.player_x + 1] = "A"
  119.             self.field.player_x += 1
  120.  
  121.         elif (direction == self.MoveDirections.LEFT):
  122.             self.field.field[self.field.player_y][self.field.player_x] = self.field.player_buffer_letter
  123.             self.field.player_buffer_letter = self.field.field[self.field.player_y][self.field.player_x - 1]
  124.             self.field.field[self.field.player_y][self.field.player_x - 1] = "A"
  125.             self.field.player_x -= 1
  126.  
  127.         elif (direction == self.MoveDirections.TOP):
  128.             self.field.field[self.field.player_y][self.field.player_x] = self.field.player_buffer_letter
  129.             self.field.player_buffer_letter = self.field.field[self.field.player_y - 1][self.field.player_x]
  130.             self.field.field[self.field.player_y - 1][self.field.player_x] = "A"
  131.             self.field.player_y -= 1
  132.  
  133.         if (self.field.player_x == self.field.endpoint_x and self.field.player_y == self.field.endpoint_y):
  134.             self.appEnd = True
  135.             return
  136.  
  137.         if (self.currentMovePlanIndex != len(self.movePlan)):
  138.             self.currentPartLength += 1
  139.             if (self.isLongPart == True):
  140.                 if (self.currentPartLength == self.defaultLongPartLength - 1):
  141.                     self.partsCounter += 1
  142.                     self.currentMovePlanIndex += 1
  143.                     self.currentPartLength = 0
  144.                    
  145.                     if (self.partsCounter == 2):
  146.                         self.isLongPart = False
  147.                         self.partsCounter = 0
  148.             else:
  149.                 if (self.currentPartLength == self.defaultShortPartLength - 1):
  150.                     self.currentMovePlanIndex += 1
  151.                     self.currentPartLength = 0
  152.                     self.isLongPart = True
  153.  
  154.  
  155. n = (int)(input()) + 1
  156.  
  157. entry_point = Application(n)
  158. entry_point.printField()
  159. while (entry_point.appEnd == False):
  160.     m = (int)(input())
  161.     for i in range(m):
  162.         entry_point.move(1)
  163.         time.sleep(0.1)
  164.         entry_point.printField()
  165.  
  166. cls()
  167. print("WIN")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement