Skip to content

Instantly share code, notes, and snippets.

@ilamanov
Created March 18, 2022 13:35
Show Gist options
  • Select an option

  • Save ilamanov/83b8649761990154f4ac5259cf69a11d to your computer and use it in GitHub Desktop.

Select an option

Save ilamanov/83b8649761990154f4ac5259cf69a11d to your computer and use it in GitHub Desktop.
helper functions
contract Chess {
/// @notice Takes in a board position, and applies the move `_move` to it.
/// @dev After applying the move, the board's perspective is updated (see {rotate}). Thus,
/// engines with symmterical search algorithms -- like negamax search -- probably work best.
/// @param _board The board to apply the move to.
/// @param _move The move to apply.
/// @return The reversed board after applying `_move` to `_board`.
function applyMove(uint256 _board, uint256 _move) internal pure returns (uint256) {
unchecked {
// Get piece at the from index
uint256 piece = (_board >> ((_move >> 6) << 2)) & 0xF;
// Replace 4 bits at the from index with 0000
_board &= type(uint256).max ^ (0xF << ((_move >> 6) << 2));
// Replace 4 bits at the to index with 0000
_board &= type(uint256).max ^ (0xF << ((_move & 0x3F) << 2));
// Place the piece at the to index
_board |= (piece << ((_move & 0x3F) << 2));
return _board.rotate();
}
}
/// @notice Switches the perspective of the board by reversing its 4-bit subdivisions (e.g.
/// 1100-0011 would become 0011-1100).
/// @dev Since the last bit exchanges positions with the 4th bit, the turn identifier is updated
/// as well.
/// @param _board The board to reverse the perspective on.
/// @return `_board` reversed.
function rotate(uint256 _board) internal pure returns (uint256) {
uint256 rotatedBoard;
unchecked {
for (uint256 i; i < 64; ++i) {
rotatedBoard = (rotatedBoard << 4) | (_board & 0xF);
_board >>= 4;
}
}
return rotatedBoard;
}
/// @notice Generates all possible pseudolegal moves for a given position and color.
/// @dev The last bit denotes which color to generate the moves for (see {Chess}). Also, the
/// function errors if more than 105 moves are found (see {Chess-MovesArray}). All moves are
/// expressed in code as shifts respective to the board's 8x8 representation (see {Chess}).
/// @param _board The board position to generate moves for.
/// @return Bitpacked uint256(s) containing moves.
function generateMoves(uint256 _board) internal pure returns (uint256[5] memory) {}
/// @notice Determines whether a move is a legal move or not (includes checking whether king is
/// checked or not after the move).
/// @param _board The board to analyze.
/// @param _move The move to check.
/// @return Whether the move is legal or not.
function isLegalMove(uint256 _board, uint256 _move) internal pure returns (bool) {}
/// @notice Determines whether a move results in a capture or not.
/// @param _board The board prior to the potential capture.
/// @param _indexAdjustedBoard The board bitshifted to the to index to consider.
/// @return Whether the move is a capture or not.
function isCapture(uint256 _board, uint256 _indexAdjustedBoard) internal pure returns (bool) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment