Advertisement
onexiv

Draft #2

Mar 4th, 2024
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.75 KB | None | 0 0
  1. class Piece:
  2.     def __init__(self, symbol, color):
  3.         self.symbol = symbol
  4.         self.color = color
  5.  
  6. class ChessGame:
  7.     def __init__(self):
  8.         self.board = self.initialize_board()
  9.  
  10.     def initialize_board(self):
  11.         # Initialize an empty 8x8 chessboard with pieces in their starting positions
  12.         board = [[None for _ in range(8)] for _ in range(8)]
  13.        
  14.         # Place white pieces
  15.         board[0] = [['♖', 'R'], ['♘', 'N'], ['♗', 'B'], ['♕', 'Q'], ['♔', 'K'], ['♗', 'B'], ['♘', 'N'], ['♖', 'R']]
  16.         board[1] = [['♙', 'P'], ['♙', 'P'], ['♙', 'P'], ['♙', 'P'], ['♙', 'P'], ['♙', 'P'], ['♙', 'P'], ['♙', 'P']]
  17.  
  18.         # Place black pieces
  19.         board[7] = [['♜', 'r'], ['♞', 'n'], ['♝', 'b'], ['♛', 'q'], ['♚', 'k'], ['♝', 'b'], ['♞', 'n'], ['♜', 'r']]
  20.         board[6] = [['♟', 'p'], ['♟', 'p'], ['♟', 'p'], ['♟', 'p'], ['♟', 'p'], ['♟', 'p'], ['♟', 'p'], ['♟', 'p']]
  21.  
  22.         return board
  23.  
  24.     def print_board(self):
  25.         # Print the current state of the chessboard with positions revealed
  26.         print("  +------------------------ A +")
  27.         for row in range(8):
  28.             row_display = f"{8 - row} | "
  29.             for col in range(8):
  30.                 if self.board[row][col] != None:
  31.                     piece_symbol = self.board[row][col][0]
  32.                 else:
  33.                     piece_symbol = '.'
  34.                 if (piece_symbol == None):
  35.                     row_display += ' . '
  36.                 else:
  37.                     row_display += f" {piece_symbol} "
  38.             row_display += " |"
  39.             print(row_display)
  40.         print("  +------------------------ a +")
  41.  
  42.         return self.board
  43.  
  44.     # def print_board(self):
  45.     #     # Print the current state of the chessboard with positions revealed
  46.     #     print("  +---------------------------------+")
  47.     #     for row in range(8):
  48.     #         row_display = f"{8 - row} | "
  49.     #         for col in range(8):
  50.     #             if self.board[row][col] is not None:
  51.     #                 piece_symbol = self.board[row][col].symbol
  52.     #                 row_display += f" {piece_symbol} "
  53.     #             else:
  54.     #                 row_display += "   "
  55.     #         row_display += "|"
  56.     #         print(row_display)
  57.     #     print("  +---------------------------------+")
  58.     #     print("    a     b     c     d     e     f     g     h")
  59.  
  60.     def capture_piece(self, row, col, moving_piece):
  61.         # Remove the piece at the specified position from the board if it belongs to the opposing player
  62.         captured_piece = self.board[row][col]
  63.         if captured_piece != ' ' and ((moving_piece.isupper() and captured_piece.islower()) or (moving_piece.islower() and captured_piece.isupper())):
  64.             self.board[row][col] = ' '  # Remove from the board
  65.             if captured_piece.isupper():  # Captured piece is white
  66.                 self.captured_pieces['black'].append(captured_piece.lower())  # Add to black's captured pieces list
  67.             else:  # Captured piece is black
  68.                 self.captured_pieces['white'].append(captured_piece.upper())  # Add to white's captured pieces list
  69.  
  70.  
  71.     def move_piece(self, piece, start_row, start_col, end_row, end_col):
  72.         # Check if the move is valid
  73.         if self.is_valid_move(piece, start_row, start_col, end_row, end_col):
  74.             # Perform the move
  75.             # while True:
  76.             piece_symbol = piece
  77.             if self.board[start_row][start_col][1] == piece_symbol:
  78.                 piece_symbol = self.board[start_row][start_col][0]
  79.             self.board[end_row][end_col] = piece_symbol
  80.             self.board[start_row][start_col] = '.'
  81.             print("Move successful!")
  82.         else:
  83.             print("Invalid move!")
  84.  
  85.     def convert_position(self, position):
  86.         # Convert algebraic notation to array indices
  87.         row = 8 - int(position[1])
  88.         col = ord(position[0]) - ord('a')
  89.         return row, col
  90.  
  91.     def is_within_board(self, row, col):
  92.         # Check if the given position is within the bounds of the board
  93.         return 0 <= row < 8 and 0 <= col < 8
  94.  
  95.     def is_valid_move(self, piece, start_row, start_col, end_row, end_col):
  96.         # Check if the move is within the bounds of the board
  97.         if not (0 <= start_row < 8 and 0 <= start_col < 8 and 0 <= end_row < 8 and 0 <= end_col < 8):
  98.             return False
  99.        
  100.         # Check if there is a piece at the starting position
  101.         if self.board[start_row][start_col] is None:
  102.             return False
  103.  
  104.         # Check if there is a piece at the starting position
  105.         if self.board[start_row][start_col][1] != piece:
  106.             return False
  107.  
  108.         # Check if the destination cell contains a friendly piece
  109.         if self.board[end_row][end_col] is not None:
  110.             if self.board[start_row][start_col].islower() == self.board[end_row][end_col].islower():
  111.                 return False
  112.  
  113.         # Check for piece-specific move validation
  114.         if piece == 'P':  # Pawn
  115.             # Pawn can move forward two squares from starting position
  116.             # Pawn can move forward one square
  117.             if start_col == end_col and end_row - start_row == 1:
  118.                 return True
  119.             if start_row == 1 and start_col == end_col and end_row - start_row == 2:
  120.                 return True
  121.             # Pawn can capture diagonally
  122.             if self.board[end_row][end_col] is not None and abs(start_col - end_col) == 1 and end_row - start_row == 1:
  123.                 return True
  124.             return False
  125.         elif piece == 'p':  # Pawn (for black)
  126.             if start_row == 7 and start_col == end_col and start_row - end_row == 2:
  127.                 return True
  128.             # Similar logic for black pawn
  129.             if start_col == end_col and start_row - end_row == 1:
  130.                 return True
  131.             if self.board[end_row][end_col] is not None and abs(start_col - end_col) == 1 and start_row - end_row == 1:
  132.                 print(self.board[end_row][end_col][0])
  133.                 return True
  134.             return False
  135.         elif piece == 'N' or piece == 'n':  # Knight
  136.             # Knight moves in an L-shape
  137.             delta_row = abs(end_row - start_row)
  138.             delta_col = abs(end_col - start_col)
  139.             return (delta_row == 2 and delta_col == 1) or (delta_row == 1 and delta_col == 2)
  140.         elif piece == 'R' or piece == 'r':  # Rook
  141.             # Rook moves horizontally or vertically
  142.             return start_row == end_row or start_col == end_col
  143.         elif piece == 'B' or piece == 'b':  # Bishop
  144.             # Bishop moves diagonally
  145.             return abs(end_row - start_row) == abs(end_col - start_col)
  146.         elif piece == 'Q' or piece == 'q':  # Queen
  147.             # Queen combines rook and bishop moves
  148.             return (start_row == end_row or start_col == end_col) or (abs(end_row - start_row) == abs(end_col - start_col))
  149.         elif piece == 'K' or piece == 'k':  # King
  150.             # King moves one square in any direction
  151.             return abs(end_row - start_row) <= 1 and abs(end_col - start_col) <= 1
  152.         else:
  153.             return False  # Default: invalid move
  154.  
  155.  
  156. if __name__ == "__main__":
  157.     game = ChessGame()
  158.     while True:
  159.         game.print_board()
  160.         move = input("Enter your move (e.g., 'N c3 to e4'): ")
  161.         if move.lower() == 'exit':
  162.             break
  163.         move_parts = move.split()
  164.         piece = move_parts[0]
  165.         start_position = move_parts[1]
  166.         end_position = move_parts[3]
  167.         start_row, start_col = game.convert_position(start_position)
  168.         end_row, end_col = game.convert_position(end_position)
  169.         game.move_piece(piece, start_row, start_col, end_row, end_col)
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement