Advertisement
Norvager

L7 v хз

Jun 3rd, 2021
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.43 KB | None | 0 0
  1. import sys
  2. sys.stdin = open('input.txt', 'r')
  3. board=[[-1, -1, -1, -1, -1, -1, -1, -1],
  4.         [-1, -1, -1, -1, -1, -1, -1, -1],
  5.         [-1, -1, -1, -1, -1, -1, -1, -1],
  6.         [-1, -1, -1, -1, -1, -1, -1, -1],
  7.         [-1, -1, -1, -1, -1, -1, -1, -1],
  8.         [-1, -1, -1, -1, -1, -1, -1, -1],
  9.         [-1, -1, -1, -1, -1, -1, -1, -1],
  10.         [-1, -1, -1, -1, -1, -1, -1, -1]]
  11. chessPiece = []
  12. class Figure():
  13.     name = None
  14.     pos = []
  15.     def __init__(self, pos_1, pos_2) :
  16.         if ord(pos_1) < 73 :
  17.             pos_1 = ord(pos_1) - 65
  18.         else :
  19.             pos_1 = ord(pos_1) - 97
  20.         self.pos = [pos_1, pos_2]
  21.         board[pos_1][pos_2] = len(chessPiece)
  22.         pass
  23.     def __del__ (self) :
  24.         board[pos_1][pos_2] = -1
  25.         pass
  26.     def output (self) :
  27.         print(f"{self.name}")
  28.         print(f"Position is {chr(self.pos[0]+65)}{self.pos[1]+1}")
  29.         pass
  30.  
  31. class Pawn(Figure):
  32.     def __init__ (self, pos_1, pos_2, name = "Pawn") :
  33.         """Создание пешки"""
  34.         self.name = name
  35.         super().__init__(pos_1, pos_2)
  36.         print(f"The {self.name} on the board")
  37.         pass
  38.     def __del__ (self) :
  39.         print(f"The {self.name} is dead")
  40.         pass
  41.     def kill_check(self) :
  42.         if self.pos[1] > 4 :
  43.             if self.pos[0] - 1 > -1 :
  44.                 if board[self.pos[0]-1][self.pos[1]-1] != -1 :
  45.                     print(f"{self.name} can kill {chessPiece[board[self.pos[0]-1][self.pos[1]-1]].output()}.")
  46.             if self.pos[0] - 1 < 8 :
  47.                 if board[self.pos[0]+1][self.pos[1]-1] != -1 :
  48.                     print(f"{self.name} can kill {chessPiece[board[self.pos[0]+1][self.pos[1]-1]].output()}.")
  49.         else :
  50.             if self.pos[0] - 1 > -1 :
  51.                 if board[self.pos[0]-1][self.pos[1]-1] != -1 :
  52.                     print(f"{self.name} can kill {chessPiece[board[self.pos[0]-1][self.pos[1]+1]].output()}.")
  53.             if self.pos[0] - 1 < 8 :
  54.                 if board[self.pos[0]+1][self.pos[1]-1] != -1 :
  55.                     print(f"{self.name} can kill {chessPiece[board[self.pos[0]+1][self.pos[1]+1]].output()}.")
  56.  
  57. class Knight(Figure):
  58.     def __init__ (self, pos_1, pos_2, name = "Knight") :
  59.         """Создание коня"""
  60.         self.name = name
  61.         super().__init__(pos_1, pos_2)
  62.         print(f"The {self.name} on the board")
  63.         pass
  64.     def __del__ (self) :
  65.         print("The {self.name} is dead")
  66.         pass
  67.     def kill_check(self):
  68.         if all(self.pos[0]-2>0, self.pos[1]-1>0, board[self.pos[0]-2][self.pos[1]-1]!=-1) :
  69.             print(f"{self.name} can kill {chessPiece[board[self.pos[0]-2][self.pos[1]-1]].output()}.")
  70.         if all(self.pos[0]-2>0, self.pos[1]+1<8, board[self.pos[0]-2][self.pos[1]+1]!=-1) :
  71.             print(f"{self.name} can kill {chessPiece[board[self.pos[0]-2][self.pos[1]+1]].output()}.")
  72.         if all(self.pos[0]+2<8, self.pos[1]-1>0, board[self.pos[0]+2][self.pos[1]-1]!=-1) :
  73.             print(f"{self.name} can kill {chessPiece[board[self.pos[0]+2][self.pos[1]-1]].output()}.")
  74.         if all(self.pos[0]+2<8, self.pos[1]+1<8, board[self.pos[0]+2][self.pos[1]+1]!=-1) :
  75.             print(f"{self.name} can kill {chessPiece[board[self.pos[0]+2][self.pos[1]+1]].output()}.")
  76.         if all(self.pos[1]-2>0, self.pos[0]-1>0, board[self.pos[1]-2][self.pos[0]-1]!=-1) :
  77.             print(f"{self.name} can kill {chessPiece[board[self.pos[1]-2][self.pos[0]-1]].output()}.")
  78.         if all(self.pos[1]-2>0, self.pos[0]+1<8, board[self.pos[1]-2][self.pos[0]+1]!=-1) :
  79.             print(f"{self.name} can kill {chessPiece[board[self.pos[1]-2][self.pos[0]+1]].output()}.")
  80.         if all(self.pos[1]+2<8, self.pos[0]-1>0, board[self.pos[1]+2][self.pos[0]-1]!=-1) :
  81.             print(f"{self.name} can kill {chessPiece[board[self.pos[1]+2][self.pos[0]-1]].output()}.")
  82.         if all(self.pos[1]+2<8, self.pos[0]+1<8, board[self.pos[1]+2][self.pos[0]+1]!=-1) :
  83.             print(f"{self.name} can kill {chessPiece[board[self.pos[1]+2][self.pos[0]+1]].output()}.")
  84.        
  85. class Queen(Figure):
  86.     def __init__ (self, pos_1, pos_2, name = "Queen") :
  87.         """Создание Ферзя"""
  88.         self.name = name
  89.         super().__init__(pos_1, pos_2)
  90.         print(f"The {self.name} on the board")
  91.         pass
  92.     def __del__ (self) :
  93.         print(f"The {self.name} is dead")
  94.         pass
  95.     def kill_check(self) :
  96.         i=self.pos[0]+1
  97.         while i!=7:
  98.             if board[i][self.pos[1]]!=-1:
  99.                 print(f"{self.name} can kill {chessPiece[board[i][self.pos[1]]].name}.")
  100.                 break
  101.             i+=1
  102.         i=self.pos[0]-1
  103.         while i!=0:
  104.             if board[i][self.pos[1]]!=-1:
  105.                 print(f"{self.name} can kill {chessPiece[board[i][self.pos[1]]].output()}.")
  106.                 break
  107.             i-=1
  108.         i=self.pos[1]+1
  109.         while i!=7:
  110.             if board[self.pos[0]][i]!=-1:
  111.                 print(f"{self.name} can kill {chessPiece[board[self.pos[0]][i]].output()}.")
  112.                 break
  113.             i+=1
  114.         i=self.pos[1]-1
  115.         while i!=0:
  116.             if board[self.pos[0]][i]!=-1:
  117.                 print(f"{self.name} can kill {chessPiece[board[self.pos[0]][i]].output()}.")
  118.                 break
  119.             i-=1
  120.         i=self.pos[0]+1; j=self.pos[1]+1
  121.         while i!=7 or j!=7:
  122.             if board[i][j]!=-1:
  123.                print(f"{self.name} can kill {chessPiece[board[i][j]].output()}.")
  124.                break
  125.             i+=1; j+=1
  126.         i=self.pos[0]-1; j=self.pos[1]-1
  127.         while i!=0 or j!=0:
  128.             if board[i][j]!=-1:
  129.                print(f"{self.name} can kill {chessPiece[board[i][j]].output()}.")
  130.                break
  131.             i-=1; j-=1
  132.         i=self.pos[0]-1; j=self.pos[1]+1
  133.         while i!=0 or j!=7:
  134.             if board[i][j]!=-1:
  135.                 print(f"{self.name} can kill {chessPiece[board[i][j]].output()}.")
  136.                 break
  137.             i-=1; j+=1
  138.         i=self.pos[0]+1; j=self.pos[1]-1
  139.         while i!=7 or j!=0:
  140.             if board[i][j]!=-1:
  141.                 print(f"{self.name} can kill {chessPiece[board[i][j]].output()}.")
  142.                 break
  143.             i+=1; j-=1
  144.  
  145.     def __del__ (self) :
  146.         print(f"The {self.name} is dead")
  147.         pass
  148.  
  149. def checkEmpty(pos) :
  150.     if ord(pos[0]) < 73 :
  151.         pos_1 = ord(pos[0]) - 65
  152.     else :
  153.         pos_1 = ord(pos[0]) - 97
  154.     if board[pos_1][int(pos[1]) - 1] == -1 :
  155.         return True
  156.     else :
  157.         return False
  158.  
  159. def chooseOperation() :
  160.     operatoin = ("1 - Add chess piece", "2 - Find kill", "0 - Exit")
  161.     for i in operatoin:
  162.         print(i)
  163.     numOfOperation = int(input())
  164.     return numOfOperation
  165.  
  166. chess = ("1 - Queen", "2 - Pawn", "3 - Knight", "0 - Cancel")
  167. while True :
  168.     choose = chooseOperation()
  169.     if choose == 1 :
  170.         print("Who should I add to the board?")
  171.         for i in chess:
  172.             print(i)
  173.         choosePiece = int(input())
  174.         print("Please use the Latin layout: A-H, a-h, 1-8")
  175.         if choosePiece == 1 :
  176.             print("Where to put the shape?")
  177.             coord = input("Enter the position of the shape. For example: a7 or D4\n")
  178.             if checkEmpty(coord) :
  179.                 chessPiece.append(Queen(coord[0], int(coord[1]) - 1))
  180.             else :
  181.                 print("This place is not free.")
  182.         elif choosePiece == 2 :
  183.             print("Where to put the shape?")
  184.             coord = input("Enter the position of the shape. For example: a7 or D4\n")
  185.             if checkEmpty(coord) :
  186.                 chessPiece.append(Pawn(coord[0], int(coord[1]) - 1))
  187.             else :
  188.                 print("This place is not free.")
  189.         elif choosePiece == 3 :
  190.             print("Where to put the shape?")
  191.             coord = input("Enter the position of the shape. For example: a7 or D4\n")
  192.             if checkEmpty(coord) :
  193.                 chessPiece.append(Knight(coord[0], int(coord[1]) - 1))
  194.             else :
  195.                 print("This place is not free.")
  196.         elif choosePiece == 0 :
  197.             print("Creation canceled!")
  198.         else :
  199.             print("Select the correct option: ")
  200.     elif choose == 2 :
  201.         print("Для какой фигуры ищем цель?")
  202.         for i in range(len(chessPiece)) :
  203.             print(f"{i+1} - {chessPiece[i].name}")
  204.         piece = int(input("\n")) - 1
  205.         chessPiece[piece].kill_check()
  206.     else :
  207.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement