比特币上的数独游戏合约的实现代码

 更新时间:2021年1月19日 20:00  点击:1668

我们在 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);
 }
}

到此这篇关于比特币上的数独游戏合约的实现代码的文章就介绍到这了,更多相关比特币数独游戏合约内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

[!--infotagslink--]

相关文章

  • 教你用go语言实现比特币交易功能(Transaction)

    每一笔比特币交易都会创造输出,输出都会被区块链记录下来。给某个人发送比特币,实际上意味着创造新的 UTXO 并注册到那个人的地址,可以为他所用,今天通过本文给大家分享go语言实现比特币交易功能,一起看看吧...2021-05-13
  • 使用Python的开发框架Brownie部署以太坊智能合约

    在本文中,我们将使用Python部署智能合约。这篇文章可能是您走向智能合约和区块链开发的桥梁!...2021-05-28
  • C语言实现数独游戏的求解

    这篇文章主要为大家详细介绍了C语言实现数独游戏的求解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • C语言数独游戏的求解方法

    这篇文章主要为大家详细介绍了C语言数独游戏的求解方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • 比特币上的数独游戏合约的实现代码

    这篇文章主要介绍了比特币上的数独游戏合约的实现代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-01-19
  • go语言实战之实现比特币地址校验步骤

    这篇文章主要介绍了go语言实战之实现比特币地址校验步骤,利用生产的随机数采用椭圆加密算法生成公钥,具体步骤实例代码请参考下本文...2021-05-13