Created
December 4, 2011 06:38
-
-
Save searls/1429431 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class window.Sudoku | |
constructor: (board) -> | |
@board = board | |
solve: -> | |
[] | |
column: (index) -> | |
(cell for cell,cellIndex in @board when cellIndex % 9 == index) | |
row: (index) -> | |
@board.slice(index*9, index*9+9) | |
box: (index) -> | |
mod3 = index % 3 | |
i = (index-mod3)*9 + mod3*3 | |
_([0..2]).chain().map((row) -> @board.slice(i+row*9,i+row*9+3)).flatten().value() | |
isValid: (bunch) -> | |
_(bunch).intersection([1..9]).length == 9 |
This file contains hidden or 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
describe "Sudoku", -> | |
n=null | |
Given -> @board = [ | |
7,9,n, n,n,n, 3,n,n, | |
n,n,n, n,n,6, 9,n,n, | |
8,n,n, n,3,n, n,7,6, | |
n,n,n, n,n,5, n,n,2, | |
n,n,5, 4,1,8, 7,n,n, | |
4,n,n, 7,n,n, n,n,n, | |
6,1,n, n,9,n, n,n,8, | |
n,n,2, 3,n,n, n,n,n, | |
n,n,9, 3,n,n, n,n,n | |
] | |
Given -> @sudoku = new Sudoku(@board) | |
xdescribe "#solve", -> | |
When -> @solution = @sudoku.solve() | |
Then -> expect(@solution).toEqual [ | |
7,9,6, 8,5,4, 3,2,1, | |
2,4,3, 1,7,6, 9,8,5, | |
8,5,1, 2,3,9, 4,7,6, | |
1,3,7, 9,6,5, 8,4,2, | |
9,2,5, 4,1,8, 7,6,3, | |
4,6,8, 7,2,3, 5,1,9, | |
6,1,4, 5,9,7, 2,3,8, | |
5,8,2, 3,4,1, 6,9,7, | |
3,7,9, 6,8,2, 1,5,4 | |
] | |
describe "#column", -> | |
context "first row", -> | |
When -> @result = @sudoku.column(0) | |
Then -> expect(@result).toEqual [7,n,8,n,n,4,6,n,n] | |
context "fifth row", -> | |
When -> @result = @sudoku.column(4) | |
Then -> expect(@result).toEqual [n,n,3,n,1,n,9,n,n] | |
describe "#row", -> | |
context "third row", -> | |
When -> @result = @sudoku.row(2) | |
Then -> expect(@result).toEqual [8,n,n,n,3,n,n,7,6] | |
context "seventh row", -> | |
When -> @result = @sudoku.row(6) | |
Then -> expect(@result).toEqual [6,1,n,n,9,n,n,n,8] | |
describe "#box", -> | |
context "the center box", -> | |
When -> @result = @sudoku.box(4) | |
Then -> expect(@result).toEqual [n,n,5, | |
4,1,8, | |
7,n,n] | |
context "the bottom-right box", -> | |
When -> @result = @sudoku.box(8) | |
Then -> expect(@result).toEqual [n,n,8, | |
n,n,n, | |
n,n,n] | |
describe "#isValid", -> | |
context "missing 5", -> | |
When -> @result = @sudoku.isValid([1,3,7, 9,6,null, 8,4,2,]) | |
Then -> expect(@result).toEqual false | |
context "two 2's", -> | |
When -> @result = @sudoku.isValid([1,2,7, 9,6,5, 8,4,2,]) | |
Then -> expect(@result).toEqual false | |
context "all of 1 through 9", -> | |
When -> @result = @sudoku.isValid([1,3,7, 9,6,5, 8,4,2]) | |
Then -> expect(@result).toEqual true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment