Advertisement
onexiv

SlingShot v1.6

Mar 10th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.75 KB | None | 0 0
  1. # Next time this will be on my server. fivy.org. I need $2 to get it running again. paypal inland14@live.com if anyone can help
  2.  
  3. class ChessGame:
  4. def __init__(self):
  5. self.board = self.initialize_board()
  6. self.current_player = 'White'
  7. self.kings = [ ['e1', 'White'], ['e8', 'Black'] ]
  8.  
  9. def initialize_board(self):
  10. # Initialize an empty 8x8 chessboard with pieces in their starting positions
  11. board = [[None for _ in range(8)] for _ in range(8)]
  12.  
  13. # Place white pieces
  14. board[0] = [['♖', 'R', 'Black'], ['♘', 'N', 'Black'], ['♗', 'B', 'Black'], ['♕', 'Q', 'Black'], ['♔', 'K', 'Black'], ['♗', 'B', 'Black'], ['♘', 'N', 'Black'], ['♖', 'R', 'Black']]
  15. board[1] = [['♙', 'P', 'Black'], ['♙', 'P', 'Black'], ['♙', 'P', 'Black'], ['♙', 'P', 'Black'], ['♙', 'P', 'Black'], ['♙', 'P', 'Black'], ['♙', 'P', 'Black'], ['♙', 'P', 'Black']]
  16.  
  17. # Place black pieces
  18. board[7] = [['♜', 'r', 'White'], ['♞', 'n', 'White'], ['♝', 'b', 'White'], ['♛', 'q', 'White'], ['♚', 'k', 'White'], ['♝', 'b', 'White'], ['♞', 'n', 'White'], ['♜', 'r', 'White']]
  19. board[6] = [['♟', 'p', 'White'], ['♟', 'p', 'White'], ['♟', 'p', 'White'], ['♟', 'p', 'White'], ['♟', 'p', 'White'], ['♟', 'p', 'White'], ['♟', 'p', 'White'], ['♟', 'p', 'White']]
  20.  
  21. return board
  22.  
  23. def print_board(self):
  24. # Print the current state of the chessboard with positions revealed
  25. print(" +------------------------ A +")
  26. for row in range(8):
  27. row_display = f"{8 - row} | "
  28. for col in range(8):
  29. if self.board[row][col] is not None:
  30. piece_symbol = self.board[row][col][0]
  31. else:
  32. piece_symbol = '.'
  33. row_display += f" {piece_symbol} "
  34. row_display += " |"
  35. print(row_display)
  36. print(" +------------------------ a +")
  37.  
  38. def capture_piece(self, row, col, moving_piece):
  39. # Remove the piece at the specified position from the board if it belongs to the opposing player
  40. if self.board[row][col] is not None:
  41. self.board[row][col] = None
  42.  
  43. def move_piece(self, piece, start_row, start_col, end_row, end_col):
  44. # Move the piece from start position to end position
  45. if self.board[start_row][start_col][1] == piece:
  46. self.board[end_row][end_col] = self.board[start_row][start_col]
  47. self.board[start_row][start_col] = ['.', '.', '.']
  48.  
  49. def convert_position(self, position):
  50. # Convert algebraic notation to array indices
  51. row = 8 - int(position[1])
  52. col = ord(position[0]) - ord('a')
  53. return row, col
  54.  
  55. def is_valid_move(self, piece, start_row, start_col, end_row, end_col):
  56. # Check if the move is within the bounds of the board
  57. if not (0 <= start_row < 8 and 0 <= start_col < 8 and 0 <= end_row < 8 and 0 <= end_col < 8):
  58. return False
  59.  
  60. # Check if there is a piece at the starting position
  61. if self.board[start_row][start_col] is None:
  62. return False
  63.  
  64. # opponent_color = 'Black' if self.board[start_row][start_col][2] == 'White' else 'White'
  65. # if self.is_check(opponent_color):
  66. # return False # Move leads to opponent's king being in check
  67. # Check for obstruction in the path
  68. if piece in ['R', 'r', 'Q', 'q']: # Rook or Queen
  69. if start_row == end_row: # 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. elif self.board[start_row][col][2] == self.board[start_row][start_col][2]:
  75. return False # Obstruction found
  76. elif end_col != col and self.board[end_row][col][2] != self.board[start_row][start_col][2]:
  77. return False # Obstruction found
  78. elif end_col == col and self.board[end_row][col][2] != self.board[start_row][start_col][2]:
  79. return True # Obstruction found
  80. return True
  81. elif start_col == end_col: # Vertical move
  82. delta_row = 1 if end_row > start_row else -1
  83. for row in range(start_row + delta_row, end_row, delta_row):
  84. if self.board[row][start_col] is None:
  85. self.board[row][start_col] = ['.', '.', '.']
  86. elif self.board[row][start_col][2] == self.board[start_row][start_col][2]:
  87. return False # Obstruction found
  88. elif end_row != row and self.board[row][start_col][2] != self.board[start_row][start_col][2]:
  89. return False # Obstruction found
  90. elif end_row == row and self.board[row][start_col][2] != self.board[start_row][start_col][2]:
  91. return True # Obstruction found
  92. return True
  93.  
  94. if piece in ['B', 'b', 'Q', 'q']: # Bishop or Queen
  95. delta_row = end_row - start_row
  96. delta_col = end_col - start_col
  97. if abs(delta_row) == abs(delta_col): # Diagonal move
  98. row_step = 1 if start_row < end_row else -1
  99. col_step = 1 if start_col < end_col else -1
  100. row = (start_row + row_step) % 8
  101. col = (start_col + col_step) % 8
  102. check_row = row
  103. check_col = col
  104. while row != 7 and col != 7:
  105. if self.board[row][col] is None:
  106. self.board[row][col] = ['.', '.', '.']
  107. elif self.board[row][col][1].lower() == 'k':
  108. print("Check!")
  109. return True
  110. elif self.board[row][col][2] == self.board[start_row][start_col][2]:
  111. row_step = 1 if start_row > end_row else -1
  112. col_step = 1 if start_col > end_col else -1
  113. row = abs(row + row_step) % 8
  114. col = abs(col + col_step) % 8
  115.  
  116. row_step = 1 if start_row < end_row else -1
  117. col_step = 1 if start_col < end_col else -1
  118. row = (start_row + row_step) % 8
  119. col = (start_col + col_step) % 8
  120. check_row = row
  121. check_col = col
  122. while row != end_row and col != end_col:
  123. if self.board[row][col] is None:
  124. self.board[row][col] = ['.', '.', '.']
  125. elif self.board[row][col][2] == self.board[start_row][start_col][2]:
  126. row_step = 1 if start_row > end_row else -1
  127. col_step = 1 if start_col > end_col else -1
  128. elif self.board[row][col][2] == self.board[start_row][start_col][2]:
  129. return False # Obstruction found
  130. elif row == end_row and self.board[row][col][2] == self.board[start_row][start_col][2]:
  131. return False # Obstruction found
  132. elif row != end_row and self.board[row][col][2] == self.board[start_row][start_col][2]:
  133. return False # Obstruction found
  134. row = abs(row + row_step) % 8
  135. col = abs(col + col_step) % 8
  136. # Check if there's a piece of the same color at the ending position
  137. 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]:
  138. return False # Obstruction found
  139.  
  140. if piece == 'N' or piece == 'n': # Knight
  141. # Check if the move is a valid knight move
  142. delta_row = abs(end_row - start_row)
  143. delta_col = abs(end_col - start_col)
  144. # return (delta_row == 1 and delta_col == 2) or (delta_row == 2 and delta_col == 1)
  145. # Check if the move is a valid knight's move or teleportation
  146. if delta_row == 2 and delta_col == 1:
  147. return True
  148. elif delta_row == 1 and delta_col == 2:
  149. return True
  150. elif delta_row == 1 and delta_col >= 6:
  151. return True
  152. elif delta_row >= 6 and delta_col == 1:
  153. return True
  154. else:
  155. return False
  156.  
  157. # Check for piece-specific move validation
  158. if piece == 'P': # Pawn
  159. # Pawn can move forward two squares from starting position
  160. if start_row == 1 and start_col == end_col and end_row - start_row == 2:
  161. return True
  162. if start_col == end_col and end_row - start_row == 1:
  163. return True
  164. # Pawn can capture diagonally
  165. 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:
  166. return True
  167. return False
  168. elif piece == 'p': # Pawn (for black)
  169. # Pawn can move forward two squares from starting position
  170. if start_row == 6 and start_col == end_col and start_row - end_row == 2:
  171. return True
  172. # Similar logic for black pawn
  173. if start_col == end_col and start_row - end_row == 1:
  174. return True
  175. if self.board[end_row][end_col] is not None and abs(start_col - end_col) == 1 and start_row - end_row == 1:
  176. return True
  177. return False
  178. elif piece == 'R' or piece == 'r': # Rook
  179. # Rook moves horizontally or vertically
  180. return start_row == end_row or start_col == end_col
  181. elif piece == 'B' or piece == 'b': # Bishop
  182. # Bishop moves diagonally
  183. return abs(end_row - start_row) == abs(end_col - start_col)
  184. elif piece == 'Q' or piece == 'q': # Queen
  185. # Queen combines rook and bishop moves
  186. return (start_row == end_row or start_col == end_col) or (abs(end_row - start_row) == abs(end_col - start_col))
  187. elif piece == 'K' or piece == 'k': # King
  188. # King moves one square in any direction
  189. str_temp = chr(end_col + ord('a')) + end_row
  190. if self.board[end_row][end_col][2] == 'White':
  191. self.kings[0][0] = str_temp
  192. elif self.board[end_row][end_col][2] == 'Black':
  193. self.kings[1][0] = str_temp
  194. return abs(end_row - start_row) <= 1 and abs(end_col - start_col) <= 1
  195. else:
  196. return False # Default: invalid move
  197. # Check if there's a piece of the same color at the ending position
  198. 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]:
  199. return False
  200.  
  201. return True # Move is valid
  202.  
  203. def is_check(self, piece, start_row, start_col, kings_row, kings_col):
  204. # Check if the move is within the bounds of the board
  205. if not (0 <= start_row < 8 and 0 <= start_col < 8 and 0 <= end_row < 8 and 0 <= end_col < 8):
  206. return False
  207.  
  208. # Check if there is a piece at the starting position
  209. if self.board[start_row][start_col] is None:
  210. return False
  211.  
  212. # opponent_color = 'Black' if self.board[start_row][start_col][2] == 'White' else 'White'
  213. # if self.is_check(opponent_color):
  214. # return False # Move leads to opponent's king being in check
  215. # Check for obstruction in the path
  216. if piece in ['R', 'r', 'Q', 'q']: # Rook or Queen
  217. if start_row == kings_row: # Horizontal move
  218. for i in range(8):
  219. if self.board[start_row][i] is None:
  220. self.board[start_row][i] = ['.', '.', '.']
  221. if self.board[start_row][i][2] != self.board[start_row][i][2] and self.board[start_row][i][1].lower() != 'k':
  222. return False
  223. if self.board[start_row][i][2] != self.board[start_row][i][2] and self.board[start_row][i][1].lower() == 'k':
  224. return True
  225. elif start_col == kings_col: # Vertical move
  226. for i in range(8):
  227. if self.board[i][start_col] is None:
  228. self.board[i][start_col] = ['.', '.', '.']
  229. if self.board[i][start_col][2] != self.board[start_row][start_col][2] and self.board[i][start_col][1].lower() != 'k':
  230. return False
  231. if self.board[i][start_col][2] != self.board[start_row][start_col][2] and self.board[i][start_col][1].lower() == 'k':
  232. return True
  233.  
  234. if piece in ['B', 'b', 'Q', 'q']: # Bishop or Queen
  235. for i in range(8):
  236. for j in range(8):
  237. if j == i:
  238. if self.board[i][j] is None:
  239. self.board[i][j] = ['.', '.', '.']
  240. if self.board[i][j][2] == self.board[start_row][start_col][2]:
  241. break
  242. if self.board[i][j][2] != self.board[start_row][start_col][2] and self.board[i][j][1].lower() != 'k':
  243. break
  244. if self.board[i][j][2] != self.board[start_row][start_col][2] and self.board[i][j][1].lower() == 'k':
  245. return True
  246.  
  247. if piece == 'N' or piece == 'n': # Knight
  248. # Check if the move is a valid knight move
  249. for i in range(8):
  250. for j in range(8):
  251. delta_row = abs(i - start_row)
  252. delta_col = abs(j - start_col)
  253. if (delta_row == 1 and delta_col == 2) or (delta_row == 2 and delta_col == 1):
  254. if self.board[i][j] is None:
  255. self.board[i][j] = ['.','.','.']
  256. if self.board[i][j][2] != self.board[start_row][start_col][2] and self.board[i][j][1].lower() == 'k':
  257. return True
  258.  
  259. def is_checkmate(self, color):
  260. """
  261. Check if the specified color is in checkmate.
  262. """
  263. # Iterate through all pieces of the specified color
  264. for row in range(8):
  265. for col in range(8):
  266. if self.board[row][col] is not None and self.board[row][col][2] == color:
  267. piece = self.board[row][col][1]
  268. # Generate all possible moves for the piece
  269. for move_row in range(8):
  270. for move_col in range(8):
  271. if self.is_valid_move(piece, row, col, move_row, move_col):
  272. # Apply the move to a copy of the board
  273. temp_board = [row[:] for row in self.board]
  274. temp_board[move_row][move_col] = temp_board[row][col]
  275. temp_board[row][col] = ['.','.','.']
  276.  
  277. # Check if the move leads to a position where the king is still in check
  278. if not self.is_check(color):
  279. return False # Not checkmate
  280. return True # Checkmate
  281.  
  282. def is_stalemate(self, color):
  283. """
  284. Check if the specified color is in stalemate.
  285. """
  286. # Similar to is_checkmate but without the check condition
  287. for row in range(8):
  288. for col in range(8):
  289. if self.board[row][col] is not None and self.board[row][col][2] == color:
  290. piece = self.board[row][col][1]
  291.  
  292. for move_row in range(8):
  293. for move_col in range(8):
  294. if self.is_valid_move(piece, row, col, move_row, move_col):
  295. temp_board = [row[:] for row in self.board]
  296. temp_board[move_row][move_col] = temp_board[row][col]
  297. temp_board[row][col] = ['.','.','.']
  298.  
  299. if not self.is_check(color):
  300. return False # Not stalemate
  301. return True # Stalemate
  302.  
  303. def check_endgame_modes(self, color):
  304. """
  305. Check each move in the endgame against each of the opposition's pieces.
  306. """
  307. endgame_modes = {'Check': 0, 'Checkmate': 0, 'Stalemate': 0}
  308.  
  309. # Iterate through all pieces of the specified color
  310. for row in range(8):
  311. for col in range(8):
  312. if self.board[row][col] is not None and self.board[row][col][2] == color:
  313. piece = self.board[row][col][1]
  314.  
  315. # Generate all possible moves for the piece
  316. for move_row in range(8):
  317. for move_col in range(8):
  318. if self.is_valid_move(piece, row, col, move_row, move_col):
  319. # Apply the move to a copy of the board
  320. temp_board = [row[:] for row in self.board]
  321. temp_board[move_row][move_col] = temp_board[row][col]
  322. temp_board[row][col] = ['.','.','.']
  323.  
  324. # Check if the move results in check, checkmate, or stalemate for the opposing player
  325. if self.is_check(color):
  326. if self.is_checkmate(color):
  327. endgame_modes['Checkmate'] += 1
  328. else:
  329. endgame_modes['Check'] += 1
  330. elif self.is_stalemate(color):
  331. endgame_modes['Stalemate'] += 1
  332. return endgame_modes
  333.  
  334. if __name__ == "__main__":
  335. game = ChessGame()
  336. current_player = 'White' # Start with white player
  337. while True:
  338. game.print_board()
  339. print(f"It's {'White' if current_player == 'White' else 'Black'}'s turn.")
  340.  
  341. move = input("Enter your move (e.g., 'N c2 to d4'): ")
  342. if move.lower() == 'exit':
  343. break
  344.  
  345. move_parts = move.split()
  346. piece = move_parts[0]
  347. start_position = move_parts[1]
  348. end_position = move_parts[3]
  349. start_row, start_col = game.convert_position(start_position)
  350. end_row, end_col = game.convert_position(end_position)
  351.  
  352. # Check if it's the player's piece
  353. if game.board[start_row][start_col] is None or game.board[start_row][start_col][2] != current_player:
  354. print("Invalid Move! It's not your piece.")
  355. continue
  356.  
  357. # if game.check_endgame_modes(current_player):
  358. if game.is_valid_move(piece, start_row, start_col, end_row, end_col):
  359. # game.check_endgame_modes(current_player)
  360. if game.is_check(piece, start_row, start_col, end_row, end_col):
  361. print("Check!")
  362. game.move_piece(piece, start_row, start_col, end_row, end_col)
  363.  
  364. offset = 0
  365. if current_player != 'White':
  366. offset = 1
  367. king_row, king_col = game.convert_position(game.kings[offset][0])
  368. if game.is_check(piece, end_row, end_col, king_row, king_col):
  369. # if game.is_check(piece, end_row, end_col, king_row, king_col):
  370. print("Check!")
  371.  
  372. # Switch player
  373. current_player = 'Black' if current_player == 'White' else 'White'
  374. else:
  375. print("Invalid Move!")
  376.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement