当前位置:   article > 正文

使用 sCrypt 实现数独游戏合约_scrypt .net

scrypt .net

我们在 BSV 区块链上实现了一个数独游戏智能合约,利用之前介绍过的一种合约范式可以将游戏中寻找解题方案的过程外包上链。因为求解数独问题的计算工作量会随着其行列数快速增长,实际上它也是一个 NP-完全 问题。不过我们可以借由 sCrypt 智能合约巧妙地寻求答案,只需要验证答案提供者所给出的解答是否满足要求即可,这样即可以将复杂的求解计算过程实现链下外包。

典型的数独游戏

sCrypt 合约代码如下:


import "array.scrypt";
import "arrayUtil.scrypt";

contract Sudoku {

    bytes board;

    static const int N = 9;
    static const bytes EMPTY = b'00';

    constructor(bytes board) {
        this.board = board;
    }

    function merge(bytes solution) : bytes {
        bytes newBoard = this.board;

        loop (N) : i {
            loop (N) : j {
                int value = readValue(newBoard, i, j);
                int inputValue = readValue(solution, i, j);
                if (value == 0) {
                    require(inputValue <= 9);
                    newBoard = setValue(newBoard, i, j, inputValue);
                }
                else {
                    require(value == inputValue);
                }
            }
        }
        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();

        loop (N) : i {
            loop (N) : j {
                // check for duplicate

                // in a row
                int rowElem = readValue(newBord, i, j);
                require(rowArray.indexOf(rowElem) == -1);
                rowArray.push(rowElem);

                // in a column
                int colElem = readValue(newBord, j, i);
                require(colArray.indexOf(colElem) == -1);
                colArray.push(colElem);

                // in a subgrid
                int squareElem = readSquareValue(newBord, i, j);
                require(squareArray.indexOf(squareElem) == -1);
                squareArray.push(squareElem);
            }

            rowArray.clear();
            colArray.clear();
            squareArray.clear();
        }

        require(true);
    }

    static function readValue(bytes board, int i, int j) : int {
        return Utils.fromLEUnsigned(ArrayUtil.getElemAt(board, Sudoku.index(i, j)));
    }

    static function setValue(bytes board, int i, int j, int value) : bytes {
        return ArrayUtil.setElemAt(board, index(i, j), Utils.toLEUnsigned(value, 1));
    }

    static function readSquareValue(bytes board, int i, int j) : int {
        return Utils.fromLEUnsigned(ArrayUtil.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);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/159624
推荐阅读
相关标签
  

闽ICP备14008679号