Skip to content

Instantly share code, notes, and snippets.

@TheDutchCoder
Last active December 5, 2020 01:02
Show Gist options
  • Save TheDutchCoder/6d96dbb630e55caf9924bc37db39f4d9 to your computer and use it in GitHub Desktop.
Save TheDutchCoder/6d96dbb630e55caf9924bc37db39f4d9 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const hanabiMachine = Machine({
id: 'hanabi',
initial: 'ready',
context: {
cards: [{suit: 1, value: 3}, {suit: 2, value: 2}, {suit: 0, value: 1}, {suit: 0, value: 2}, {suit: 4, value: 1}, {suit: 0, value: 5}, {suit: 2, value: 2}, {suit: 4, value: 3}, {suit: 3, value: 2}, {suit: 0, value: 1}, {suit: 0, value: 4}, {suit: 2, value: 1}, {suit: 2, value: 4}, {suit: 4, value: 2}, {suit: 0, value: 3}, {suit: 2, value: 1}, {suit: 3, value: 1}, {suit: 0, value: 1}, {suit: 2, value: 5}, {suit: 1, value: 1}, {suit: 3, value: 5}, {suit: 1, value: 5}, {suit: 3, value: 2}, {suit: 0, value: 3}, {suit: 1, value: 2}, {suit: 4, value: 1}, {suit: 4, value: 5}, {suit: 0, value: 2}, {suit: 2, value: 3}, {suit: 4, value: 4}, {suit: 2, value: 3}, {suit: 0, value: 4}, {suit: 3, value: 4}, {suit: 4, value: 1}, {suit: 1, value: 4}, {suit: 3, value: 3}, {suit: 4, value: 3}, {suit: 3, value: 1}, {suit: 1, value: 3}, {suit: 1, value: 2}, {suit: 1, value: 1}, {suit: 4, value: 4}, {suit: 2, value: 1}, {suit: 3, value: 1}, {suit: 2, value: 4}, {suit: 4, value: 2}, {suit: 1, value: 1}, {suit: 3, value: 3}, {suit: 1, value: 4}, {suit: 3, value: 4}],
clues: 3,
bombs: 0,
discarded: [],
played: {
0: [],
1: [],
2: [],
3: [],
4: [],
}
},
states: {
ready: {
on: {
CLUE: 'clue',
DISCARD: 'discard',
PLAY: 'play',
}
},
clue: {
invoke: {
src: () => {},
}
},
discard: {
invoke: {
src: () => {},
}
},
play: {
invoke: {
src: (ctx, evt) => {
if (ctx.played[evt.data.suit].length === evt.data.value - 1) {
ctx.played[evt.data.suit].push(evt.data.value)
return Promise.resolve()
} else {
ctx.bombs += 1
return Promise.resolve()
}
},
onDone: 'next'
}
},
next: {
invoke: {
src: (ctx) => {
if (ctx.bombs !== 3 && ctx.cards.length > 0) {
return Promise.resolve()
}
return Promise.reject()
},
onDone: 'ready',
onError: 'final',
}
},
final: '',
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment