Get available files and new games with Ajax
parent
562451fa5d
commit
156b052b63
|
@ -1,14 +1,18 @@
|
||||||
window.Nikoli = Nikoli = {}
|
window.Nikoli = Nikoli = {}
|
||||||
|
|
||||||
class Nikoli.Game
|
class Nikoli.Game
|
||||||
constructor: (@board, @name) ->
|
constructor: (@board, @name, @url) ->
|
||||||
@name = 'nikoli' unless @name?
|
@name = 'nikoli' unless @name?
|
||||||
|
@url = "/data/#{@name}" unless @url?
|
||||||
|
|
||||||
@board.classList.add @name
|
@board.classList.add @name
|
||||||
|
|
||||||
@grid = document.createElement 'div'
|
@grid = document.createElement 'div'
|
||||||
@grid.classList.add 'game-container'
|
@grid.classList.add 'game-container'
|
||||||
@board.appendChild @grid
|
@board.appendChild @grid
|
||||||
|
|
||||||
|
@getFiles()
|
||||||
|
|
||||||
buttons_div = document.createElement 'div'
|
buttons_div = document.createElement 'div'
|
||||||
buttons = {check: 'Check', reset: 'Reset', newgame: 'New game', help: '?'}
|
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('.check').addEventListener('click', @check.bind(this))
|
||||||
@board.querySelector('.reset').addEventListener('click', @reset.bind(this))
|
@board.querySelector('.reset').addEventListener('click', @reset.bind(this))
|
||||||
|
@board.querySelector('.newgame').addEventListener('click', @newgame.bind(this))
|
||||||
|
|
||||||
check: ->
|
check: ->
|
||||||
errors = @errors()
|
errors = @errors()
|
||||||
|
@ -32,6 +37,28 @@ class Nikoli.Game
|
||||||
else
|
else
|
||||||
alert errors.map((el) -> el.message).join()
|
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: ->
|
reset: ->
|
||||||
@generate()
|
@generate()
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
class Nikoli.Hitori extends Nikoli.Game
|
class Nikoli.Hitori extends Nikoli.Game
|
||||||
constructor: (@board, @name = 'hitori') ->
|
constructor: (@board, @name = 'hitori', @url = null) ->
|
||||||
super @board, @name
|
super @board, @name, @url
|
||||||
|
|
||||||
errors: ->
|
errors: ->
|
||||||
solution = @toArray()
|
solution = @toArray()
|
||||||
|
|
|
@ -6,17 +6,5 @@ h1 Hitori
|
||||||
script type="text/javascript" src="/hitori.js"
|
script type="text/javascript" src="/hitori.js"
|
||||||
javascript:
|
javascript:
|
||||||
document.addEventListener("DOMContentLoaded", function() {
|
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 = new Nikoli.Hitori(document.getElementById('board'));
|
||||||
hitori.generate(game);
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -7,11 +7,4 @@ script type="text/javascript" src="/nurikabe.js"
|
||||||
javascript:
|
javascript:
|
||||||
document.addEventListener("DOMContentLoaded", function() {
|
document.addEventListener("DOMContentLoaded", function() {
|
||||||
nurikabe = new Nikoli.Nurikabe(document.getElementById('board'));
|
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();
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -7,11 +7,4 @@ script type="text/javascript" src="/sudoku.js"
|
||||||
javascript:
|
javascript:
|
||||||
document.addEventListener("DOMContentLoaded", function() {
|
document.addEventListener("DOMContentLoaded", function() {
|
||||||
sudoku = new Nikoli.Sudoku(document.getElementById('board'));
|
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();
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue