Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Piece:
- def __init__(self, symbol, color):
- self.symbol = symbol
- self.color = color
- class ChessGame:
- def __init__(self):
- self.board = self.initialize_board()
- def initialize_board(self):
- # Initialize an empty 8x8 chessboard with pieces in their starting positions
- board = [[None for _ in range(8)] for _ in range(8)]
- # Place white pieces
- board[0] = [['♖', 'R'], ['♘', 'N'], ['♗', 'B'], ['♕', 'Q'], ['♔', 'K'], ['♗', 'B'], ['♘', 'N'], ['♖', 'R']]
- board[1] = [['♙', 'P'], ['♙', 'P'], ['♙', 'P'], ['♙', 'P'], ['♙', 'P'], ['♙', 'P'], ['♙', 'P'], ['♙', 'P']]
- # Place black pieces
- board[7] = [['♜', 'r'], ['♞', 'n'], ['♝', 'b'], ['♛', 'q'], ['♚', 'k'], ['♝', 'b'], ['♞', 'n'], ['♜', 'r']]
- board[6] = [['♟', 'p'], ['♟', 'p'], ['♟', 'p'], ['♟', 'p'], ['♟', 'p'], ['♟', 'p'], ['♟', 'p'], ['♟', 'p']]
- return board
- def print_board(self):
- # Print the current state of the chessboard with positions revealed
- print(" +------------------------ A +")
- for row in range(8):
- row_display = f"{8 - row} | "
- for col in range(8):
- if self.board[row][col] != None:
- piece_symbol = self.board[row][col][0]
- else:
- piece_symbol = '.'
- if (piece_symbol == None):
- row_display += ' . '
- else:
- row_display += f" {piece_symbol} "
- row_display += " |"
- print(row_display)
- print(" +------------------------ a +")
- return self.board
- # def print_board(self):
- # # Print the current state of the chessboard with positions revealed
- # print(" +---------------------------------+")
- # for row in range(8):
- # row_display = f"{8 - row} | "
- # for col in range(8):
- # if self.board[row][col] is not None:
- # piece_symbol = self.board[row][col].symbol
- # row_display += f" {piece_symbol} "
- # else:
- # row_display += " "
- # row_display += "|"
- # print(row_display)
- # print(" +---------------------------------+")
- # print(" a b c d e f g h")
- def capture_piece(self, row, col, moving_piece):
- # Remove the piece at the specified position from the board if it belongs to the opposing player
- captured_piece = self.board[row][col]
- if captured_piece != ' ' and ((moving_piece.isupper() and captured_piece.islower()) or (moving_piece.islower() and captured_piece.isupper())):
- self.board[row][col] = ' ' # Remove from the board
- if captured_piece.isupper(): # Captured piece is white
- self.captured_pieces['black'].append(captured_piece.lower()) # Add to black's captured pieces list
- else: # Captured piece is black
- self.captured_pieces['white'].append(captured_piece.upper()) # Add to white's captured pieces list
- def move_piece(self, piece, start_row, start_col, end_row, end_col):
- # Check if the move is valid
- if self.is_valid_move(piece, start_row, start_col, end_row, end_col):
- # Perform the move
- # while True:
- piece_symbol = piece
- if self.board[start_row][start_col][1] == piece_symbol:
- piece_symbol = self.board[start_row][start_col][0]
- self.board[end_row][end_col] = piece_symbol
- self.board[start_row][start_col] = '.'
- print("Move successful!")
- else:
- print("Invalid move!")
- def convert_position(self, position):
- # Convert algebraic notation to array indices
- row = 8 - int(position[1])
- col = ord(position[0]) - ord('a')
- return row, col
- def is_within_board(self, row, col):
- # Check if the given position is within the bounds of the board
- return 0 <= row < 8 and 0 <= col < 8
- def is_valid_move(self, piece, start_row, start_col, end_row, end_col):
- # Check if the move is within the bounds of the board
- if not (0 <= start_row < 8 and 0 <= start_col < 8 and 0 <= end_row < 8 and 0 <= end_col < 8):
- return False
- # Check if there is a piece at the starting position
- if self.board[start_row][start_col] is None:
- return False
- # Check if there is a piece at the starting position
- if self.board[start_row][start_col][1] != piece:
- return False
- # Check if the destination cell contains a friendly piece
- if self.board[end_row][end_col] is not None:
- if self.board[start_row][start_col].islower() == self.board[end_row][end_col].islower():
- return False
- # Check for piece-specific move validation
- if piece == 'P': # Pawn
- # Pawn can move forward two squares from starting position
- # Pawn can move forward one square
- if start_col == end_col and end_row - start_row == 1:
- return True
- if start_row == 1 and start_col == end_col and end_row - start_row == 2:
- return True
- # Pawn can capture diagonally
- if self.board[end_row][end_col] is not None and abs(start_col - end_col) == 1 and end_row - start_row == 1:
- return True
- return False
- elif piece == 'p': # Pawn (for black)
- if start_row == 7 and start_col == end_col and start_row - end_row == 2:
- return True
- # Similar logic for black pawn
- if start_col == end_col and start_row - end_row == 1:
- return True
- if self.board[end_row][end_col] is not None and abs(start_col - end_col) == 1 and start_row - end_row == 1:
- print(self.board[end_row][end_col][0])
- return True
- return False
- elif piece == 'N' or piece == 'n': # Knight
- # Knight moves in an L-shape
- delta_row = abs(end_row - start_row)
- delta_col = abs(end_col - start_col)
- return (delta_row == 2 and delta_col == 1) or (delta_row == 1 and delta_col == 2)
- elif piece == 'R' or piece == 'r': # Rook
- # Rook moves horizontally or vertically
- return start_row == end_row or start_col == end_col
- elif piece == 'B' or piece == 'b': # Bishop
- # Bishop moves diagonally
- return abs(end_row - start_row) == abs(end_col - start_col)
- elif piece == 'Q' or piece == 'q': # Queen
- # Queen combines rook and bishop moves
- return (start_row == end_row or start_col == end_col) or (abs(end_row - start_row) == abs(end_col - start_col))
- elif piece == 'K' or piece == 'k': # King
- # King moves one square in any direction
- return abs(end_row - start_row) <= 1 and abs(end_col - start_col) <= 1
- else:
- return False # Default: invalid move
- if __name__ == "__main__":
- game = ChessGame()
- while True:
- game.print_board()
- move = input("Enter your move (e.g., 'N c3 to e4'): ")
- if move.lower() == 'exit':
- break
- move_parts = move.split()
- piece = move_parts[0]
- start_position = move_parts[1]
- end_position = move_parts[3]
- start_row, start_col = game.convert_position(start_position)
- end_row, end_col = game.convert_position(end_position)
- game.move_piece(piece, start_row, start_col, end_row, end_col)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement