Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Taking mutability out of the state machine

Matt Ball
October 01, 2013

Taking mutability out of the state machine

Or things I learned, building a functional snake clone using F# and XNA

Matt Ball

October 01, 2013
Tweet

More Decks by Matt Ball

Other Decks in Programming

Transcript

  1. Taking mutability out of the state machine Or things I

    learned, building a functional snake clone using F# and XNA @MattDrivenDev at #IWDev 16
  2. What and why? • Hit “the wall” this summer –

    needed something to catch my attention. • Inspired by @GarethIW’s #1GAM efforts I wanted to make a little game prototype. • Snake seemed simple and doable. • Graphics are nothing more than a coloured rectangle. • Functional code in F#, because it is fun and forced me to think differently. #IWDev 16/ Matt Ball / @saxonmatt
  3. State and Behaviour • (OOP) Objects represent state + behaviour,

    communicating with other Objects. • (FP) state and behaviour are separated into values and functions. #IWDev 16/ Matt Ball / @saxonmatt Snake Class State: position, size, colour etc. Behaviour: move, eat, grow Behaviours modify the state of this snake. Snake Data Position, size, colour. Snake Functions Move, eat, grow Functions take a snake and return a new snake
  4. Game Loop • Big fat while loop that allows us

    to capture time and input and tell our game engine to do things. • (OOP) call methods on objects with input/time and change their state – then draw that state to the screen. • (FP) take the value of the last known state pass it through functions to get new values for the state – then draw that state to the screen. #IWDev 16/ Matt Ball / @saxonmatt