Create Nikoli.Game class

master
Guillaume Dott 2014-12-09 12:30:29 +01:00
parent 32de80fd6d
commit 123f5df76b
7 changed files with 47 additions and 100 deletions

View File

@ -1,5 +1,40 @@
window.Nikoli = Nikoli = {} window.Nikoli = Nikoli = {}
class Nikoli.Game
constructor: (@board, @name) ->
@name = 'nikoli' unless @name?
@board.classList.add @name
@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()
reset: ->
@generate()
class Nikoli.Stream class Nikoli.Stream
constructor: (@game) -> constructor: (@game) ->
@cells = [] @cells = []

View File

@ -1,33 +1,6 @@
window.Hitori = class Nurikabe class Nikoli.Hitori extends Nikoli.Game
constructor: (@board) -> constructor: (@board, @name = 'hitori') ->
@board.classList.add 'hitori' super @board, @name
@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: -> errors: ->
solution = @toArray() solution = @toArray()
@ -72,9 +45,6 @@ window.Hitori = class Nurikabe
return return
reset: ->
@generate()
toggle: (cell) -> toggle: (cell) ->
if cell.classList.contains 'black' if cell.classList.contains 'black'
cell.classList.remove 'black' cell.classList.remove 'black'

View File

@ -17,6 +17,6 @@ javascript:
[3,3,3,2,4,6,1,7] [3,3,3,2,4,6,1,7]
]; ];
hitori = new Hitori(document.getElementById('board')); hitori = new Nikoli.Hitori(document.getElementById('board'));
hitori.generate(game); hitori.generate(game);
}); });

View File

@ -1,31 +1,6 @@
window.Nurikabe = class Nurikabe class Nikoli.Nurikabe extends Nikoli.Game
constructor: (@board) -> constructor: (@board, @name = 'nurikabe') ->
@grid = document.createElement 'div' super @board, @name
@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: -> errors: ->
solution = @toArray() solution = @toArray()
@ -75,9 +50,6 @@ window.Nurikabe = class Nurikabe
return return
reset: ->
@generate()
toggle: (cell) -> toggle: (cell) ->
if cell.classList.contains 'black' if cell.classList.contains 'black'
cell.classList.remove 'black' cell.classList.remove 'black'

View File

@ -6,7 +6,7 @@ h1 Nurikabe
script type="text/javascript" src="/nurikabe.js" script type="text/javascript" src="/nurikabe.js"
javascript: javascript:
document.addEventListener("DOMContentLoaded", function() { document.addEventListener("DOMContentLoaded", function() {
nurikabe = new Nurikabe(document.getElementById('board')); nurikabe = new Nikoli.Nurikabe(document.getElementById('board'));
xmlhttp = new XMLHttpRequest(); xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "#{url('/data/nurikabe/5x5.json')}"); xmlhttp.open("GET", "#{url('/data/nurikabe/5x5.json')}");

View File

@ -1,33 +1,6 @@
window.Sudoku = class Sudoku class Nikoli.Sudoku extends Nikoli.Game
constructor: (@board) -> constructor: (@board, @name = 'sudoku') ->
@board.classList.add 'sudoku' super @board, @name
@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: -> errors: ->
solution = @toArray() solution = @toArray()
@ -65,9 +38,6 @@ window.Sudoku = class Sudoku
return return
reset: ->
@generate()
toggle: (cell) -> toggle: (cell) ->
if cell.classList.contains 'black' if cell.classList.contains 'black'
cell.classList.remove 'black' cell.classList.remove 'black'

View File

@ -6,7 +6,7 @@ h1 Sudoku
script type="text/javascript" src="/sudoku.js" script type="text/javascript" src="/sudoku.js"
javascript: javascript:
document.addEventListener("DOMContentLoaded", function() { document.addEventListener("DOMContentLoaded", function() {
sudoku = new Sudoku(document.getElementById('board')); sudoku = new Nikoli.Sudoku(document.getElementById('board'));
xmlhttp = new XMLHttpRequest(); xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "#{url('/data/sudoku/gnome-sudoku_easy.json')}"); xmlhttp.open("GET", "#{url('/data/sudoku/gnome-sudoku_easy.json')}");