Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WHITE = "WHITE"
- BLACK = "BLACK"
- '''
- correct_coords - Проверка на принадлежность координат промежуткам [0;7]
- '''
- def correct_coords(x, y):
- return int(x) < 8 and int(x) >= 0 and int(y) < 8 and int(y) >= 0
- '''
- opponent - Вернуть цвет, противоположный данному
- '''
- def opponent(color):
- if color == WHITE:
- return BLACK
- else:
- return WHITE
- '''
- Базовый класс фигуры
- '''
- class Figure:
- def __init__(self, color):
- self.color = color
- self.str_char = "#"
- def get_color(self):
- return self.color
- def char(self):
- return self.str_char
- def can_move(self, board, row, col, row1, col1):
- return False
- def can_attack(self, board, row, col, row1, col1):
- return False
- '''
- Пешка
- Реализовано всё кроме:
- - Превращение в фигуры
- '''
- class Pawn(Figure): #Пешка
- def __init__(self, color):
- super().__init__(color)
- self.str_char = "P"
- def can_move(self, board, row, col, row1, col1):
- if col != col1:
- return False
- if self.color == WHITE:
- direction = 1
- start_row = 1
- else:
- direction = -1
- start_row = 6
- if row + direction == row1 and board.field[row+direction][col] is None:
- return True
- if row == start_row and row + 2 * direction == row1 and board.field[row+direction][col] is None:
- return True
- return False
- def can_attack(self, board, row, col, row1, col1):
- direction = 1 if (self.color == WHITE) else -1
- return row + direction == row1 and (col+1 == col1 or col-1 == col1)
- '''
- Слон
- '''
- class Bishop(Figure): # Слон
- def __init__(self, color):
- super().__init__(color)
- self.str_char = "B"
- def can_move(self, board, row, col, row1, col1):
- if abs(row1-row) != abs(col1-col):
- return False
- directiony = row1-row
- directionx = col1-col
- signx = 1 if directionx > 0 else -1
- if directiony > 0:
- for i in range(1, directiony+1):
- if board.field[row+signx*i][col+i] is not None:
- return False
- else:
- for i in range(1, abs(directiony)+1):
- if board.field[row+signx*i][col-i] is not None:
- return False
- return True
- def can_attack(self, board, row, col, row1, col1):
- if abs(row1-row) != abs(col1-col):
- return False
- directiony = row1-row
- directionx = col1-col
- signx = 1 if directionx > 0 else -1
- if directiony > 0:
- for i in range(1, directiony):
- if board.field[row+signx*i][col+i] is not None:
- return False
- else:
- for i in range(1, abs(directiony)):
- if board.field[row+signx*i][col-i] is not None:
- return False
- return True
- '''
- Ладья
- '''
- class Rook(Figure): # Ладья
- def __init__(self, color):
- super().__init__(color)
- self.str_char = "R"
- def can_move(self, board, row, col, row1, col1):
- if col1 != col and row1 != row:
- return False
- if col1 != col:
- direction = col1-col
- if direction > 0:
- sign = 1
- for i in range(col + 1, col + direction + 1, sign):
- if board.field[row1][i] is not None:
- return False
- else:
- sign = -1
- for i in range(col + direction, col, sign):
- if board.field[row1][i] is not None:
- return False
- elif row1 != row:
- direction = row1-row
- if direction > 0:
- sign = 1
- for i in range(row + 1, row + direction + 1, sign):
- if board.field[i][col1] is not None:
- return False
- else:
- sign = -1
- for i in range(row + direction, row, sign):
- if board.field[i][col1] is not None:
- return False
- else:
- return False
- return True
- def can_attack(self, board, row, col, row1, col1):
- if col1 != col and row1 != row:
- return False
- if col1 != col:
- direction = col1-col
- if direction > 0:
- sign = 1
- for i in range(col + 1, col + direction, sign):
- if board.field[row1][i] is not None:
- return False
- else:
- sign = -1
- for i in range(col + direction + 1, col, sign):
- if board.field[row1][i] is not None:
- return False
- if board.field[row1][col1] is not None:
- return True
- elif row1 != row:
- direction = row1-row
- if direction > 0:
- sign = 1
- for i in range(row + 1, row + direction, sign):
- if board.field[i][col1] is not None:
- return False
- else:
- sign = -1
- for i in range(row + direction + 1, row, sign):
- if board.field[i][col1] is not None:
- return False
- if board.field[row1][col1] is not None:
- return True
- else:
- return False
- return False
- '''
- Конь
- Не реализовано
- '''
- class Knight(Figure): # Конь
- def __init__(self, color):
- super().__init__(color)
- self.str_char = "K"
- def can_move(self, board, row, col, row1, col1):
- return (abs(row1-row) == 1 and abs(col1-col) == 2) or (abs(row1-row) == 2 and abs(col1-col) == 1)
- def can_attack(self, board, row, col, row1, col1):
- return self.can_move(board, row, col, row1, col1)
- '''
- Ферзь
- Не реализовано
- '''
- class Queen(Figure): # Ферзь
- def __init__(self, color):
- super().__init__(color)
- self.str_char = "Q"
- '''
- Король
- '''
- class King(Figure): # Король
- def __init__(self, color):
- super().__init__(color)
- self.str_char = "*"
- def can_move(self, board, row, col, row1, col1):
- return not (abs(row1-row) > 1 or abs(col1-col) > 1)
- def can_attack(self, board, row, col, row1, col1):
- return self.can_move(board, row, col, row1, col1)
- '''
- Класс доски
- '''
- class Board:
- def __init__(self, type = "default"):
- self.color = WHITE
- self.field = [[None]*8 for i in range(8)]
- if type == "default":
- self.field[0] = [
- Rook(WHITE), Knight(WHITE), Bishop(WHITE), Queen(WHITE),
- King(WHITE), Bishop(WHITE), Knight(WHITE), Rook(WHITE)
- ]
- self.field[1] = [
- Pawn(WHITE), Pawn(WHITE), Pawn(WHITE), Pawn(WHITE),
- Pawn(WHITE), Pawn(WHITE), Pawn(WHITE), Pawn(WHITE)
- ]
- self.field[7] = [
- Rook(BLACK), Knight(BLACK), Bishop(BLACK), Queen(BLACK),
- King(BLACK), Bishop(BLACK), Knight(BLACK), Rook(BLACK)
- ]
- self.field[6] = [
- Pawn(BLACK), Pawn(BLACK), Pawn(BLACK), Pawn(BLACK),
- Pawn(BLACK), Pawn(BLACK), Pawn(BLACK), Pawn(BLACK)
- ]
- elif type == "RookTest":
- self.field[0][0] = Rook(BLACK)
- self.field[7][7] = Rook(WHITE)
- elif type == "BishopTest":
- self.field[3][3] = Bishop(BLACK)
- self.field[4][4] = Bishop(WHITE)
- elif type == "KingTest":
- self.field[3][3] = King(BLACK)
- self.field[4][4] = King(WHITE)
- elif type == "KnightTest":
- self.field[3][3] = Knight(BLACK)
- self.field[4][4] = Knight(WHITE)
- def cell(self, row, col):
- piece = self.field[row][col]
- if piece is None:
- return ' '
- color = piece.get_color()
- c = 'w' if color == WHITE else 'b'
- return c + piece.char()
- def current_player_color(self):
- return self.color
- def move_piece(self, row, col, row1, col1):
- if not correct_coords(row, col) or not correct_coords(row1, col1):
- return False
- if row == row1 and col == col1:
- return False
- piece = self.field[row][col]
- if isinstance(self.field[row1][col1], King):
- return False
- if piece is None:
- return False
- if piece.get_color() != self.color:
- return False
- if self.field[row1][col1] is None:
- if not piece.can_move(self, row, col, row1, col1):
- return False
- elif self.field[row1][col1].get_color() == opponent(piece.get_color()):
- if not piece.can_attack(self, row, col, row1, col1):
- return False
- else:
- return False
- self.field[row][col] = None
- self.field[row1][col1] = piece
- self.color = opponent(self.color)
- return True
- def print_board(board):
- print(" +-----+-----+-----+-----+-----+-----+-----+-----+")
- for row in range(7, -1, -1):
- print(' ', row, end=' ')
- for col in range(8):
- print(' |', board.cell(row, col), end=' ')
- print(' |')
- print(" +-----+-----+-----+-----+-----+-----+-----+-----+")
- print(end=' ')
- for col in range(8):
- print(col, end=' ')
- def main():
- board = Board("KnightTest")
- while True:
- print_board(board)
- print('\nКоманды')
- print('exit -- выход')
- print('move <row> <col> <row1> <col1> -- ход из клетки (row; col) в клетку (row1; col1)')
- if board.current_player_color() == WHITE:
- print("Ход белых:")
- else:
- print("Ход чёрных:")
- command = input()
- if command == "exit":
- break
- move_type, row, col, row1, col1 = command.split()
- if board.move_piece(int(row), int(col), int(row1), int(col1)):
- print("Ход успешен")
- else:
- print("Попробуйте другой ход!")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement