乘风原创程序

  • 比特币上的数独游戏合约的实现代码
  • 2021/1/19 9:38:56
  • 我们在 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);
     }
    }

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