Advertisement
onexiv

Draft #2 - better movements, more rules in place

Mar 4th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.46 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', 'b'], ['♘', 'N', 'b'], ['♗', 'B', 'b'], ['♕', 'Q', 'b'], ['♔', 'K', 'b'], ['♗', 'B', 'b'], ['♘', 'N', 'b'], ['♖', 'R', 'b']]
  16. board[1] = [['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b']]
  17.  
  18. # Place black pieces
  19. board[7] = [['♜', 'r', 'w'], ['♞', 'n', 'w'], ['♝', 'b', 'w'], ['♛', 'q', 'w'], ['♚', 'k', 'w'], ['♝', 'b', 'w'], ['♞', 'n', 'w'], ['♜', 'r', 'w']]
  20. board[6] = [['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w']]
  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 capture_piece(self, row, col, moving_piece):
  45. # Remove the piece at the specified position from the board if it belongs to the opposing player
  46. captured_piece = self.board[row][col]
  47. if captured_piece != ' ' and ((moving_piece.isupper() and captured_piece.islower()) or (moving_piece.islower() and captured_piece.isupper())):
  48. self.board[row][col] = ' ' # Remove from the board
  49. if captured_piece.isupper(): # Captured piece is white
  50. self.captured_pieces['black'].append(captured_piece.lower()) # Add to black's captured pieces list
  51. else: # Captured piece is black
  52. self.captured_pieces['white'].append(captured_piece.upper()) # Add to white's captured pieces list
  53.  
  54. def move_piece(self, piece, start_row, start_col, end_row, end_col):
  55. # Check if the move is valid
  56. if self.is_valid_move(piece, start_row, start_col, end_row, end_col):
  57. # Perform the move
  58. # while True:
  59. piece_symbol = piece
  60. self.board[end_row][end_col] = self.board[start_row][start_col]
  61. self.board[start_row][start_col] = ['.','.','.']
  62. print("Move successful!")
  63. else:
  64. print("Invalid move!")
  65.  
  66. def convert_position(self, position):
  67. # Convert algebraic notation to array indices
  68. row = 8 - int(position[1])
  69. col = ord(position[0]) - ord('a')
  70. return row, col
  71.  
  72. def is_within_board(self, row, col):
  73. # Check if the given position is within the bounds of the board
  74. return 0 <= row < 8 and 0 <= col < 8
  75.  
  76. def is_valid_move(self, piece, start_row, start_col, end_row, end_col):
  77. # Check if the move is within the bounds of the board
  78. if not (0 <= start_row < 8 and 0 <= start_col < 8 and 0 <= end_row < 8 and 0 <= end_col < 8):
  79. return False
  80.  
  81. # Check if there is a piece at the starting position
  82. if self.board[start_row][start_col] is None:
  83. return False
  84. # Check for obstruction in the path
  85. # Check for obstruction in the path
  86. if piece in ['R', 'r', 'Q', 'q']: # Rook or Queen
  87. if start_row == end_row: # Horizontal move
  88. delta_col = 1 if end_col > start_col else -1
  89. for col in range(start_col + delta_col, end_col, delta_col):
  90. if self.board[start_row][col] is not None:
  91. return False # Obstruction found
  92. elif start_col == end_col: # Vertical move
  93. delta_row = 1 if end_row > start_row else -1
  94. for row in range(start_row + delta_row, end_row, delta_row):
  95. if self.board[row][start_col] is not None:
  96. return False # Obstruction found
  97.  
  98. if piece in ['B', 'b', 'Q', 'q']: # Bishop or Queen
  99. delta_row = end_row - start_row
  100. delta_col = end_col - start_col
  101. if abs(delta_row) == abs(delta_col): # Diagonal move
  102. row_step = 1 if delta_row > 0 else -1
  103. col_step = 1 if delta_col > 0 else -1
  104. row = start_row + row_step
  105. col = start_col + col_step
  106. while row != end_row and col != end_col:
  107. if self.board[row][col] is not None:
  108. return False # Obstruction found
  109. row += row_step
  110. col += col_step
  111.  
  112. # Check if there's a piece of the same color at the ending position
  113. 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]:
  114. return False # Obstruction found
  115. # Check for piece-specific move validation
  116. if piece == 'P': # Pawn
  117. # Pawn can move forward two squares from starting position
  118. if start_row == 1 and start_col == end_col and end_row - start_row == 2:
  119. return True
  120. if start_col == end_col and end_row - start_row == 1:
  121. return True
  122. # Pawn can capture diagonally
  123. 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:
  124. return True
  125. return False
  126. elif piece == 'p': # Pawn (for black)
  127. # Pawn can move forward two squares from starting position
  128. if start_row == 6 and start_col == end_col and start_row - end_row == 2:
  129. return True
  130. # Similar logic for black pawn
  131. if start_col == end_col and start_row - end_row == 1:
  132. return True
  133. if self.board[end_row][end_col] is not None and abs(start_col - end_col) == 1 and start_row - end_row == 1:
  134. print(self.board[end_row][end_col][0])
  135. return True
  136. return False
  137. elif piece == 'N' or piece == 'n': # Knight
  138. # Knight moves in an L-shape
  139. delta_row = abs(end_row - start_row)
  140. delta_col = abs(end_col - start_col)
  141. return (delta_row == 2 and delta_col == 1) or (delta_row == 1 and delta_col == 2)
  142. elif piece == 'R' or piece == 'r': # Rook
  143. # Rook moves horizontally or vertically
  144. return start_row == end_row or start_col == end_col
  145. elif piece == 'B' or piece == 'b': # Bishop
  146. # Bishop moves diagonally
  147. return abs(end_row - start_row) == abs(end_col - start_col)
  148. elif piece == 'Q' or piece == 'q': # Queen
  149. # Queen combines rook and bishop moves
  150. return (start_row == end_row or start_col == end_col) or (abs(end_row - start_row) == abs(end_col - start_col))
  151. elif piece == 'K' or piece == 'k': # King
  152. # King moves one square in any direction
  153. return abs(end_row - start_row) <= 1 and abs(end_col - start_col) <= 1
  154. else:
  155. return False # Default: invalid move
  156.  
  157.  
  158. if __name__ == "__main__":
  159. game = ChessGame()
  160. while True:
  161. game.print_board()
  162. move = input("Enter your move (e.g., 'N c3 to e4'): ")
  163. if move.lower() == 'exit':
  164. break
  165. move_parts = move.split()
  166. piece = move_parts[0]
  167. start_position = move_parts[1]
  168. end_position = move_parts[3]
  169. start_row, start_col = game.convert_position(start_position)
  170. end_row, end_col = game.convert_position(end_position)
  171. game.move_piece(piece, start_row, start_col, end_row, end_col)
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement