Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
contract TicTacToe {
PubKey alice;
PubKey bob;
//Represents whether it is alice's turn to play chess
@state
bool is_alice_turn;
//Represents the position of the chessboard. For example, a chess piece with alice in the first row
//and first column is expressed as [1,0,0,0,0,0,0,0,0]
@state
int[9] board;
static const int TURNLEN = 1;
static const int BOARDLEN = 9;
static const int EMPTY = 0;
static const int ALICE = 1;
static const int BOB = 2;
public function move(int n, Sig sig, int amount, SigHashPreimage txPreimage) {
require(n >= 0 && n < BOARDLEN);
// not filled
require(this.board[n] == EMPTY);
int play = this.is_alice_turn ? ALICE : BOB;
PubKey player = this.is_alice_turn ? this.alice : this.bob;
// ensure it's player's turn
require(checkSig(sig, player));
// make the move
this.board[n] = play;
this.is_alice_turn = !this.is_alice_turn;
bytes outputs = b'';
if (this.won(play)) {
bytes outputScript = Utils.buildPublicKeyHashScript(hash160(player));
bytes output = Utils.buildOutput(outputScript, amount);
outputs = output;
}
else if (this.full()) {
bytes aliceScript = Utils.buildPublicKeyHashScript(hash160(this.alice));
bytes aliceOutput = Utils.buildOutput(aliceScript, amount);
bytes bobScript = Utils.buildPublicKeyHashScript(hash160(this.bob));
bytes bobOutput = Utils.buildOutput(bobScript, amount);
outputs = aliceOutput + bobOutput;
}
else {
bytes scriptCode_ = this.getStateScript();
bytes output = Utils.buildOutput(scriptCode_, amount);
outputs = output;
}
require(this.propagateState(txPreimage, outputs));
}
function won(int play) : bool {
int[8][3] lines = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]];
bool anyLine = false;
loop (8) : i {
bool line = true;
loop (3) : j {
line = line && this.board[lines[i][j]] == play;
}
anyLine = anyLine || line;
}
return anyLine;
}
function full() : bool {
bool full = true;
loop (BOARDLEN) : i {
full = full && this.board[i] != TicTacToe.EMPTY;
}
return full;
}
function propagateState(SigHashPreimage txPreimage, bytes outputs) : bool {
require(Tx.checkPreimage(txPreimage));
return (hash256(outputs) == SigHash.hashOutputs(txPreimage));
}
}