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', 'b'], ['♘', 'N', 'b'], ['♗', 'B', 'b'], ['♕', 'Q', 'b'], ['♔', 'K', 'b'], ['♗', 'B', 'b'], ['♘', 'N', 'b'], ['♖', 'R', 'b']]
- board[1] = [['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b']]
- # Place black pieces
- board[7] = [['♜', 'r', 'w'], ['♞', 'n', 'w'], ['♝', 'b', 'w'], ['♛', 'q', 'w'], ['♚', 'k', 'w'], ['♝', 'b', 'w'], ['♞', 'n', 'w'], ['♜', 'r', 'w']]
- board[6] = [['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w']]
- 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 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
- self.board[end_row][end_col] = self.board[start_row][start_col]
- 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 for obstruction in the path
- # Check for obstruction in the path
- if piece in ['R', 'r', 'Q', 'q']: # Rook or Queen
- if start_row == end_row: # Horizontal move
- delta_col = 1 if end_col > start_col else -1
- for col in range(start_col + delta_col, end_col, delta_col):
- if self.board[start_row][col] is not None:
- return False # Obstruction found
- elif start_col == end_col: # Vertical move
- delta_row = 1 if end_row > start_row else -1
- for row in range(start_row + delta_row, end_row, delta_row):
- if self.board[row][start_col] is not None:
- return False # Obstruction found
- if piece in ['B', 'b', 'Q', 'q']: # Bishop or Queen
- delta_row = end_row - start_row
- delta_col = end_col - start_col
- if abs(delta_row) == abs(delta_col): # Diagonal move
- row_step = 1 if delta_row > 0 else -1
- col_step = 1 if delta_col > 0 else -1
- row = start_row + row_step
- col = start_col + col_step
- while row != end_row and col != end_col:
- if self.board[row][col] is not None:
- return False # Obstruction found
- row += row_step
- col += col_step
- # Check if there's a piece of the same color at the ending position
- if self.board[end_row][end_col] is not None and self.board[end_row][end_col][2] == self.board[start_row][start_col][2]:
- return False # Obstruction found
- # Check for piece-specific move validation
- if piece == 'P': # Pawn
- # Pawn can move forward two squares from starting position
- if start_row == 1 and start_col == end_col and end_row - start_row == 2:
- return True
- if start_col == end_col and end_row - start_row == 1:
- return True
- # Pawn can capture diagonally
- if self.board[end_row][end_col] is not None and self.board[start_row][start_col][2] != self.board[end_row][end_col][2] and abs(start_col - end_col) == 1 and end_row - start_row == 1:
- return True
- return False
- elif piece == 'p': # Pawn (for black)
- # Pawn can move forward two squares from starting position
- if start_row == 6 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