Skip to content

Instantly share code, notes, and snippets.

@sesteva
Last active June 11, 2020 14:43
Show Gist options
  • Save sesteva/6500d57f5821a3e76c0e9fb09d7b6a08 to your computer and use it in GitHub Desktop.
Save sesteva/6500d57f5821a3e76c0e9fb09d7b6a08 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const gameMachine = Machine(
{
id: "pyramid",
initial: "play",
context: {
bet: 25,
levels: 3,
currentLevel: 1,
pyramid: {
// 0 => losing element
// 1 => winning element
// 2 => bonus element
1: {
winningProbability: 0.9,
payoffFactor: 100, // 1.00,
vector: [1, 0, 1, 1, 1, 1, 1, 1, 1, 1]
},
2: {
winningProbability: 0.9,
payoffFactor: 100, // 1.00,
vector: [1, 1, 1, 0, 1, 1, 1, 1, 2, 1]
},
3: {
winningProbability: 0.9,
payoffFactor: 100, // 1.00
vector: [1, 1, 1, 1, 1, 1, 1, 1, 1, 0]
}
}
},
states: {
play: {
on: {
PICK: [
{
target: "lost",
cond: "foundLosingElement"
},
{
target: "bonus",
cond: "foundBonusElement",
actions: ["incrementCurrentLevel"]
},
{
target: "celebration",
cond: "noMoreMoreLevels"
},
{ target: "play", actions: ["incrementCurrentLevel"] }
],
CHANGE_BET: "play",
HELP: "help",
HISTORY: "history",
EXIT: "exit"
}
},
bonus: {
after: {
2500: "play"
}
},
celebration: {
after: {
2500: "reset"
}
},
lost: {
after: {
2500: "reset"
}
},
fund: {
on: {
ACCEPT: "play",
CANCEL: "play"
}
},
history: {
id: "history",
initial: "empty",
states: {
empty: {
on: {
ACCEPT: "#pyramid.play"
}
},
full: {
on: {
NEXT: "#pyramid.history",
PREV: "#pyramid.history",
EXIT: "#pyramid.play"
}
}
}
},
reset: {
entry: ["resetBoard"],
after: {
500: "play"
}
},
help: {
on: {
NEXT: "help",
CLOSE: "play"
}
},
exit: {
type: "final"
}
}
},
{
guards: {
foundLosingElement: (_, event) => event.payload === 0,
foundBonusElement: (_, event) => event.payload === 2,
noMoreMoreLevels: (context, event) =>
event.payload === 1 && context.currentLevel === context.levels
},
actions: {
incrementCurrentLevel: assign({
currentLevel: ctx => ctx.currentLevel + 1
}),
resetBoard: assign({
currentLevel: 1
})
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment