我们在 Bitcoin SV 上实现了一个数独游戏智能合约,利用之前介绍过的一种合约范式可以将游戏中寻找解题方案的过程外包上链。因为求解数独问题的计算工作量会随着其行列数快速增长,实际上它也是一个 NP-完全 问题。不过我们可以借由比特币智能合约巧妙地寻求答案,只需要验证答案提供者所给出的解答是否满足要求即可,这样即可以将复杂的求解计算过程实现链下外包。
sCrypt 合约代码如下:
import "util.scrypt"; import "array.scrypt"; contract Sudoku { bytes board; static const int N = 9; static bytes EMPTY = b'00'; constructor(bytes board) { this.board = board; } function merge(bytes solution) : bytes { bytes newBoard = this.board; int i = 0; loop (N) { int j = 0; loop (N) { int value = this.readValue(newBoard, i, j); int inputValue = this.readValue(solution, i, j); if (value == 0) { require(inputValue <= 9); newBoard = this.setValue(newBoard, i, j, inputValue); } else { require(value == inputValue); } j++; } i++; } return newBoard; } public function solve(bytes solution) { require(len(solution) == Sudoku.N * Sudoku.N); bytes newBord = this.merge(solution); Array rowArray = new Array(); Array colArray = new Array(); Array squareArray = new Array(); int i = 0; loop (N) { int j = 0; loop (N) { // check for duplicate // in a row int rowElem = this.readValue(newBord, i, j); require(rowArray.indexOf(rowElem) == -1); rowArray.push(rowElem); // in a column int colElem = this.readValue(newBord, j, i); require(colArray.indexOf(colElem) == -1); colArray.push(colElem); // in a subgrid int squareElem = this.readSquareValue(newBord, i, j); require(squareArray.indexOf(squareElem) == -1); squareArray.push(squareElem); j++; } rowArray.clear(); colArray.clear(); squareArray.clear(); i++; } require(true); } static function readValue(bytes board, int i, int j): int { return Util.fromLEUnsigned(Util.getElemAt(board, Sudoku.index(i, j))); } static function setValue(bytes board, int i, int j, int value): bytes { return Util.setElemAt(board, this.index(i, j), Util.toLEUnsigned(value, 1)); } static function readSquareValue(bytes board, int i, int j): int { return Util.fromLEUnsigned(Util.getElemAt(board, Sudoku.indexSquare(i, j))); } static function index(int row, int col) : int { return row * Sudoku.N + col; } static function indexSquare(int i, int j) : int { int row = i / 3 * 3 + j / 3; int col = i % 3 * 3 + j % 3; return Sudoku.index(row, col); } }
标签:
比特币数独游戏合约,比特币合约
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
岱庙资源网 Copyright www.zgmyg.com
暂无“比特币上的数独游戏合约的实现代码”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年05月18日
2024年05月18日
- 群星.1986-新格名曲精丫新格】【WAV+CUE】
- 群星.1986-新格名曲精选-歌3【新格】【WAV+CUE】
- 妙音唱片净化心灵·归还真我群星-无尘系列6CDWAV+CUE
- 孙协志2004-胜立精选(闽南语)[大旗][WAV+CUE]
- 徐若瑄《先听我说完》[320K/MP3][93.78MB]
- 徐若瑄《先听我说完》[FLAC/分轨][498.77MB]
- Resa Club《Soft Opening(Explicit)》[320K/MP3][72.41MB]
- 宝丽金群星.1997-舞池中Remix【宝丽金】【WAV+CUE】
- 吕继宏2010-广州独唱音乐会LIVE专辑[DMC唱片][WAV+CUE]
- 李丹阳1999-红月亮[上海音像][WAV+CUE]
- 廖昌永2007-怀念[中唱][WAV+CUE]
- 區瑞強1999-極品陌上歸人II[FLAC]
- [In-AkustikINAK7961]ATastySoundCollection-CoffeeLatin《咖啡和拉斗(2009)[WAV+CUE]
- Resa Club《Soft Opening(Explicit)》[FLAC/分轨][192.37MB]
- 紫龙游戏《钢岚 (原声音乐辑)》[320K/MP3][145.56MB]