From 123f5df76b2c0b8e8ca2f6ab50e23aa2252ad2e5 Mon Sep 17 00:00:00 2001 From: Guillaume Dott Date: Tue, 9 Dec 2014 12:30:29 +0100 Subject: [PATCH] Create Nikoli.Game class --- views/application.coffee | 35 +++++++++++++++++++++++++++++++++++ views/hitori.coffee | 36 +++--------------------------------- views/hitori.slim | 2 +- views/nurikabe.coffee | 34 +++------------------------------- views/nurikabe.slim | 2 +- views/sudoku.coffee | 36 +++--------------------------------- views/sudoku.slim | 2 +- 7 files changed, 47 insertions(+), 100 deletions(-) diff --git a/views/application.coffee b/views/application.coffee index 0f683ad..6c85497 100644 --- a/views/application.coffee +++ b/views/application.coffee @@ -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 = [] diff --git a/views/hitori.coffee b/views/hitori.coffee index 645b099..b475c07 100644 --- a/views/hitori.coffee +++ b/views/hitori.coffee @@ -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' diff --git a/views/hitori.slim b/views/hitori.slim index 0c4f3af..784949a 100644 --- a/views/hitori.slim +++ b/views/hitori.slim @@ -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); }); diff --git a/views/nurikabe.coffee b/views/nurikabe.coffee index 6800ea9..999ce27 100644 --- a/views/nurikabe.coffee +++ b/views/nurikabe.coffee @@ -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' diff --git a/views/nurikabe.slim b/views/nurikabe.slim index 24e709f..86ba49e 100644 --- a/views/nurikabe.slim +++ b/views/nurikabe.slim @@ -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')}"); diff --git a/views/sudoku.coffee b/views/sudoku.coffee index b82f26c..36a26be 100644 --- a/views/sudoku.coffee +++ b/views/sudoku.coffee @@ -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' diff --git a/views/sudoku.slim b/views/sudoku.slim index 3448bb4..fc9b5e7 100644 --- a/views/sudoku.slim +++ b/views/sudoku.slim @@ -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')}");