![]() ![]() Separating the piece's color is a simple matter of using binary AND with the value and the BLACK bit-field. This is a knight, which also has the value 3. For example, if the value of board is 11 (binary 00001011), then the binary AND of 11 and the mask PIECE (7, or binary 00000111) is binary 00000011, or 3. The binary AND operator ( &) combines two binary values so that for any bit position, if that bit in both numbers is 1, then the result is also 1. What piece is there, and is it black or white? To identify the chess piece, combine the element's value with the PIECE mask using the binary AND: int board We can separate the piece using a binary mask.įor example, the program might need to know the contents of a specific square on the board during the chess game, such as the array element at board. This stores the value 1 because the binary OR of WHITE (0) and PAWN (1) is just 1: 00000000 = 0ĭuring the chess game, the program will need to know what piece is in a square and its color. Similarly, to store a white pawn in position 6,0 of the array, you could use this: board = WHITE | PAWN For every bit position, if that bit from either number is 1, the result for that bit position is also 1. The | is a binary OR, which means the computer will combine the bits from two numbers. For example, to store a black rook in position 0,0 of an array, you would use this code: int board When you assign a value to a square, such as when initializing the chessboard, you can assign a single int value to track both the piece and its color. To write a chess program using bit-fields and masks, we might start with these definitions: /* game pieces */ That's called a bit-field, which we can pull out later using a binary mask. If this bit is 1, it's black if it's 0, it's white. ![]() We can use the number 8 (binary 00001000) to indicate if a piece is black. All of the other bits in the binary representation of 6 are zeroes.Īnd with a bit of cleverness, we can use one of those extra always-zero bits to track if a piece is black or white. To list all pieces on a chessboard, we only need the three bits that represent (from right to left) the values 1, 2, and 4. Remember that binary numbers are made up of bits, which are either zero or one. I'll help us along to the next step by representing the number in its binary form, the way the computer would track it. Let's start by listing the possible chess pieces and assigning a number to each. When using bit-fields to represent data, it helps to think like a computer. Rather than storing a structure of two int values for every square on a chessboard, we can store a single int value and use binary bit-fields and masks to identify the pieces and color in each square. But there's a more straightforward way to track the same information while using less data and memory. You can quickly identify if the piece is a pawn, rook, knight, bishop, queen, or king-and if the piece is black or white. With this programming structure, your program will know what piece is in every square and its color. Free online course: RHEL Technical Overview.Using different color shapes to highlight a given area. Possible extensions: Adding further features such as piece capture with considering if the captured piece is protected settingįree Pascal features: Loading (exchanging) images from files. Some features (such the forbidden moves) are actually based on situation-depending data (string constants) cf. Note for programmers: In the actual version 1.0, not all chess rules, as used by he application, are implemented within the code. This includes moves to empty fields, captures, setting the opponent check and forbidden moves, in particular of the king piece,Ī quick start guide, help concerning the chess rules and a detailed description of how to use the application are included. In setting up the chess board, trying out all possible moves at game opening, trying out the castling move and trying to move the different pieces in 6 But, the application isn't limited to a single piece on the board: User may also train themselves The program telling them, if the move is valid or not. Users can take a bishop piece, place it on the board and let them show, where the bishop actually may move or, alternately, try to move the piece themselves, Instead of reading in a chess book "the bishop moves diagonally only", the application Who may use it to learn to correctly move the different kind chess pieces. ![]() Download ChessMoves desktop application (Windows 64bit)ĭownload ChessMoves source code (Lazarus/Free Pascal)ĭescription: ChessMoves is an interactive graphical application, intended for absolute chess beginners, ![]()
0 Comments
Leave a Reply. |