

Your pieces should be arranged in two rows: the back row should contain all of your special pieces, and the front row should contain all of your pawns. To set up a chess board, place all of the white pieces on one side and all of the black pieces on the other side. This can happen accidentally, but it’s more common when both players repeat the same move 3 times because nobody thinks they can win. A game also automatically ends in a draw if the same board state is achieved 3 times.A game also ends in a draw if the opponent has no legal move that they can make, such as if the king is trapped but also not in check.Games may also end in a draw if neither player can force a checkmate, such as if the kings are the only pieces left on the board.Let your opponent double-check if they can make a move before declaring yourself the winner! X Research source When this happens, say “checkmate” out loud to declare the end of the game. Checkmate occurs if you put your opponent’s king in check (meaning it is under direct threat) and they are incapable of moving the king or blocking the threat with another piece. I assume it does.The goal of chess is to capture your opponent’s king however, most games end before the capture when the opponent is unable to prevent their king from being captured. piece.removeLegalMove(square_mask):īut I'm fairly unfamiliar with C#, so I don't know if it handles bit masks.
Chess pieces moves code how to#
At that point you might switch to a straight-forward check for the moving piece itself, abandoning updating the other pieces.Īs for how to store it, a 64-bit mask would be enough to encode an 8x8 board. Somewhere along the line, when pieces get removed, this will get slower than just checking legal moves for the pieces themselves. It might be handy to keep some flag on the squares, so that you know if special reachability rules apply to the pawns or king/rook. Of course, checks along lines should stop when they encounter another piece that is blocking the path. So (in pseudo code): move piece from X0,Y0 to X1,Y1Ĭheck surrounding squares for king/pawn moves, add legal move where appropriateĬheck the knight positions, add legal move where appropriateĬheck horizontal/vertical lines for rook/queen, add legal move where appropriateĬheck diagonal lines for queen/bishops, add legal move where appropriate For the new square, you remove legal moves from the pieces that could move there before. For the square that you left, you add new moves to the pieces that can now move there.
Chess pieces moves code update#
When you move a piece, you update the legal moves of the other pieces. The fastest way, I think, would be to keep an array of legal moves for every piece. I think you would be best off to set up rules for each piece including special considerations, direction and distance, rather than an array to step through, since you literally have to determine where the piece is designed to go, then examine whether the position after it goes there will be legal. Does the move expose the player's king to check? For castling, will the king have to move through check (whether final position results in check or not)? If the king is already in check, does the move counter the check?

Then it gets further complicated by other situational considerations. In the case of en passant, you even need to know whether the opponents last move enabled en passant, as the move is only valid for one move after the opponent made a move to enable it. But is it faster to loop or write LINQ queries or store arrays and matrices? public class Moveįiguring out the legal moves available for a particular piece on a chess board in a particular chess position is very situational, especially when considering pawns.1 space except when it is on its starting position, then two, and then throw in captures and en passant captures.so building an array of moves that are "mechanically" possible is a bit hairy to start. I could use matrix math as well I suppose but I don't know.īasically I want to persist the rules for a given piece so I can assign a piece object a little template of all it's possible moves considering it is starting from it's current location, which should be just one table of data. I wonder what the fastest way to store and read a legal move on a piece object for calculation. etc etc for each row where 1 is a legal move, but I still have to populate it. Or, I can store in an array and then check if x and y are not 0 then it is a legal move and then I save the data like this For example if a move is attempted I could just loop through a list of legal moves and compare the x,y but I have to write logic to calculate those at least every time the piece is moved.
