Advertisement
onexiv

Draft #6 teleporting good - knights follow rules after teleport

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