Skip to content

Instantly share code, notes, and snippets.

@jmdejong
Last active October 3, 2020 14:49
Show Gist options
  • Select an option

  • Save jmdejong/9d825f114f3301580d7c67d935a9ba71 to your computer and use it in GitHub Desktop.

Select an option

Save jmdejong/9d825f114f3301580d7c67d935a9ba71 to your computer and use it in GitHub Desktop.
// helper function for counting the number of adjacent mines
// This is a function of one argument that returns a function of one argument
// This is actually an interesting syntax to make curryable functions in javascript
f=i=>x=>s+=(c[i+x]|0)+(c[i-x]|0);
// board size
// The board is actually one column wider to prevent edge wrapping
// this variable will be used to know how many fields should be cleared before you won
m=256;
for(i=272;i--;)
// there is some undocumented browser behaviour that makes the id of an element a variable in javascript
// (at least, I coudn't find documentation)
// the variable refers to the element
// since we have this objects, and numbers can be properties of an object, this object also holds the main game field
c.innerHTML += i%17 ?
// for every tile not on the invisible border
`<a onclick=event.target.outerHTML=c[i=${
// set whether a mine is there
// random takes no arguments, so that's a good place to put an unrelated expression
// decrease m for every mine
m -=c[i]=Math.random(a=alert)<.2,
i // and return the index
// when clicked and it is a mine, explode
// when it is not a mine fill in the number
// the indices of adjacent tiles are -18, -17, -16, -1, 1, 16, 17, 18
// the function f takes care of the negatives
// again, f is a curried function so f(i) returns a function of one argument
// decrease m (number of tiles left to clear) and if it is 0 then you won
}]?(a("BOOM"),"X"):[1,16,17,18].map(f(i),s=0,--m||a("=D"))|s>#</a> `
:
// the invisible border is drawn as a newline
`
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment