tictactoe - Circuit.if or if

Hello friends.

I’m trying to understand the code.

Can explain why this code doesn’t work

 update(x: Field, y: Field, playerToken: Bool) {
    for (let i = 0; i < 3; i++) {
      for (let j = 0; j < 3; j++) {
        // is this the cell the player wants to play?
        const toUpdate = x.equals(new Field(i)).and(y.equals(new Field(j)));

        // make sure we can play there
        toUpdate.and(this.board[i][j].isSome).assertEquals(false);

        if (toUpdate) {
          this.board[i][j] = new Optional(new Bool(true), playerToken);
        }

        // this.board[i][j] = Circuit.if(
        //   toUpdate,
        //   new Optional(new Bool(true), playerToken),
        //   this.board[i][j]
        // );
      }
    }
  }
Error: [[],[["Update_not_permitted_app_state"],["Account_app_state_precondition_unsatisfied",2]]]
    at raise_error (/Users/alex/Developer/zk/tictactoe/node_modules/snarkyjs/dist/node/_node_bindings/snarky_js_node.bc.cjs:430864:44)
    at apply_json_transaction (/Users/alex/Developer/zk/tictactoe/node_modules/snarkyjs/dist/node/_node_bindings/snarky_js_node.bc.cjs:433764:29)
    at caml_call_gen (/Users/alex/Developer/zk/tictactoe/node_modules/snarkyjs/dist/node/_node_bindings/snarky_js_node.bc.cjs:2081:17)
    at Object.applyJsonTransaction (/Users/alex/Developer/zk/tictactoe/node_modules/snarkyjs/dist/node/_node_bindings/snarky_js_node.bc.cjs:3651:16)
    at Object.sendTransaction (file:///Users/alex/Developer/zk/tictactoe/node_modules/snarkyjs/dist/node/lib/mina.js:224:24)
    at async sendTransaction (file:///Users/alex/Developer/zk/tictactoe/node_modules/snarkyjs/dist/node/lib/mina.js:543:12)
    at async Object.send (file:///Users/alex/Developer/zk/tictactoe/node_modules/snarkyjs/dist/node/lib/mina.js:142:20)
    at async makeMove (file:///Users/alex/Developer/zk/tictactoe/build/src/run.js:87:5)
    at async file:///Users/alex/Developer/zk/tictactoe/build/src/run.js:55:1

this code works without errors

 update(x: Field, y: Field, playerToken: Bool) {
    for (let i = 0; i < 3; i++) {
      for (let j = 0; j < 3; j++) {
        // is this the cell the player wants to play?
        const toUpdate = x.equals(new Field(i)).and(y.equals(new Field(j)));

        // make sure we can play there
        toUpdate.and(this.board[i][j].isSome).assertEquals(false);

        // if (toUpdate) {
        //   this.board[i][j] = new Optional(new Bool(true), playerToken);
        // }

        this.board[i][j] = Circuit.if(
          toUpdate,
          new Optional(new Bool(true), playerToken),
          this.board[i][j]
        );
      }
    }
  }

Thx.

Have you seen this section of the docs: Tutorial 5: Common Types and Functions | Mina Documentation and How to Write a zkApp | Mina Documentation

You can’t use traditional conditional statements currently.

1 Like