How would one go about designing such a thing like chess? I've been working on different ideas for it but I still can't seem to figure out how the overall structure of the game works in terms of programming.
The logic and implementation of an abstract strategy game like chess
I actually completed a chess game that accounts for:
- Valid moves and capturing by piece with indicators
- Check - Checkmate - Stalemate
- En Passant
- Pawn Double move from initial row
- Castling with rooks or the king
- Pawn to any piece upon reaching the final row
https://gamedev.net/blogs/entry/2268757-3d-chess-complete-4x-space-game-plan-2020-goals/
I used 2D arrays to keep track of the current board and possible move positions, and capture positions. On piece select I would populate a list by getting the object type (example Rook) and checking from the current row and col in all valid directions (left, up, right, down) for empty positions until I either hit an enemy piece which I would mark as a capturable piece then end the check, or my own piece or the end of the board and end the check. With the Rook I also used castling so if that rook hasn't moved yet and the King hasn't moved and there is open space between the two pieces I could allow castling to occur. I would also run a check that would make sure none of the available moves would place me into check, and if so remove that selection from the list including the ability to castle if it would also cause check to occur.
Once the ‘action’ list was populated I would show on-screen markers and wait for input. The player would then click the square they wish to perform an action on, or they could deselect the piece and select another which would repeat the above for the new selected piece.
After the action was performed I would check to see if my move placed the enemy king into check, if so, then I would check for checkmate (I have a function I call to verify any valid moves per player, if no valid moves exist then checkmate is true), if no checkmate was made you would then see if the king can move out of check, if not then check if that piece which is putting the king in check can be removed, if not then you see if any piece can block the path of the piece putting your king into check - all while ensuring my king doesn't remain in check after. Utilize early exits on any check, for example if I know I can remove check by moving my king then I have no reason to check for any other condition unless you want to pre-populate possible moves by indicator. On the player's turn they're already forced to make only legal moves which would only be removing themselves from check on their turn. This works naturally as I have it programmed that will not allow you to make a move that puts you into check, and since you're already in check you must clear that condition.
If no check has occurred prior to starting the next turn I would also check for stalemate. The quickest way was to cycle through the most likely pieces to make valid moves first because with this check you only need to confirm one move is possible without going into check to avoid a stalemate, otherwise you keep checking and if you've gone through all pieces and no valid moves exist then you would be in a stalemate.
One approach I always use when programming anything is to break it down into logical steps for how I would solve any solution. In Chess every piece has a defined movement pattern, and there is an order of operations you can follow. This makes it easy to structure what order things occur in.
I would make sure you can do it by pseudo code first. If you can, then transferring it over to your language of choice will be easy.
Programmer and 3D Artist
steven4 said:
how the overall structure of the game works in terms of programming.
Then this is not a Game Design question. I've moved the discussion to a more appropriate forum.
-- Tom Sloper -- sloperama.com
We have an old series on GameDev.net for this: Chess Programming Part 1
Part 2 covers Data Structures, which may be more of interest.
Admin for GameDev.net.