Skip to content

Instantly share code, notes, and snippets.

@stackdump
Created June 4, 2023 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stackdump/b57e2756bb783c8182084c380dcfbd8e to your computer and use it in GitHub Desktop.
Save stackdump/b57e2756bb783c8182084c380dcfbd8e to your computer and use it in GitHub Desktop.
TicTacToeModel
// REVIEW visual model design in JS:
// https://pflow.dev/chainlink2023/tictactoe/
contract TicTacToeModel is MetamodelUint8, Uint8ModelFactory {
enum Roles{ X, O, HALT }
enum Properties {
_00, _01, _02,
_10, _11, _12,
_20, _21, _22,
SIZE
}
enum Actions {
// x moves
X00, X01, X02,
X10, X11, X12,
X20, X21, X22,
// O moves
O00, O01, O02,
O10, O11, O12,
O20, O21, O22,
HALT
}
function addMove(Properties prop, Actions action, Roles role) internal {
if (role >= Roles.HALT) {
revert("Invalid role");
}
if (action >= Actions.HALT) {
revert("Invalid action");
}
txn(1, places[uint8(prop)], fn(uint8(Properties.SIZE), uint8(action), uint8(role)));
}
constructor() {
cell(1, 1); // _00
cell(1, 1); // _01
cell(1, 1); // _02
cell(1, 1); // _10
cell(1, 1); // _11
cell(1, 1); // _12
cell(1, 1); // _20
cell(1, 1); // _21
cell(1, 1); // _22
addMove(Properties._00, Actions.X00, Roles.X);
addMove(Properties._01, Actions.X01, Roles.X);
addMove(Properties._02, Actions.X02, Roles.X);
addMove(Properties._10, Actions.X10, Roles.X);
addMove(Properties._11, Actions.X11, Roles.X);
addMove(Properties._12, Actions.X12, Roles.X);
addMove(Properties._20, Actions.X20, Roles.X);
addMove(Properties._21, Actions.X21, Roles.X);
addMove(Properties._22, Actions.X22, Roles.X);
addMove(Properties._00, Actions.O00, Roles.O);
addMove(Properties._01, Actions.O01, Roles.O);
addMove(Properties._02, Actions.O02, Roles.O);
addMove(Properties._10, Actions.O10, Roles.O);
addMove(Properties._11, Actions.O11, Roles.O);
addMove(Properties._12, Actions.O12, Roles.O);
addMove(Properties._20, Actions.O20, Roles.O);
addMove(Properties._21, Actions.O21, Roles.O);
addMove(Properties._22, Actions.O22, Roles.O);
}
function declaration() public view returns (Uint8Model.PetriNet memory) {
return Uint8Model.PetriNet(places, transitions);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment