Skip to content

Instantly share code, notes, and snippets.

@mattflo
Created October 31, 2012 01:44
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 mattflo/3984330 to your computer and use it in GitHub Desktop.
Save mattflo/3984330 to your computer and use it in GitHub Desktop.
# node, jasmine, coffeescript, and rake installed/configured using
# steps outlined at this website:
# http://brizzled.clapper.org/id/117/index.html
# gem install guard
# gem install guard-shell
# guard init
# then put the following in GuardFile
# guard :shell do
# watch('.*') {`rake`}
# end
bombs = []
describe "minesweeper bomb cells", ->
beforeEach ->
bombs = [[0,0]]
it "explodes", ->
expect(play(0,0)).toContain "YOU LOST"
it "shoud have one nighboring bombs", ->
expect(play(0,1)).toEqual 1
it "shouldn't have any nighboring bombs", ->
expect(play(0,2)).toEqual 0
describe "adjacent cells", ->
beforeEach ->
@neighbors = adjacent([0,0])
it "looks in the negative x direction", ->
expect(@neighbors).toContain [-1,0]
it "looks in the negative y direction", ->
expect(@neighbors).toContain [0,-1]
it "looks in the positive x direction", ->
expect(@neighbors).toContain [1,0]
it "looks in the positive y direction", ->
expect(@neighbors).toContain [0,1]
it "looks in the positive y, positive x direction", ->
expect(@neighbors).toContain [1,1]
it "looks in the negative y, negative x direction", ->
expect(@neighbors).toContain [-1,-1]
it "looks in the positive y, negative x direction", ->
expect(@neighbors).toContain [-1,1]
it "looks in the negative y, positive x direction", ->
expect(@neighbors).toContain [1,-1]
adjacent = (cell) ->
[
[cell[0]-1,cell[1]],
[cell[0],cell[1]-1],
[cell[0]+1,cell[1]],
[cell[0],cell[1]+1],
[cell[0]+1,cell[1]+1],
[cell[0]-1,cell[1]-1],
[cell[0]-1,cell[1]+1],
[cell[0]+1,cell[1]-1],
]
play = (x, y) ->
return "GAME OVER!!! YOU LOST!!!" if bombs.some (c) -> c[0] == x and c[1] == y
neighboringBombs = intersection bombs, adjacent([x,y])
neighboringBombs.length
intersection = (a, b) ->
[a, b] = [b, a] if a.length > b.length
value for value in a when b.some (c) -> c[0] == value[0] && c[1] == value[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment