Advertisement
575

chess v2

575
Aug 24th, 2022
1,277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.80 KB | None | 0 0
  1. x_axis = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
  2. y_axis = ('8', '7', '6', '5', '4', '3', '2', '1')
  3. pieces = ('p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'r1', 'r2', 'n1', 'n2', 'b1', 'b2', 'q', 'k')
  4.  
  5. def DrawPiece(piece, color):
  6.     if color == 'w':
  7.         print(piece[: 1].upper(), end = ' ')
  8.     else:
  9.         print(piece[: 1], end = ' ')
  10.  
  11.  
  12. def DrawField(white_pieces, black_pieces):
  13.     global x_axis
  14.     global y_axis
  15.     global pieces
  16.     is_drown = False
  17.    
  18.     print('  A B C D E F G H', '\n')
  19.    
  20.     for y in y_axis:
  21.         print(y, end = ' ')
  22.  
  23.         for x in x_axis:
  24.             position = [x, y]
  25.  
  26.             for piece in pieces:
  27.                 if white_pieces[piece] == position:
  28.                     DrawPiece(piece, 'w')
  29.                     is_drown = True
  30.                 elif black_pieces[piece] == position:
  31.                     DrawPiece(piece, 'b')
  32.                     is_drown = True
  33.                 else:
  34.                     if is_drown:
  35.                         break
  36.             if not is_drown:
  37.                 print('.', end = ' ')
  38.             is_drown = False
  39.  
  40.         print(y)
  41.  
  42.     print('\n', ' A B C D E F G H', '\n')
  43.  
  44.  
  45. def CheckChoice(pos, color, white_pieces, black_pieces):
  46.     global pieces
  47.  
  48.     if color == 'w':
  49.         for piece in pieces:
  50.             if white_pieces[piece] == pos:
  51.                 return piece
  52.         return None
  53.     else:
  54.         for piece in pieces:
  55.             if black_pieces[piece] == pos:
  56.                 return piece
  57.         return None
  58.  
  59.  
  60. def CheckEmptySquare(pos, white_pieces, black_pieces):
  61.     global pieces
  62.     for piece in pieces:
  63.         if pos == white_pieces[piece] or pos == black_pieces[piece]:
  64.             return False
  65.     return True
  66.  
  67.  
  68. def CheckPosToGo(pos):
  69.     global x_axis
  70.     global y_axis
  71.  
  72.     if pos[0] not in x_axis or pos[1] not in y_axis or len(pos) != 2:
  73.         return False
  74.     else:
  75.         return True
  76.  
  77.  
  78. def CheckPieceColor(pos):
  79.     global pieces
  80.     for piece in pieces:
  81.         if pos == white_pieces[piece]:
  82.             return 'w'
  83.         elif pos == black_pieces[piece]:
  84.             return 'b'
  85.     return None
  86.  
  87.  
  88. def CheckRoute(pos, pos_to_go, white_pieces, black_pieces):
  89.     global x_axis
  90.     global y_axis
  91.  
  92.     check_pos = pos.copy()
  93.     if pos[0] == pos_to_go[0]:
  94.         for i in range(abs(y_axis.index(pos[1]) - y_axis.index(pos_to_go[1])) - 1):
  95.             if y_axis.index(pos[1]) > y_axis.index(pos_to_go[1]):
  96.                 check_pos[1] = y_axis[y_axis.index(pos[1]) - 1 - i]
  97.                 if not CheckEmptySquare(check_pos, white_pieces, black_pieces):
  98.                     return False
  99.             else:
  100.                 check_pos[1] = y_axis[y_axis.index(pos[1]) + 1 + i]
  101.                 if not CheckEmptySquare(check_pos, white_pieces, black_pieces):
  102.                     return False
  103.         return True
  104.  
  105.     elif pos[1] == pos_to_go[1]:
  106.         for i in range(abs(x_axis.index(pos[0]) - x_axis.index(pos_to_go[0])) - 1):
  107.             if x_axis.index(pos[0]) > x_axis.index(pos_to_go[0]):
  108.                 check_pos[0] = x_axis[x_axis.index(pos[0]) - 1 - i]
  109.                 if not CheckEmptySquare(check_pos, white_pieces, black_pieces):
  110.                     return False
  111.             else:
  112.                 check_pos[0] = x_axis[x_axis.index(pos[0]) + 1 + i]
  113.                 if not CheckEmptySquare(check_pos, white_pieces, black_pieces):
  114.                     return False
  115.         return True
  116.  
  117.     elif abs(x_axis.index(pos[0]) - x_axis.index(pos_to_go[0])) == abs(y_axis.index(pos[1]) - y_axis.index(pos_to_go[1])):
  118.         for i in range(abs(y_axis.index(pos[1]) - y_axis.index(pos_to_go[1])) - 1):
  119.             if y_axis.index(pos[1]) > y_axis.index(pos_to_go[1]):
  120.                 check_pos[1] = y_axis[y_axis.index(pos[1]) - 1 - i]
  121.             else:
  122.                 check_pos[1] = y_axis[y_axis.index(pos[1]) + 1 + i]
  123.  
  124.             if x_axis.index(pos[0]) > x_axis.index(pos_to_go[0]):
  125.                 check_pos[0] = x_axis[x_axis.index(pos[0]) - 1 - i]
  126.             else:
  127.                 check_pos[0] = x_axis[x_axis.index(pos[0]) + 1 + i]
  128.  
  129.             if not CheckEmptySquare(check_pos, white_pieces, black_pieces):
  130.                 return False
  131.         return True
  132.  
  133.  
  134. def CheckMove(pos, pos_to_go, piece, color, white_pieces, black_pieces):
  135.     global x_axis
  136.     global y_axis
  137.     global pieces
  138.     is_empty = CheckEmptySquare(pos_to_go, white_pieces, black_pieces)
  139.  
  140.     if piece[0] == 'p':
  141.         if color == 'w':
  142.             if x_axis.index(pos[0]) - x_axis.index(pos_to_go[0]) == 0 and y_axis.index(pos[1]) - y_axis.index(pos_to_go[1]) == 1 and is_empty:
  143.                 return True
  144.             elif x_axis.index(pos[0]) - x_axis.index(pos_to_go[0]) == 0 and y_axis.index(pos[1]) - y_axis.index(pos_to_go[1]) == 2 and pos[1] == '2' and is_empty and CheckEmptySquare([pos[0], '3'], white_pieces, black_pieces):
  145.                 return True
  146.             elif abs(x_axis.index(pos[0]) - x_axis.index(pos_to_go[0])) == 1 and y_axis.index(pos[1]) - y_axis.index(pos_to_go[1]) == 1 and not is_empty:
  147.                 if CheckPieceColor(pos_to_go) == 'b':
  148.                     return True
  149.                 else:
  150.                     return False
  151.             else:
  152.                 return False
  153.         else:
  154.             if x_axis.index(pos[0]) - x_axis.index(pos_to_go[0]) == 0 and y_axis.index(pos[1]) - y_axis.index(pos_to_go[1]) == -1 and is_empty:
  155.                 return True
  156.             elif x_axis.index(pos[0]) - x_axis.index(pos_to_go[0]) == 0 and y_axis.index(pos[1]) - y_axis.index(pos_to_go[1]) == -2 and pos[1] == '7' and is_empty and CheckEmptySquare([pos[0], '6'], white_pieces, black_pieces):
  157.                 return True
  158.             elif abs(x_axis.index(pos[0]) - x_axis.index(pos_to_go[0])) == 1 and y_axis.index(pos[1]) - y_axis.index(pos_to_go[1]) == -1 and not is_empty:
  159.                 if CheckPieceColor(pos_to_go) == 'w':
  160.                     return True
  161.                 else:
  162.                     return False
  163.             else:
  164.                 return False
  165.  
  166.     elif piece[0] == 'n':
  167.         if color == 'w':
  168.             if (abs(x_axis.index(pos[0]) - x_axis.index(pos_to_go[0])) == 2 and abs(y_axis.index(pos[1]) - y_axis.index(pos_to_go[1])) == 1) or (abs(x_axis.index(pos[0]) - x_axis.index(pos_to_go[0])) == 1 and abs(y_axis.index(pos[1]) - y_axis.index(pos_to_go[1])) == 2):
  169.                 if is_empty or CheckPieceColor(pos_to_go) == 'b':
  170.                     return True
  171.                 else:
  172.                     return False
  173.             else:
  174.                 return False
  175.         else:
  176.             if (abs(x_axis.index(pos[0]) - x_axis.index(pos_to_go[0])) == 2 and abs(y_axis.index(pos[1]) - y_axis.index(pos_to_go[1])) == 1) or (abs(x_axis.index(pos[0]) - x_axis.index(pos_to_go[0])) == 1 and abs(y_axis.index(pos[1]) - y_axis.index(pos_to_go[1])) == 2):
  177.                 if is_empty or CheckPieceColor(pos_to_go) == 'w':
  178.                     return True
  179.                 else:
  180.                     return False
  181.  
  182.     elif piece[0] == 'r':
  183.         if color == 'w':
  184.             if (pos[0] == pos_to_go[0] or pos[1] == pos_to_go[1]) and CheckRoute(pos, pos_to_go, white_pieces, black_pieces):
  185.                 if is_empty or CheckPieceColor(pos_to_go) == 'b':
  186.                     return True
  187.                 else:
  188.                     return False
  189.             else:
  190.                 return False
  191.         else:
  192.             if (pos[0] == pos_to_go[0] or pos[1] == pos_to_go[1]) and CheckRoute(pos, pos_to_go, white_pieces, black_pieces):
  193.                 if is_empty or CheckPieceColor(pos_to_go) == 'w':
  194.                     return True
  195.                 else:
  196.                     return False
  197.             else:
  198.                 return False
  199.  
  200.     elif piece[0] == 'b':
  201.         if color == 'w':
  202.             if abs(x_axis.index(pos[0]) - x_axis.index(pos_to_go[0])) == abs(y_axis.index(pos[1]) - y_axis.index(pos_to_go[1])) and CheckRoute(pos, pos_to_go, white_pieces, black_pieces):
  203.                 if is_empty or CheckPieceColor(pos_to_go) == 'b':
  204.                     return True
  205.                 else:
  206.                     return False
  207.             else:
  208.                 return False
  209.         else:
  210.             if abs(x_axis.index(pos[0]) - x_axis.index(pos_to_go[0])) == abs(y_axis.index(pos[1]) - y_axis.index(pos_to_go[1])) and CheckRoute(pos, pos_to_go, white_pieces, black_pieces):
  211.                 if is_empty or CheckPieceColor(pos_to_go) == 'w':
  212.                     return True
  213.                 else:
  214.                     return False
  215.             else:
  216.                 return False
  217.  
  218.     elif piece[0] == 'q':
  219.         if color == 'w':
  220.             if abs(x_axis.index(pos[0]) - x_axis.index(pos_to_go[0])) == abs(y_axis.index(pos[1]) - y_axis.index(pos_to_go[1])) or (pos[0] == pos_to_go[0] or pos[1] == pos_to_go[1]) and CheckRoute(pos, pos_to_go, white_pieces, black_pieces):
  221.                 if is_empty or CheckPieceColor(pos_to_go) == 'b':
  222.                     return True
  223.                 else:
  224.                     return False
  225.             else:
  226.                 return False
  227.         else:
  228.             if abs(x_axis.index(pos[0]) - x_axis.index(pos_to_go[0])) == abs(y_axis.index(pos[1]) - y_axis.index(pos_to_go[1])) or (pos[0] == pos_to_go[0] or pos[1] == pos_to_go[1]) and CheckRoute(pos, pos_to_go, white_pieces, black_pieces):
  229.                 if is_empty or CheckPieceColor(pos_to_go) == 'w':
  230.                     return True
  231.                 else:
  232.                     return False
  233.             else:
  234.                 return False
  235.  
  236.     elif piece[0] == 'k':
  237.         if color == 'w':
  238.             if abs(x_axis.index(pos[0]) - x_axis.index(pos_to_go[0])) <= 1 and abs(y_axis.index(pos[1]) - y_axis.index(pos_to_go[1])) <= 1 and CheckRoute(pos, pos_to_go, white_pieces, black_pieces):
  239.                 if is_empty or CheckPieceColor(pos_to_go) == 'b':
  240.                     can_move = True
  241.                     for piece in pieces:
  242.                         if black_pieces[piece] != [None, None]:
  243.                             if CheckMove(black_pieces[piece], pos_to_go, piece, 'b', white_pieces, black_pieces):
  244.                                 can_move = False
  245.                                 break
  246.                     if can_move:
  247.                         return True
  248.                     else:
  249.                         return False
  250.         else:
  251.             if abs(x_axis.index(pos[0]) - x_axis.index(pos_to_go[0])) <= 1 and abs(y_axis.index(pos[1]) - y_axis.index(pos_to_go[1])) <= 1 and CheckRoute(pos, pos_to_go, white_pieces, black_pieces):
  252.                 if is_empty or CheckPieceColor(pos_to_go) == 'w':
  253.                     can_move = True
  254.                     for piece in pieces:
  255.                         if white_pieces[piece] != [None, None]:
  256.                             if CheckMove(white_pieces[piece], pos_to_go, piece, 'w', white_pieces, black_pieces):
  257.                                 can_move = False
  258.                                 break
  259.                     if can_move:
  260.                         return True
  261.                     else:
  262.                         return False
  263.  
  264.  
  265.  
  266. white_pieces = {'p1' : ['a', '2'], 'p2' : ['b', '2'], 'p3' : ['c', '2'], 'p4' : ['d', '2'], 'p5' : ['e', '2'], 'p6' : ['f', '2'], 'p7' : ['g', '2'], 'p8' : ['h', '2'],
  267.                 'r1' : ['a', '1'], 'r2' : ['h', '1'], 'n1' : ['b', '1'], 'n2' : ['g', '1'], 'b1' : ['c', '1'], 'b2' : ['f', '1'], 'q' : ['d', '1'], 'k' : ['e', '1']}
  268.  
  269. black_pieces = {'p1' : ['a', '7'], 'p2' : ['b', '7'], 'p3' : ['c', '7'], 'p4' : ['d', '7'], 'p5' : ['e', '7'], 'p6' : ['f', '7'], 'p7' : ['g', '7'], 'p8' : ['h', '7'],
  270.                 'r1' : ['a', '8'], 'r2' : ['h', '8'], 'n1' : ['b', '8'], 'n2' : ['g', '8'], 'b1' : ['c', '8'], 'b2' : ['f', '8'], 'q' : ['d', '8'], 'k' : ['e', '8']}
  271.  
  272. is_w_k_moved = False
  273. is_w_r1_moved = False
  274. is_w_r2_moved = False
  275. is_b_k_moved = False
  276. is_b_r1_moved = False
  277. is_b_r2_moved = False
  278.  
  279. history = [[white_pieces.copy(), black_pieces.copy(), is_w_k_moved, is_w_r1_moved, is_w_r2_moved, is_b_k_moved, is_b_r1_moved, is_b_r2_moved]]
  280.  
  281. color = ('w', 'b')
  282. c = 0
  283.  
  284. while True:
  285.     DrawField(white_pieces, black_pieces)
  286.  
  287.     if color[c % 2] == 'w':
  288.         pos = input('ход белых, выберите фигуру: ')
  289.     else:
  290.         pos = input('ход черных, выберите фигуру: ')
  291.  
  292.     if pos[0] != '-':
  293.         pos = list(pos)
  294.         picked_piece = CheckChoice(pos, color[c % 2], white_pieces, black_pieces)
  295.         while picked_piece == None:
  296.             pos = list(input('на этом месте нет вашей фигуры, введите другую позицию: '))
  297.             picked_piece = CheckChoice(pos, color[c % 2], white_pieces, black_pieces)
  298.  
  299.         pos_to_go = list(input('введите позицию, на которую пойдет фигура: '))
  300.  
  301.         while not CheckPosToGo(pos_to_go):
  302.             pos_to_go = list(input('такой позиции нет на шахматной доске, введите другую: '))
  303.         correct_move = CheckMove(pos, pos_to_go, picked_piece, color[c % 2], white_pieces, black_pieces)
  304.    
  305.         while not correct_move:
  306.             pos_to_go = list(input('ваша фигура не может сделать такой ход, выберите другую позицию: '))
  307.             while not CheckPosToGo(pos_to_go):
  308.                 pos_to_go = list(input('такой позиции нет на шахматной доске, введите другую: '))
  309.             correct_move = CheckMove(pos, pos_to_go, picked_piece, color[c % 2], white_pieces, black_pieces)
  310.    
  311.         if not CheckEmptySquare(pos_to_go, white_pieces, black_pieces):
  312.             piece_to_kill = CheckChoice(pos_to_go, color[(c + 1) % 2], white_pieces, black_pieces)
  313.             if color[c % 2] == 'w':
  314.                 black_pieces[piece_to_kill] = [None, None]
  315.             else:
  316.                 white_pieces[piece_to_kill] = [None, None]
  317.  
  318.         if color[c % 2] == 'w':
  319.             white_pieces[picked_piece] = pos_to_go
  320.         else:
  321.             black_pieces[picked_piece] = pos_to_go
  322.         if color[c % 2] == 'w':
  323.             if picked_piece == 'k':
  324.                 is_w_k_moved = True
  325.             elif picked_piece == 'r1':
  326.                 is_w_r1_moved = True
  327.             elif picked_piece == 'r2':
  328.                 is_w_r2_moved = True
  329.         else:
  330.             if picked_piece == 'k':
  331.                 is_b_k_moved = True
  332.             elif picked_piece == 'r1':
  333.                 is_b_r1_moved = True
  334.             elif picked_piece == 'r2':
  335.                 is_b_r2_moved = True
  336.  
  337.         c += 1
  338.         history.append([white_pieces.copy(), black_pieces.copy(), is_w_k_moved, is_w_r1_moved, is_w_r2_moved, is_b_k_moved, is_b_r1_moved, is_b_r2_moved])
  339.  
  340.     elif pos == '-back':
  341.         print('выберите количество ходов на которое хотите вернуться (максимум', c, 'ходов): ', end = '')
  342.         q_of_moves = int(input())
  343.        
  344.         while q_of_moves > c:
  345.             print('вы выбрали неправильное количество ходов, введите новое значение (максимум', c, 'ходов): ', end = '')
  346.             q_of_moves = int(input())
  347.  
  348.         white_pieces = history[c - q_of_moves][0]
  349.         black_pieces = history[c - q_of_moves][1]
  350.         is_w_k_moved = history[c - q_of_moves][2]
  351.         is_w_r1_moved = history[c - q_of_moves][3]
  352.         is_w_r2_moved = history[c - q_of_moves][4]
  353.         is_b_k_moved = history[c - q_of_moves][5]
  354.         is_b_r1_moved = history[c - q_of_moves][6]
  355.         is_b_r2_moved = history[c - q_of_moves][7]
  356.         history = history[: c - q_of_moves]
  357.         c -= q_of_moves
  358.  
  359.     elif pos == '-castling':
  360.         if color[c % 2] == 'w':
  361.             if is_w_k_moved == True or (is_w_r1_moved and is_w_r2_moved):
  362.                 print('рокировка невозможна, выберите другой ход')
  363.             else:
  364.                 if not is_w_r1_moved and CheckRoute(['a', '1'], ['e', '1'], white_pieces, black_pieces):
  365.                     print('вам доступна длинная рокировка, введите -long чтобы выполнить её')
  366.                 if not is_w_r2_moved and CheckRoute(['h', '1'], ['e', '1'], white_pieces, black_pieces):
  367.                     print('вам доступна короткая рокировка, введите -short чтобы выполнить её')
  368.                 type = input('введите тип рокеровки:')
  369.                
  370.                 if type == '-long':
  371.                     white_pieces['k'] = ['c', '1']
  372.                     white_pieces['r1'] = ['d', '1']
  373.                 else:
  374.                     white_pieces['k'] = ['g', '1']
  375.                     white_pieces['r2'] = ['f', '1']
  376.         else:
  377.             if is_b_k_moved == True or (is_b_r1_moved and is_b_r2_moved):
  378.                 print('рокировка невозможна, выберите другой ход')
  379.             else:
  380.                 if not is_b_r1_moved and CheckRoute(['a', '8'], ['e', '8'], white_pieces, black_pieces):
  381.                     print('вам доступна длинная рокировка, введите -long чтобы выполнить её')
  382.                 if not is_b_r2_moved and CheckRoute(['h', '8'], ['e', '8'], white_pieces, black_pieces):
  383.                     print('вам доступна короткая рокировка, введите -short чтобы выполнить её')
  384.  
  385.                 type = input('введите тип рокировки:')
  386.  
  387.                 if type == '-long':
  388.                     black_pieces['k'] = ['c', '8']
  389.                     black_pieces['r1'] = ['d', '8']
  390.                 else:
  391.                     black_pieces['k'] = ['g', '8']
  392.                     black_pieces['r2'] = ['f', '8']
  393.                 c += 1
  394.                    
  395.  
  396.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement