Contrato de juego de Sudoku en Bitcoin

Hemos implementado un contrato inteligente de juego de Sudoku en Bitcoin SV. Usando un paradigma de contrato introducido anteriormente, podemos subcontratar el proceso de encontrar una solución en el juego en la cadena. Debido a que la carga de trabajo computacional de resolver problemas de Sudoku aumentará rápidamente con el número de filas y columnas, de hecho también es un problema NP-completo . Sin embargo, podemos utilizar el contrato inteligente de Bitcoin para buscar inteligentemente la respuesta. Solo necesitamos verificar si la respuesta dada por el proveedor de respuestas cumple con los requisitos, de modo que el complejo proceso de cálculo de la solución se pueda subcontratar fuera de la cadena.

Juego típico de Sudoku

El código de contrato de sCrypt es el siguiente:

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);
    }
}

Supongo que te gusta

Origin blog.csdn.net/freedomhero/article/details/112758212
Recomendado
Clasificación