Last active
September 10, 2021 18:46
-
-
Save vnglst/6519b479949768376410e3ef1134e1e4 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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