Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vnglst
Last active September 10, 2021 18:46
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 vnglst/6519b479949768376410e3ef1134e1e4 to your computer and use it in GitHub Desktop.
Save vnglst/6519b479949768376410e3ef1134e1e4 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const addNumber = assign({
numbers: (ctx, event) => {
let { howMany = 5 } = event;
let toAdd = [];
for (let i = 0; i < howMany; i++) {
toAdd.push(Math.floor(Math.random() * 9));
}
return [...ctx.numbers, ...toAdd];
},
});
// Guard game over
function isGameOver(context) {
return context.numbers.length >= 20;
}
const gameMachine = Machine(
{
initial: "start",
context: { numbers: [] },
states: {
start: {
on: {
ADD_NUMBER: {
target: "playing",
actions: "addNumber",
},
},
},
playing: {
always: {
target: "gameover",
cond: "isGameOver",
},
on: {
ADD_NUMBER: {
target: "playing",
actions: "addNumber",
},
},
},
gameover: {},
},
},
{ actions: { addNumber }, guards: { isGameOver } }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment