Advertisement
onexiv

Slingshot v1.4 :D

Mar 5th, 2024 (edited)
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.81 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 None:
  66.                         self.board[start_row][col] = ['.', '.', '.']
  67.                     elif self.board[start_row][col][2] == self.board[start_row][start_col][2]:
  68.                         return False  # Obstruction found
  69.                     elif end_col != col and self.board[end_row][col][2] != self.board[start_row][start_col][2]:
  70.                         return False  # Obstruction found
  71.                     elif end_col == col and self.board[end_row][col][2] != self.board[start_row][start_col][2]:
  72.                         return True  # Obstruction found
  73.                 return True
  74.             elif start_col == end_col:  # Vertical move
  75.                 delta_row = 1 if end_row > start_row else -1
  76.                 for row in range(start_row + delta_row, end_row, delta_row):
  77.                     if self.board[row][start_col] is None:
  78.                         self.board[row][start_col] = ['.', '.', '.']
  79.                     elif self.board[row][start_col][2] == self.board[start_row][start_col][2]:
  80.                         return False  # Obstruction found
  81.                     elif end_row != row and self.board[row][start_col][2] != self.board[start_row][start_col][2]:
  82.                         return False  # Obstruction found
  83.                     elif end_row == row and self.board[row][start_col][2] != self.board[start_row][start_col][2]:
  84.                         return True  # Obstruction found
  85.                 return True
  86.  
  87.         if piece in ['B', 'b', 'Q', 'q']:  # Bishop or Queen
  88.             delta_row = end_row - start_row
  89.             delta_col = end_col - start_col
  90.             print(delta_col, delta_row)
  91.             if abs(delta_row) == abs(delta_col):  # Diagonal move
  92.                 row_step = 1 if start_row < end_row else -1
  93.                 col_step = 1 if start_col < end_col else -1
  94.                 row = (start_row + row_step) % 8
  95.                 col = (start_col + col_step) % 8
  96.                 while row != end_row and col != end_col:
  97.                     if self.board[row][col] is None:
  98.                         self.board[row][col] = ['.', '.', '.']
  99.                     elif self.board[row][col][2] == self.board[start_row][start_col][2]:
  100.                         row_step = 1 if start_row > end_row else -1
  101.                         col_step = 1 if start_col > end_col else -1
  102.                     elif self.board[row][col][2] == self.board[start_row][start_col][2]:
  103.                         self.board[row][col] = self.board[start_row][start_col]
  104.                         self.board[start_row][start_col] = ['.', '.', '.']
  105.                         return True  # Obstruction found
  106.                     elif row == end_row and self.board[row][col][2] == self.board[start_row][start_col][2]:
  107.                         self.board[row][col] = self.board[start_row][start_col]
  108.                         self.board[start_row][start_col] = ['.', '.', '.']
  109.                         return False  # Obstruction found
  110.                     elif row != end_row and self.board[row][col][2] == self.board[start_row][start_col][2]:
  111.                         self.board[row][col] = self.board[start_row][start_col]
  112.                         self.board[start_row][start_col] = ['.', '.', '.']
  113.                         return False  # Obstruction found
  114.                     row = abs(row + row_step) % 8
  115.                     col = abs(col + col_step) % 8
  116.         # Check if there's a piece of the same color at the ending position
  117.         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]:
  118.             return False  # Obstruction found
  119.        
  120.         if piece == 'N' or piece == 'n':  # Knight
  121.             # Check if the move is a valid knight move
  122.             delta_row = abs(end_row - start_row)
  123.             delta_col = abs(end_col - start_col)
  124.             # return (delta_row == 1 and delta_col == 2) or (delta_row == 2 and delta_col == 1)
  125.             # Check if the move is a valid knight's move or teleportation
  126.             print(delta_col, delta_row)
  127.             if delta_row == 2 and delta_col == 1:
  128.                 return True
  129.             elif delta_row == 1 and delta_col == 2:
  130.                 return True
  131.             elif delta_row == 1 and delta_col >= 6:
  132.                 return True
  133.             elif delta_row >= 6 and delta_col == 1:
  134.                 return True
  135.             else:
  136.                 return False
  137.  
  138.         # Allow non-pawns and the king to move around the outside of the board
  139.         # if piece not in ['P', 'p', 'K', 'k']:
  140.         #     # Check if the move is along the edge of the board and the destination is on the opposite side
  141.         #     if (start_row == 0 and end_row == 7 and start_col == end_col) or \
  142.         #     (start_row == 7 and end_row == 0 and start_col == end_col) or \
  143.         #     (start_col == 0 and end_col == 7 and start_row == end_row) or \
  144.         #     (start_col == 7 and end_col == 0 and start_row == end_row):
  145.         #         return True
  146.  
  147.         # Check for piece-specific move validation
  148.         if piece == 'P':  # Pawn
  149.             # Pawn can move forward two squares from starting position
  150.             if start_row == 1 and start_col == end_col and end_row - start_row == 2:
  151.                 return True
  152.             if start_col == end_col and end_row - start_row == 1:
  153.                 return True
  154.             # Pawn can capture diagonally
  155.             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:
  156.                 return True
  157.             return False
  158.         elif piece == 'p':  # Pawn (for black)
  159.             # Pawn can move forward two squares from starting position
  160.             if start_row == 6 and start_col == end_col and start_row - end_row == 2:
  161.                 return True
  162.             # Similar logic for black pawn
  163.             if start_col == end_col and start_row - end_row == 1:
  164.                 return True
  165.             if self.board[end_row][end_col] is not None and abs(start_col - end_col) == 1 and start_row - end_row == 1:
  166.                 return True
  167.             return False
  168.         elif piece == 'R' or piece == 'r':  # Rook
  169.             # Rook moves horizontally or vertically
  170.             return start_row == end_row or start_col == end_col
  171.         elif piece == 'B' or piece == 'b':  # Bishop
  172.             # Bishop moves diagonally
  173.             return abs(end_row - start_row) == abs(end_col - start_col)
  174.         elif piece == 'Q' or piece == 'q':  # Queen
  175.             # Queen combines rook and bishop moves
  176.             return (start_row == end_row or start_col == end_col) or (abs(end_row - start_row) == abs(end_col - start_col))
  177.         elif piece == 'K' or piece == 'k':  # King
  178.             # King moves one square in any direction
  179.             return abs(end_row - start_row) <= 1 and abs(end_col - start_col) <= 1
  180.         else:
  181.             return False  # Default: invalid move
  182.  
  183. if __name__ == "__main__":
  184.     game = ChessGame()
  185.     while True:
  186.         game.print_board()
  187.         move = input("Enter your move (e.g., 'n b1 to a3') You may wrap around the board too!: ")
  188.         if move.lower() == 'exit':
  189.             break
  190.  
  191.         move_parts = move.split()
  192.         piece = move_parts[0]
  193.         start_position = move_parts[1]
  194.         end_position = move_parts[3]
  195.         start_row, start_col = game.convert_position(start_position)
  196.         end_row, end_col = game.convert_position(end_position)
  197.         if game.is_valid_move(piece, start_row, start_col, end_row, end_col):
  198.             game.move_piece(piece, start_row, start_col, end_row, end_col)
  199.         else:
  200.             print("Invalid Move!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement