# First draft

Mar 3rd, 2024
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
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
7.         board = [[' ' for _ in range(8)] for _ in range(8)]
8.         # Place white pieces
9.         board[0] = ['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R']
10.         board[1] = ['P'] * 8
11.         # Place black pieces
12.         board[7] = ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r']
13.         board[6] = ['p'] * 8
14.         return board
15.
16.     def print_board(self):
17.         # Print the current state of the chessboard with positions revealed
18.         for row_num, row in enumerate(self.board):
19.             row_display = []
20.             for col_num, piece in enumerate(row):
21.                 if piece == ' ':
22.                     row_display.append(f"{chr(col_num + ord('a'))}{8 - row_num} -")
23.                 else:
24.                     row_display.append(f"{chr(col_num + ord('a'))}{8 - row_num} {piece}")
25.             print(" ".join(row_display))
26.
27.     def move_piece(self, piece, start_row, start_col, end_row, end_col):
28.         # Check if the move is valid
29.         if self.is_valid_move(piece, start_row, start_col, end_row, end_col):
30.             # Perform the move
31.             self.board[end_row][end_col] = piece
32.             self.board[start_row][start_col] = ' '
33.             print("Move successful!")
34.         else:
35.             print("Invalid move!")
36.
37.     def convert_position(self, piece, position):
38.         # Convert algebraic notation to array indices
39.         row = 8 - int(position[1])
40.         col = ord(position[0]) - ord('a')
41.         if piece.islower():  # Check if it's a black piece
42.             row = 7 - row  # Invert row for Black side
43.         return row, col
44.
45.     def is_within_board(self, row, col):
46.         # Check if the given position is within the bounds of the board
47.         return 0 <= row < 8 and 0 <= col < 8
48.
49.     def is_valid_move(self, piece, start_row, start_col, end_row, end_col):
50.         # Check if the move is valid for the given piece
51.         # Implementation of move validation omitted for brevity
52.         return True
53.
54.
55. if __name__ == "__main__":
56.     game = ChessGame()
57.     while True:
58.         game.print_board()
59.         move = input("Enter your move (e.g., 'N c3 to e4'): ")
60.         if move.lower() == 'exit':
61.             break
62.         move_parts = move.split()
63.         piece = move_parts[0]
64.         start_position = move_parts[1]
65.         end_position = move_parts[3]
66.         start_row, start_col = game.convert_position(piece, start_position)
67.         end_row, end_col = game.convert_position(piece, end_position)
68.         game.move_piece(piece, start_row, start_col, end_row, end_col)
69.