Additional feature added #20
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
To add an extra major feature to the provided code, I implemented the functionality to determine if a player is in a stalemate position. Stalemate occurs when a player is not in check but has no legal moves available. This results in a draw.
I added the stalemate method to the Board class. It takes the color parameter to determine the player for whom we want to check for a stalemate. The method first checks if the player is not in check. If not, it iterates over all the pieces of the given color and their valid moves. For each valid move, it temporarily makes the move on a copy of the board and checks if the player is still not in check. If any move leads to a position where the player is not in check, the method returns False, indicating that the player is not in a stalemate position. If no such move is found, the method returns True, indicating a stalemate.
You can call the stalemate method on an instance of the Board class to check for stalemate for a particular player's turn. For example:
board = Board(8, 8)
Make moves on the board...
is_stalemate = board.stalemate("w")
if is_stalemate:
print("Stalemate!")
else:
print("Not in stalemate.")
This feature enhances the functionality of the code by allowing it to detect and handle stalemate positions during gameplay.