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 = {}
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
constructor: (@game) ->
@cells = []

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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