window.Sudoku = class Sudoku constructor: (@board) -> @board.classList.add 'sudoku' @grid = document.createElement 'div' @grid.classList.add 'game-container' @board.appendChild @grid buttons_div = document.createElement 'div' buttons = {check: 'Check', reset: 'Reset', newgame: 'New game', help: '?'} for k,v of buttons button = document.createElement 'button' button.innerHTML = v button.classList.add k buttons_div.appendChild button @board.appendChild buttons_div @board.querySelector('.check').addEventListener('click', @check.bind(this)) @board.querySelector('.reset').addEventListener('click', @reset.bind(this)) check: -> errors = @errors() if errors.length == 0 alert 'Congratulations!' else alert errors.map((el) -> el.message).join() errors: -> solution = @toArray() errors = [] for i in [0..8] square = [] column = [] row = solution[i] for j in [0..8] column.push(solution[j][i]) square.push(solution[(Math.floor(i/3)*3) + Math.floor(j/3)][(i%3*3) + (j%3)]) console.log square console.log column ### # check square [i%3*3][j/3*3] # check row [i][.] # check column [.][i] ### errors generate: (game, solution = false) -> @game = game if game? @grid.innerHTML = @game.map((row) -> '