Advertisement
onexiv

Draft #6 getting there. Queens, bishops, rooks, knights teleport (all i wanted) teleport

Mar 5th, 2024 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.13 KB | None | 0 0
  1. class ChessGame:
  2. def __init__(self):
  3. self.board = self.initialize_board()
  4.  
  5. def initialize_board(self):
  6. # Initialize an empty 8x8 chessboard with pieces in their starting positions
  7. board = [[None for _ in range(8)] for _ in range(8)]
  8.  
  9. # Place white pieces
  10. board[0] = [['♖', 'R', 'b'], ['♘', 'N', 'b'], ['♗', 'B', 'b'], ['♕', 'Q', 'b'], ['♔', 'K', 'b'], ['♗', 'B', 'b'], ['♘', 'N', 'b'], ['♖', 'R', 'b']]
  11. board[1] = [['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b'], ['♙', 'P', 'b']]
  12. # Place black pieces
  13. board[7] = [['♜', 'r', 'w'], ['♞', 'n', 'w'], ['♝', 'b', 'w'], ['♛', 'q', 'w'], ['♚', 'k', 'w'], ['♝', 'b', 'w'], ['♞', 'n', 'w'], ['♜', 'r', 'w']]
  14. board[6] = [['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w'], ['♟', 'p', 'w']]
  15.  
  16. return board
  17.  
  18. def print_board(self):
  19. # Print the current state of the chessboard with positions revealed
  20. print(" +------------------------ A +")
  21. for row in range(8):
  22. row_display = f"{8 - row} | "
  23. for col in range(8):
  24. if self.board[row][col] is not None:
  25. piece_symbol = self.board[row][col][0]
  26. else:
  27. piece_symbol = '.'
  28. row_display += f" {piece_symbol} "
  29. row_display += " |"
  30. print(row_display)
  31. print(" +------------------------ a +")
  32.  
  33. def capture_piece(self, row, col, moving_piece):
  34. # Remove the piece at the specified position from the board if it belongs to the opposing player
  35. if self.board[row][col] is not None:
  36. self.board[row][col] = None
  37.  
  38. def move_piece(self, piece, start_row, start_col, end_row, end_col):
  39. # Move the piece from start position to end position
  40. if self.is_valid_move(piece, start_row, start_col, end_row, end_col):
  41. self.board[end_row][end_col] = self.board[start_row][start_col]
  42. self.board[start_row][start_col] = ['.', '.', '.']
  43.  
  44. def convert_position(self, position):
  45. # Convert algebraic notation to array indices
  46. row = 8 - int(position[1])
  47. col = ord(position[0]) - ord('a')
  48. return row, col
  49.  
  50. def is_valid_move(self, piece, start_row, start_col, end_row, end_col):
  51. # Check if the move is within the bounds of the board
  52. if not (0 <= start_row < 8 and 0 <= start_col < 8 and 0 <= end_row < 8 and 0 <= end_col < 8):
  53. return False
  54.  
  55. # Check if there is a piece at the starting position
  56. if self.board[start_row][start_col] is None:
  57. return False
  58.  
  59. print(self.board[start_row][start_col][2], self.board[end_row][end_col])
  60. if self.board[end_row][end_col] is None:
  61. self.board[end_row][end_col] = ['.','.','.']
  62.  
  63. if self.board[start_row][start_col][2] == self.board[end_row][end_col][2]:
  64. return False
  65.  
  66. # Check for obstruction in the path
  67. if piece in ['R', 'r', 'Q', 'q']: # Rook or Queen
  68. print(start_row, end_row, start_col, end_col)
  69. if start_row != end_row and start_col == end_col: # Horizontal move
  70. delta_col = 1 if end_col > start_col else -1
  71. for col in range(start_col + delta_col, end_col, delta_col):
  72. if self.board[start_row][col] is None:
  73. self.board[start_row][col] = ['.','.','.']
  74. if (self.board[start_row][col][2] == self.board[start_row][col][2]):
  75. return False # Obstruction found
  76. if (self.board[row][start_col][2] != self.board[row][start_col][2]):
  77. return True # Obstruction found
  78. return True
  79. elif start_col != end_col and start_row == end_row: # Vertical move
  80. delta_row = 1 if end_row > start_row else -1
  81. for row in range(start_row + delta_row, end_row, delta_row):
  82. if self.board[row][start_col] is None:
  83. self.board[row][start_col] = ['.','.','.']
  84. if (self.board[row][start_col][2] == self.board[row][start_col][2]):
  85. return False # Obstruction found
  86. if (self.board[row][start_col][2] != self.board[row][start_col][2]):
  87. return True # Obstruction found
  88. return True
  89.  
  90. if piece in ['B', 'b', 'Q', 'q']: # Bishop or Queen
  91.  
  92. print(start_row, end_row, start_col, end_col)
  93. delta_row = end_row - start_row
  94. delta_col = end_col - start_col
  95. if start_col != end_row and start_row == end_col: # Diagonal move
  96. print(row_step, delta_row, start_row)
  97. row_step = 1 if delta_row < start_row else -1
  98. col_step = 1 if delta_col < start_col else -1
  99. row = start_row + row_step
  100. col = start_col + col_step
  101. while row != end_row and col != end_col:
  102. if self.board[row][col] is None:
  103. self.board[row][col] = ['.','.','.']
  104. if (self.board[row][col][2] == self.board[start_row][start_col][2]):
  105. return False # Obstruction found
  106. if (self.board[row][start_col][2] != self.board[row][start_col][2]):
  107. return True # Obstruction found
  108. row += row_step
  109. col += col_step
  110. return True
  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.  
  116. # Handle the knight's movement allowing teleportation
  117. if piece == 'N' or piece == 'n': # Knight
  118. # Check if the move is a valid knight move
  119. delta_row = abs(end_row - start_row)
  120. delta_col = abs(end_col - start_col)
  121. # return (delta_row == 1 and delta_col == 2) or (delta_row == 2 and delta_col == 1)
  122. # Check if the move is a valid knight's move or teleportation
  123. print(delta_col, delta_row)
  124. if delta_row == 2 and delta_col == 1:
  125. return True
  126. elif delta_row == 1 and delta_col == 2:
  127. return True
  128. elif delta_row == 1 and delta_col >= 6:
  129. return True
  130. elif delta_row >= 6 and delta_col == 1:
  131. return True
  132. else:
  133. return False
  134.  
  135. # Allow non-pawns and the king to move around the outside of the board
  136. if piece not in ['P', 'p', 'K', 'k']:
  137. # Check if the move is along the edge of the board and the destination is on the opposite side
  138. if (start_row == 0 and end_row == 7 and start_col == end_col) or \
  139. (start_row == 7 and end_row == 0 and start_col == end_col) or \
  140. (start_col == 0 and end_col == 7 and start_row == end_row) or \
  141. (start_col == 7 and end_col == 0 and start_row == end_row):
  142. return True
  143.  
  144. # Check for piece-specific move validation
  145. if piece == 'P': # Pawn
  146. # Pawn can move forward two squares from starting position
  147. if start_row == 1 and start_col == end_col and end_row - start_row == 2:
  148. return True
  149. if start_col == end_col and end_row - start_row == 1:
  150. return True
  151. # Pawn can capture diagonally
  152. 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:
  153. return True
  154. return False
  155. elif piece == 'p': # Pawn (for black)
  156. # Pawn can move forward two squares from starting position
  157. if start_row == 6 and start_col == end_col and start_row - end_row == 2:
  158. return True
  159. # Similar logic for black pawn
  160. if start_col == end_col and start_row - end_row == 1:
  161. return True
  162. if self.board[end_row][end_col] is not None and abs(start_col - end_col) == 1 and start_row - end_row == 1:
  163. return True
  164. return False
  165. elif piece == 'R' or piece == 'r': # Rook
  166. # Rook moves horizontally or vertically
  167. return start_row == end_row or start_col == end_col
  168. elif piece == 'B' or piece == 'b': # Bishop
  169. # Bishop moves diagonally
  170. return abs(end_row - start_row) == abs(end_col - start_col)
  171. elif piece == 'Q' or piece == 'q': # Queen
  172. # Queen combines rook and bishop moves
  173. delta_row = abs(end_row - start_row)
  174. delta_col = abs(end_col - start_col)
  175. return (delta_row == delta_col) or (end_row == start_row or end_col == start_col) # (start_row == end_row or start_col == end_col) or (abs(end_row - start_row) == abs(end_col - start_col))
  176. elif piece == 'K' or piece == 'k': # King
  177. # King moves one square in any direction
  178. return abs(end_row - start_row) <= 1 and abs(end_col - start_col) <= 1
  179. else:
  180. return False # Default: invalid move
  181.  
  182. if __name__ == "__main__":
  183. game = ChessGame()
  184. while True:
  185. game.print_board()
  186. move = input("Enter your move (e.g., 'c2 to c4'): ")
  187. if move.lower() == 'exit':
  188. break
  189.  
  190. move_parts = move.split()
  191. piece = move_parts[0]
  192. start_position = move_parts[1]
  193. end_position = move_parts[3]
  194. start_row, start_col = game.convert_position(start_position)
  195. end_row, end_col = game.convert_position(end_position)
  196. game.move_piece(piece, start_row, start_col, end_row, end_col)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement