Created
October 31, 2012 01:44
-
-
Save mattflo/3984330 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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