Skip to content

Instantly share code, notes, and snippets.

View ilamanov's full-sized avatar

Nazar Ilamanov ilamanov

View GitHub Profile
@ilamanov
ilamanov / Chess.sol
Created March 18, 2022 13:38
library used for types
contract Chess {
using Chess for uint256;
using Chess for Chess.MovesArray;
}
@ilamanov
ilamanov / Chess.sol
Created March 18, 2022 13:35
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
@ilamanov
ilamanov / Chess.sol
Created March 18, 2022 12:32
moves array
contract Chess {
/// ``moves'' are bitpacked into uint256s for efficiency. Since every move is defined by at most
/// 12 bits, a uint256 can contain up to 21 moves via bitpacking (21 * 12 = 252 < 256).
/// Therefore, `items` can contain up to 21 * 5 = 105 moves. 105 is a safe upper bound for the
/// number of possible moves a given side may have during a real game, but be wary because there
/// is no formal proof of the upper bound being less than or equal to 105.
struct MovesArray {
uint256 index;
uint256[5] items;
}
@ilamanov
ilamanov / Chess.sol
Created March 15, 2022 11:01
Move representation
contract Chess {
/// ======================================Move Representation=======================================
/// Each move is allocated 12 bits. The first 6 bits are the index the piece is moving from, and the
/// last 6 bits are the index the piece is moving to. Since the index representing a square is at
/// most 54, 6 bits sufficiently represents any index (0b111111 = 63 > 54). e.g. 1243 denotes a move
/// from index 19 to 27 (1243 = (19 << 6) | 27).
}
@ilamanov
ilamanov / Chess.sol
Created March 15, 2022 10:59
Sentinels and whose turn
contract Chess {
/// The top/bottom rows and left/right columns are treated as sentinel rows/columns for efficient
/// boundary validation (see {Chess-generateMoves} and {Chess-isValid}). i.e., (63, ..., 56),
/// (07, ..., 00), (63, ..., 07), and (56, ..., 00) never contain pieces. Every bit in those rows
/// and columns should be ignored, except for the last bit. The last bit denotes whose turn it is to
/// play (0 means black's turn; 1 means white's turn). e.g. a potential starting position:
/// Black
/// 00 00 00 00 00 00 00 00 Black
/// 00 03 02 05 06 02 03 00 ♜ ♝ ♛ ♚ ♝ ♜
/// 00 01 01 01 01 01 01 00 ♟ ♟ ♟ ♟ ♟ ♟
@ilamanov
ilamanov / Chess.sol
Last active March 15, 2022 10:55
isValid
contract Chess {
/// @notice Determines whether a move is valid or not (i.e. within bounds and not capturing
/// same colored piece).
/// @dev As mentioned above, the board representation has 2 sentinel rows and columns for
/// efficient boundary validation as follows:
/// 0 0 0 0 0 0 0 0
/// 0 1 1 1 1 1 1 0
/// 0 1 1 1 1 1 1 0
/// 0 1 1 1 1 1 1 0
/// 0 1 1 1 1 1 1 0
@ilamanov
ilamanov / Chess.sol
Last active March 15, 2022 10:57
Board representation
contract Chess {
/// ======================================Board Representation======================================
/// The board is an 8x8 representation of a 6x6 chess board. For efficiency, all information is
/// bitpacked into a single uint256. Thus, unlike typical implementations, board positions are
/// accessed via bit shifts and bit masks, as opposed to array accesses. Since each piece is 4 bits,
/// there are 64 ``indices'' to access:
/// 63 62 61 60 59 58 57 56
/// 55 54 53 52 51 50 49 48
/// 47 46 45 44 43 42 41 40
/// 39 38 37 36 35 34 33 32
@ilamanov
ilamanov / Chess.sol
Last active March 15, 2022 10:57
Piece representation
contract Chess {
/// ======================================Piece Representation======================================
/// Each chess piece is defined with 4 bits as follows:
/// * The first bit denotes the color (0 means black; 1 means white).
/// * The last 3 bits denote the type:
/// | Bits | # | Type |
/// | ---- | - | ------ |
/// | 000 | 0 | Empty |
/// | 001 | 1 | Pawn |
/// | 010 | 2 | Bishop |
contract KittyCore is KittyMinting {
address public newContractAddress;
function setNewAddress(address _v2Address) external onlyCEO whenPaused {
newContractAddress = _v2Address;
ContractUpgrade(_v2Address);
}
}
contract ERC721Metadata {
function getMetadata(uint256 _tokenId, string) public view returns (bytes32[4] buffer, uint256 count) {
if (_tokenId == 1) {
buffer[0] = "Hello World! :D";
count = 15;
} else if (_tokenId == 2) {
buffer[0] = "I would definitely choose a medi";
buffer[1] = "um length string.";
count = 49;
} else if (_tokenId == 3) {