Master Chess
class ChessGame:
def __init__(self):
self.board = self.create_board()
self.current_turn = 'white'
self.game_over = False
def create_board(self):
board = []
for _ in range(8):
row = [' '] * 8
board.append(row)
# Placing white pieces
board[0] = ['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R']
board[1] = ['P'] * 8
# Placing black pieces
board[6] = ['p'] * 8
board[7] = ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r']
return board
def print_board(self):
for row in self.board:
print(' '.join(row))
def make_move(self, start, end):
start_row, start_col = start
end_row, end_col = end
piece = self.board[start_row][start_col]
if piece == ' ':
print("No piece at the given position.")
return False
if (self.current_turn == 'white' and piece.islower()) or (self.current_turn == 'black' and piece.isupper()):
print("It's not your turn!")
return False
if self.current_turn == 'white' and piece.isupper() and self.check_valid_move(start, end):
self.board[end_row][end_col] = piece
self.board[start_row][start_col] = ' '
self.current_turn = 'black'
return True
elif self.current_turn == 'black' and piece.islower() and self.check_valid_move(start, end):
self.board[end_row][end_col] = piece
self.board[start_row][start_col] = ' '
self.current_turn = 'white'
return True
else:
print("Invalid move!")
return False
def check_valid_move(self, start, end):
# Logic for checking valid moves goes here
return True # Placeholder, implement actual logic
def is_checkmate(self):
# Logic for checking checkmate goes here
return False # Placeholder, implement actual logic
# Example Usage:
if __name__ == "__main__":
game = ChessGame()
while not game.game_over:
game.print_board()
start = input("Enter start position (e.g., 'a2'): ")
end = input("Enter end position (e.g., 'a4'): ")
start_row, start_col = ord(start[1]) - 49, ord(start[0]) - 97
end_row, end_col = ord(end[1]) - 49, ord(end[0]) - 97
if game.make_move((start_row, start_col), (end_row, end_col)):
if game.is_checkmate():
game.game_over = True
print("Checkmate!")
elif game.current_turn == 'white' and game.check_valid_move:
print("Black's turn")
elif game.current_turn == 'black' and game.check_valid_move:
print("White's turn")
0 Comments