diff --git a/views/application.coffee b/views/application.coffee index 6c85497..6c1ad59 100644 --- a/views/application.coffee +++ b/views/application.coffee @@ -1,14 +1,18 @@ window.Nikoli = Nikoli = {} class Nikoli.Game - constructor: (@board, @name) -> + constructor: (@board, @name, @url) -> @name = 'nikoli' unless @name? + @url = "/data/#{@name}" unless @url? + @board.classList.add @name @grid = document.createElement 'div' @grid.classList.add 'game-container' @board.appendChild @grid + @getFiles() + buttons_div = document.createElement 'div' buttons = {check: 'Check', reset: 'Reset', newgame: 'New game', help: '?'} @@ -23,6 +27,7 @@ class Nikoli.Game @board.querySelector('.check').addEventListener('click', @check.bind(this)) @board.querySelector('.reset').addEventListener('click', @reset.bind(this)) + @board.querySelector('.newgame').addEventListener('click', @newgame.bind(this)) check: -> errors = @errors() @@ -32,6 +37,28 @@ class Nikoli.Game else alert errors.map((el) -> el.message).join() + getFiles: -> + xmlhttp = new XMLHttpRequest() + xmlhttp.open("GET", "#{@url}.json") + + xmlhttp.addEventListener('load', (evt) => + @setFiles JSON.parse(evt.target.responseText)) + xmlhttp.send() + + setFiles: (files) -> + @files = files + @file = @files[0] + + @newgame() unless @game? + + newgame: -> + xmlhttp = new XMLHttpRequest() + xmlhttp.open("GET", "#{@url}/#{@file}.json") + + xmlhttp.addEventListener('load', (evt) => + @generate JSON.parse(evt.target.responseText)) + xmlhttp.send() + reset: -> @generate() diff --git a/views/hitori.coffee b/views/hitori.coffee index b475c07..2046400 100644 --- a/views/hitori.coffee +++ b/views/hitori.coffee @@ -1,6 +1,6 @@ class Nikoli.Hitori extends Nikoli.Game - constructor: (@board, @name = 'hitori') -> - super @board, @name + constructor: (@board, @name = 'hitori', @url = null) -> + super @board, @name, @url errors: -> solution = @toArray() diff --git a/views/hitori.slim b/views/hitori.slim index 784949a..883b25c 100644 --- a/views/hitori.slim +++ b/views/hitori.slim @@ -6,17 +6,5 @@ h1 Hitori script type="text/javascript" src="/hitori.js" javascript: document.addEventListener("DOMContentLoaded", function() { - var game = [ - [4,5,6,8,3,7,8,1], - [1,1,1,4,8,6,7,2], - [5,4,7,6,3,1,2,8], - [7,4,5,3,2,8,1,6], - [2,2,2,7,1,4,4,4], - [8,7,4,5,5,2,6,3], - [6,7,8,5,5,4,3,2], - [3,3,3,2,4,6,1,7] - ]; - hitori = new Nikoli.Hitori(document.getElementById('board')); - hitori.generate(game); }); diff --git a/views/nurikabe.slim b/views/nurikabe.slim index 86ba49e..12f9229 100644 --- a/views/nurikabe.slim +++ b/views/nurikabe.slim @@ -7,11 +7,4 @@ script type="text/javascript" src="/nurikabe.js" javascript: document.addEventListener("DOMContentLoaded", function() { nurikabe = new Nikoli.Nurikabe(document.getElementById('board')); - - xmlhttp = new XMLHttpRequest(); - xmlhttp.open("GET", "#{url('/data/nurikabe/5x5.json')}"); - - xmlhttp.addEventListener('load', function(evt) { - nurikabe.generate(JSON.parse(evt.target.responseText)) }) - xmlhttp.send(); }); diff --git a/views/sudoku.slim b/views/sudoku.slim index fc9b5e7..92b71c6 100644 --- a/views/sudoku.slim +++ b/views/sudoku.slim @@ -7,11 +7,4 @@ script type="text/javascript" src="/sudoku.js" javascript: document.addEventListener("DOMContentLoaded", function() { sudoku = new Nikoli.Sudoku(document.getElementById('board')); - - xmlhttp = new XMLHttpRequest(); - xmlhttp.open("GET", "#{url('/data/sudoku/gnome-sudoku_easy.json')}"); - - xmlhttp.addEventListener('load', function(evt) { - sudoku.generate(JSON.parse(evt.target.responseText)) }) - xmlhttp.send(); });