Created
October 11, 2018 07:11
-
-
Save smartcontracts/13bb7005daf3729a8c492a515b9758d7 to your computer and use it in GitHub Desktop.
Hmm...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.0; | |
import "./BrainFuck.sol"; | |
import "./KittyInterface.sol"; | |
contract MindBreed { | |
KittyInterface kittyContract = KittyInterface(0x06012c8cf97BEaD5deAe237070F9587f8E7A266d); | |
mapping (address => bytes1[]) programs; | |
event BountyClaimed( | |
address winner | |
); | |
function () public payable {} | |
function setInstruction(uint256 _kittyId) public { | |
kittyContract.transferFrom(msg.sender, address(this), _kittyId); | |
programs[msg.sender].push(translateKitty(_kittyId)); | |
} | |
function claimBounty() public { | |
bytes memory input; | |
bytes32 result = keccak256(BrainFuck.execute(getProgram(msg.sender), input)); | |
bytes32 target = keccak256(abi.encodePacked(bytes2(0x6869))); // hi | |
require(result == target, "Invalid result."); | |
msg.sender.transfer(address(this).balance); | |
emit BountyClaimed(msg.sender); | |
} | |
function translateKitty(uint256 _kittyId) internal view returns (bytes1) { | |
// Lookup table | |
bytes1[256] memory lookup; | |
lookup[0] = 0x2B; | |
lookup[1] = 0x2C; | |
lookup[3] = 0x2D; | |
lookup[4] = 0x2E; | |
lookup[5] = 0x3C; | |
lookup[7] = 0x3E; | |
lookup[9] = 0x5B; | |
lookup[10] = 0x5D; | |
uint256 genes; | |
(, , , , , , , , , genes) = kittyContract.getKitty(_kittyId); | |
bytes1 instruction = lookup[genes & 0x0F]; | |
require(instruction != 0x00, "Invalid instruction."); | |
return instruction; | |
} | |
function getProgram(address _user) internal view returns (bytes) { | |
uint length = programs[_user].length; | |
bytes memory program = new bytes(length); | |
for (uint i = 0; i < length; i++) { | |
program[i] = programs[_user][i]; | |
} | |
return program; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment