Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.55 KB | None | 0 0
  1. WHITE = "WHITE"
  2. BLACK = "BLACK"
  3.  
  4.  
  5. '''
  6. correct_coords - Проверка на принадлежность координат промежуткам [0;7]
  7. '''
  8.  
  9.  
  10. def correct_coords(x, y):
  11.     return int(x) < 8 and int(x) >= 0 and int(y) < 8 and int(y) >= 0
  12.  
  13.  
  14. '''
  15. opponent - Вернуть цвет, противоположный данному
  16. '''
  17.  
  18.  
  19. def opponent(color):
  20.     if color == WHITE:
  21.         return BLACK
  22.     else:
  23.         return WHITE
  24.  
  25.  
  26. '''
  27. Базовый класс фигуры
  28. '''
  29.  
  30.  
  31. class Figure:
  32.     def __init__(self, color):
  33.         self.color = color
  34.         self.str_char = "#"
  35.     def get_color(self):
  36.         return self.color
  37.     def char(self):
  38.         return self.str_char
  39.     def can_move(self, board, row, col, row1, col1):
  40.         return False
  41.     def can_attack(self, board, row, col, row1, col1):
  42.         return False
  43.  
  44.  
  45. '''
  46. Пешка
  47. Реализовано всё кроме:
  48. - Превращение в фигуры
  49. '''
  50.  
  51.  
  52. class Pawn(Figure): #Пешка
  53.     def __init__(self, color):
  54.         super().__init__(color)
  55.         self.str_char = "P"
  56.     def can_move(self, board, row, col, row1, col1):
  57.         if col != col1:
  58.             return False
  59.         if self.color == WHITE:
  60.             direction = 1
  61.             start_row = 1
  62.         else:
  63.             direction = -1
  64.             start_row = 6
  65.         if row + direction == row1 and board.field[row+direction][col] is None:
  66.             return True
  67.         if row == start_row and row + 2 * direction == row1 and board.field[row+direction][col] is None:
  68.             return True
  69.         return False
  70.     def can_attack(self, board, row, col, row1, col1):
  71.         direction = 1 if (self.color == WHITE) else -1
  72.         return row + direction == row1 and (col+1 == col1 or col-1 == col1)
  73.  
  74.  
  75. '''
  76. Слон
  77. '''
  78.  
  79.  
  80. class Bishop(Figure):  # Слон
  81.     def __init__(self, color):
  82.         super().__init__(color)
  83.         self.str_char = "B"
  84.     def can_move(self, board, row, col, row1, col1):
  85.         if abs(row1-row) != abs(col1-col):
  86.             return False
  87.         directiony = row1-row
  88.         directionx = col1-col
  89.         signx = 1 if directionx > 0 else -1
  90.         if directiony > 0:
  91.             for i in range(1, directiony+1):
  92.                 if board.field[row+signx*i][col+i] is not None:
  93.                     return False
  94.         else:
  95.             for i in range(1, abs(directiony)+1):
  96.                 if board.field[row+signx*i][col-i] is not None:
  97.                     return False
  98.         return True
  99.     def can_attack(self, board, row, col, row1, col1):
  100.         if abs(row1-row) != abs(col1-col):
  101.             return False
  102.         directiony = row1-row
  103.         directionx = col1-col
  104.         signx = 1 if directionx > 0 else -1
  105.         if directiony > 0:
  106.             for i in range(1, directiony):
  107.                 if board.field[row+signx*i][col+i] is not None:
  108.                     return False
  109.         else:
  110.             for i in range(1, abs(directiony)):
  111.                 if board.field[row+signx*i][col-i] is not None:
  112.                     return False
  113.         return True
  114.  
  115.  
  116. '''
  117. Ладья
  118. '''
  119.  
  120.  
  121. class Rook(Figure):  # Ладья
  122.     def __init__(self, color):
  123.         super().__init__(color)
  124.         self.str_char = "R"
  125.  
  126.     def can_move(self, board, row, col, row1, col1):
  127.         if col1 != col and row1 != row:
  128.             return False
  129.         if col1 != col:
  130.             direction = col1-col
  131.             if direction > 0:
  132.                 sign = 1
  133.                 for i in range(col + 1, col + direction + 1, sign):
  134.                     if board.field[row1][i] is not None:
  135.                         return False
  136.             else:
  137.                 sign = -1
  138.                 for i in range(col + direction, col, sign):
  139.                     if board.field[row1][i] is not None:
  140.                         return False
  141.         elif row1 != row:
  142.             direction = row1-row
  143.             if direction > 0:
  144.                 sign = 1
  145.                 for i in range(row + 1, row + direction + 1, sign):
  146.                     if board.field[i][col1] is not None:
  147.                         return False
  148.             else:
  149.                 sign = -1
  150.                 for i in range(row + direction, row, sign):
  151.                     if board.field[i][col1] is not None:
  152.                         return False
  153.  
  154.         else:
  155.             return False
  156.         return True
  157.  
  158.     def can_attack(self, board, row, col, row1, col1):
  159.         if col1 != col and row1 != row:
  160.             return False
  161.         if col1 != col:
  162.             direction = col1-col
  163.             if direction > 0:
  164.                 sign = 1
  165.                 for i in range(col + 1, col + direction, sign):
  166.                     if board.field[row1][i] is not None:
  167.                         return False
  168.             else:
  169.                 sign = -1
  170.                 for i in range(col + direction + 1, col, sign):
  171.                     if board.field[row1][i] is not None:
  172.                         return False
  173.             if board.field[row1][col1] is not None:
  174.                 return True
  175.         elif row1 != row:
  176.             direction = row1-row
  177.             if direction > 0:
  178.                 sign = 1
  179.                 for i in range(row + 1, row + direction, sign):
  180.                     if board.field[i][col1] is not None:
  181.                         return False
  182.             else:
  183.                 sign = -1
  184.                 for i in range(row + direction + 1, row, sign):
  185.                     if board.field[i][col1] is not None:
  186.                         return False
  187.             if board.field[row1][col1] is not None:
  188.                 return True
  189.         else:
  190.             return False
  191.         return False
  192.  
  193.  
  194. '''
  195. Конь
  196. Не реализовано
  197. '''
  198.  
  199.  
  200. class Knight(Figure): # Конь
  201.     def __init__(self, color):
  202.         super().__init__(color)
  203.         self.str_char = "K"
  204.     def can_move(self, board, row, col, row1, col1):
  205.         return (abs(row1-row) == 1 and abs(col1-col) == 2) or (abs(row1-row) == 2 and abs(col1-col) == 1)
  206.     def can_attack(self, board, row, col, row1, col1):
  207.         return self.can_move(board, row, col, row1, col1)
  208.  
  209.  
  210. '''
  211. Ферзь
  212. Не реализовано
  213. '''
  214.  
  215.  
  216. class Queen(Figure): # Ферзь
  217.     def __init__(self, color):
  218.         super().__init__(color)
  219.         self.str_char = "Q"
  220.  
  221.  
  222. '''
  223. Король
  224. '''
  225.  
  226.  
  227. class King(Figure): # Король
  228.     def __init__(self, color):
  229.         super().__init__(color)
  230.         self.str_char = "*"
  231.     def can_move(self, board, row, col, row1, col1):
  232.         return not (abs(row1-row) > 1 or abs(col1-col) > 1)
  233.     def can_attack(self, board, row, col, row1, col1):
  234.         return self.can_move(board, row, col, row1, col1)
  235.  
  236.  
  237. '''
  238. Класс доски
  239. '''
  240.  
  241.  
  242. class Board:
  243.     def __init__(self, type = "default"):
  244.         self.color = WHITE
  245.         self.field = [[None]*8 for i in range(8)]
  246.         if type == "default":
  247.             self.field[0] = [
  248.                 Rook(WHITE), Knight(WHITE), Bishop(WHITE), Queen(WHITE),
  249.                 King(WHITE), Bishop(WHITE), Knight(WHITE), Rook(WHITE)
  250.             ]
  251.             self.field[1] = [
  252.                 Pawn(WHITE), Pawn(WHITE), Pawn(WHITE), Pawn(WHITE),
  253.                 Pawn(WHITE), Pawn(WHITE), Pawn(WHITE), Pawn(WHITE)
  254.             ]
  255.             self.field[7] = [
  256.                 Rook(BLACK), Knight(BLACK), Bishop(BLACK), Queen(BLACK),
  257.                 King(BLACK), Bishop(BLACK), Knight(BLACK), Rook(BLACK)
  258.             ]
  259.             self.field[6] = [
  260.                 Pawn(BLACK), Pawn(BLACK), Pawn(BLACK), Pawn(BLACK),
  261.                 Pawn(BLACK), Pawn(BLACK), Pawn(BLACK), Pawn(BLACK)
  262.             ]
  263.         elif type == "RookTest":
  264.             self.field[0][0] = Rook(BLACK)
  265.             self.field[7][7] = Rook(WHITE)
  266.         elif type == "BishopTest":
  267.             self.field[3][3] = Bishop(BLACK)
  268.             self.field[4][4] = Bishop(WHITE)
  269.         elif type == "KingTest":
  270.             self.field[3][3] = King(BLACK)
  271.             self.field[4][4] = King(WHITE)
  272.         elif type == "KnightTest":
  273.             self.field[3][3] = Knight(BLACK)
  274.             self.field[4][4] = Knight(WHITE)
  275.  
  276.     def cell(self, row, col):
  277.         piece = self.field[row][col]
  278.         if piece is None:
  279.             return '  '
  280.         color = piece.get_color()
  281.         c = 'w' if color == WHITE else 'b'
  282.         return c + piece.char()
  283.  
  284.     def current_player_color(self):
  285.         return self.color
  286.  
  287.     def move_piece(self, row, col, row1, col1):
  288.         if not correct_coords(row, col) or not correct_coords(row1, col1):
  289.             return False
  290.         if row == row1 and col == col1:
  291.             return False
  292.         piece = self.field[row][col]
  293.         if isinstance(self.field[row1][col1], King):
  294.             return False
  295.         if piece is None:
  296.             return False
  297.         if piece.get_color() != self.color:
  298.             return False
  299.         if self.field[row1][col1] is None:
  300.             if not piece.can_move(self, row, col, row1, col1):
  301.                 return False
  302.         elif self.field[row1][col1].get_color() == opponent(piece.get_color()):
  303.             if not piece.can_attack(self, row, col, row1, col1):
  304.                 return False
  305.         else:
  306.             return False
  307.         self.field[row][col] = None
  308.         self.field[row1][col1] = piece
  309.         self.color = opponent(self.color)
  310.         return True
  311.  
  312.  
  313. def print_board(board):
  314.     print("     +-----+-----+-----+-----+-----+-----+-----+-----+")
  315.     for row in range(7, -1, -1):
  316.         print(' ', row, end=' ')
  317.         for col in range(8):
  318.             print(' |', board.cell(row, col), end=' ')
  319.         print(' |')
  320.         print("     +-----+-----+-----+-----+-----+-----+-----+-----+")
  321.     print(end='        ')
  322.     for col in range(8):
  323.         print(col, end='     ')
  324.  
  325.  
  326. def main():
  327.     board = Board("KnightTest")
  328.     while True:
  329.         print_board(board)
  330.         print('\nКоманды')
  331.         print('exit                           -- выход')
  332.         print('move <row> <col> <row1> <col1> -- ход из клетки (row; col) в клетку (row1; col1)')
  333.         if board.current_player_color() == WHITE:
  334.             print("Ход белых:")
  335.         else:
  336.             print("Ход чёрных:")
  337.         command = input()
  338.         if command == "exit":
  339.             break
  340.         move_type, row, col, row1, col1 = command.split()
  341.         if board.move_piece(int(row), int(col), int(row1), int(col1)):
  342.             print("Ход успешен")
  343.         else:
  344.             print("Попробуйте другой ход!")
  345.  
  346.  
  347. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement