Norvager

L7 vх

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