Skip to content

Instantly share code, notes, and snippets.

@pke
Last active April 1, 2021 21:27
Show Gist options
  • Save pke/4de6b20174d4b0d3f5ed0078addce6d1 to your computer and use it in GitHub Desktop.
Save pke/4de6b20174d4b0d3f5ed0078addce6d1 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 hasEnded = (ctx) => false
const randomColor = (ctx) => Object.keys(ctx.lanes)[Math.ceil(Math.random()*6)-1]
const isFisher = colour => ["green", "red"].indexOf(colour) !== -1
const fetchMachine = Machine({
id: 'fishgame',
initial: 'waitForRoll',
context: {
lanes: {
red: 0,
green: 0,
blue: 5,
yellow: 5,
orange: 5,
pink: 5
},
color: ""
},
states: {
idle: {
on: {
START: "waitForRoll"
}
},
waitForRoll: {
on: {
ROLL_DICE: {
target: "diceRolled",
actions: "rollDice"
}
}
},
diceRolled: {
on: {
"": [{
target: "moveBoat",
cond: "isFisher"
}, {
target: "moveFish"
}]
}
},
moveBoat: {
},
moveFish: {
on: {
"": "waitForRoll"
}
},
fishWon: {
type: "final"
},
fisherWon: {
type: "final"
},
noWinner: {
type: "final"
}
}
}, {
guards: {
isFisher: (ctx) => isFisher(ctx.color)
},
actions: {
rollDice: assign(ctx => ({
color: randomColor(ctx)
}))
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment