Advertisement
Norvager

L7 v final

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