Skip to content

Instantly share code, notes, and snippets.

@MarceloAlves
Last active May 21, 2020 05:09
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 MarceloAlves/4047e08b696323ae889452e81e95540a to your computer and use it in GitHub Desktop.
Save MarceloAlves/4047e08b696323ae889452e81e95540a 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 gameMachine = Machine({
id: 'game',
initial: 'idle',
context: {
retries: 0
},
states: {
idle: {
on: {
START_GAME: 'running'
}
},
running:{
initial: 'setup',
states: {
setup: {
on: {
'': {
actions: ['setPlayers', 'selectStartingPlayer'],
target: 'playerTurn'
}
}
},
playerTurn: {
initial: 'awaitingRoll',
states: {
awaitingRoll: {
on: {
ROLL_DICE: 'rolling'
}
},
rolling: {
entry: 'rollDice',
after: {
1500: {
target: 'selectMarble'
}
}
},
selectMarble: {
on: {
SELECT: {
actions: ['selectMarble'],
target: 'findingMoves'
}
}
},
findingMoves: {
entry: ['findAvailableSpots'],
on: {
'': [{
cond: 'hasAvailableMoves',
target: 'movingMarble'
}, {
action: 'showNoMovesMessage',
target: 'selectMarble'
}]
}
},
movingMarble: {
on: {
MOVE: {
actions: ['moveMarble'],
target: '#game.running.playerTurn.finishMove'
}
}
},
finishMove: {
entry: ['checkForWinner', 'checkMarbleOvertake'],
on: {
'': [{
cond: 'hasWinner',
target: '#game.finish'
},{
action: ['resetPlayerState', 'selectNextPlayer'],
target: '#game.running.playerTurn.awaitingRoll'
}]
}
},
}
}
}
},
finish: {
type: 'final'
}
}
},{
guards: {
hasAvailableMoves: (ctx, evt) => true,
hasWinner: (ctx, evt) => false,
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment