比特币上的数独游戏合约的实现代码
作者:freedomhero 时间:2023-10-13 00:46:04
我们在 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);
}
}
来源:https://blog.csdn.net/freedomhero/article/details/112758212
标签:比特币,数独游戏,合约
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
js+CSS实现弹出居中背景半透明div层的方法
2024-04-18 10:52:51
python人工智能算法之决策树流程示例详解
2022-02-27 17:34:31
Oracle SQL性能优化系列学习二
2010-07-23 13:23:00
python scatter散点图用循环分类法加图例
2021-07-26 01:44:01
![](https://img.aspxhome.com/file/2023/9/130569_0s.jpg)
MybatisPlus BaseMapper 实现对数据库增删改查源码
2024-01-26 10:03:57
![](https://img.aspxhome.com/file/2023/7/72317_0s.jpg)
Python3直接爬取图片URL并保存示例
2022-10-31 17:30:40
python实现kNN算法
2023-01-24 13:58:06
![](https://img.aspxhome.com/file/2023/1/78831_0s.png)
js算法实例之字母大小写转换
2024-04-16 08:52:05
![](https://img.aspxhome.com/file/2023/2/136832_0s.png)
python实现LBP方法提取图像纹理特征实现分类的步骤
2023-05-24 02:12:27
![](https://img.aspxhome.com/file/2023/9/76739_0s.png)
Python使用文件锁实现进程间同步功能【基于fcntl模块】
2022-07-04 17:45:15
mysql常用命令行操作语句
2024-01-12 23:51:20
JavaScript 各种动画渐变效果
2008-09-02 10:38:00
Python爬虫获取基金变动信息
2022-08-15 21:57:15
![](https://img.aspxhome.com/file/2023/5/93975_0s.webp)
Python实现消消乐小游戏
2021-02-19 19:37:42
![](https://img.aspxhome.com/file/2023/5/90585_0s.jpg)
Oracle 数据库中创建合理的数据库索引
2009-07-02 12:31:00
详解将Python程序(.py)转换为Windows可执行文件(.exe)
2022-05-29 20:46:25
![](https://img.aspxhome.com/file/2023/8/105868_0s.png)
Mysql Innodb 引擎优化
2010-10-25 20:01:00
教你两步解决conda安装pytorch时下载速度慢or超时的问题
2022-04-12 17:19:59
![](https://img.aspxhome.com/file/2023/5/110435_0s.png)
mysql中的临时表如何使用
2024-01-28 10:19:21
![](https://img.aspxhome.com/file/2023/3/71713_0s.png)
pytorch 使用单个GPU与多个GPU进行训练与测试的方法
2022-04-04 10:39:07