From 9f8fdbb025e6df636047d389c29309a6a116f302 Mon Sep 17 00:00:00 2001 From: Guillaume Dott Date: Fri, 12 Dec 2014 12:18:22 +0100 Subject: [PATCH] Use Cell objects to generate grid --- views/akari.coffee | 40 ++++++++++++++++++++++------------------ views/application.coffee | 40 ++++++++++++++++++++++++++++++++-------- views/hitori.coffee | 16 +++++++++------- views/nurikabe.coffee | 34 +++++++++++++++++++++++----------- views/sudoku.coffee | 26 ++++++++++++++------------ 5 files changed, 100 insertions(+), 56 deletions(-) diff --git a/views/akari.coffee b/views/akari.coffee index a45d14b..2782a60 100644 --- a/views/akari.coffee +++ b/views/akari.coffee @@ -9,7 +9,7 @@ class Nikoli.Akari extends Nikoli.Game for i in [0...solution.length] row = solution[i] for j in [0...row.length] - cell = new Nikoli.Cell(i, j, solution) + cell = new Nikoli.AkariCell(i, j, solution) if cell.value == -5 errors.push {row: i, column: j, message: 'The light is illuminated by another one'} @@ -23,23 +23,7 @@ class Nikoli.Akari extends Nikoli.Game errors generate: (game, solution = false) -> - @game = game if game? - @grid.innerHTML = @game.map((row, i) -> - '
' + row.map((cell, j) -> - data = "data-row=\"#{i}\" data-column=\"#{j}\"" - if cell <= -2 - if solution - color_class = if cell == -3 - 'black' - else if cell == -4 - 'light' - else if cell == -5 - 'black light' - "
 
" - else - "
#{if cell >= 0 then cell else ' '}
" - ).join('') + '
' - ).join('') + super game, solution, Nikoli.AkariCell for cell in board.querySelectorAll('.empty') cell.addEventListener 'click', ((evenment) => @toggle evenment.target), false @@ -79,6 +63,26 @@ class Nikoli.Akari extends Nikoli.Game if isNaN(value) then -1 else value class Nikoli.AkariCell extends Nikoli.Cell + create: (value, solution = false) -> + cell = super + + if value >= -1 + cell.classList.add 'white' + cell.innerHTML = value if value >= 0 + else + cell.classList.add 'empty' + + if solution + if value == -3 + cell.classList.add 'black' + else if value == -4 + cell.classList.add 'light' + else if value == -5 + cell.classList.add 'black' + cell.classList.add 'light' + + cell + isIlluminated: -> @lightLeft() || @lightRight() || @lightUp() || @lightDown() diff --git a/views/application.coffee b/views/application.coffee index 97019d9..d1c80cf 100644 --- a/views/application.coffee +++ b/views/application.coffee @@ -43,6 +43,19 @@ class Nikoli.Game else alert errors.map((el) -> el.message).join() + generate: (game, solution = false, cell_class = Nikoli.Cell) -> + @game = game if game? + + @grid.innerHTML = '' + @game.forEach((row, i) => + row_elem = new Nikoli.Row().create() + row.forEach((cell, j) -> + row_elem.appendChild new cell_class(i, j).create(cell)) + + @grid.appendChild row_elem) + + return + getFiles: -> xmlhttp = new XMLHttpRequest() xmlhttp.open("GET", "#{@url}.json") @@ -70,9 +83,27 @@ class Nikoli.Game reset: -> @generate() +class Nikoli.Row + create: -> + row = document.createElement 'div' + row.classList.add 'grid-row' + + row + class Nikoli.Cell constructor: (@x, @y, @game) -> - @value = @game[@x][@y] if @valid() + @value = @game[@x][@y] if @game? && @valid() + + create: (value) -> + cell = document.createElement 'div' + cell.dataset.row = @x + cell.dataset.column = @y + + cell.classList.add 'grid-cell' + + cell.innerHTML = ' ' + + cell toString: -> "#{@x};#{@y}" @@ -91,13 +122,6 @@ class Nikoli.Cell new Cell(@x, @y - 1, @game) ] - isPool: -> - [ - new Cell(@x, @y + 1, @game), - new Cell(@x + 1, @y, @game), - new Cell(@x + 1, @y + 1, @game), - ].every (cell) => cell.valid(@value) - valid: (value) -> 0 <= @x < @game.length && 0 <= @y < @game[@x].length && (!value? || value < 0 && @game[@x][@y] < 0 || value >= 0 && @game[@x][@y] >= 0) diff --git a/views/hitori.coffee b/views/hitori.coffee index 08aca82..0ffe81d 100644 --- a/views/hitori.coffee +++ b/views/hitori.coffee @@ -11,7 +11,7 @@ class Nikoli.Hitori extends Nikoli.Game for i in [0...solution.length] row = solution[i] for j in [0...row.length] - cell = new Nikoli.Cell(i, j, solution) + cell = new Nikoli.HitoriCell(i, j, solution) if cell.value >= 0 white_stream.calculate(cell) if white_stream.empty() @@ -28,12 +28,7 @@ class Nikoli.Hitori extends Nikoli.Game errors generate: (game, solution = false) -> - @game = game if game? - @grid.innerHTML = @game.map((row) -> - '
' + row.map((cell) -> - "
#{cell}
" - ).join('') + '
' - ).join('') + super game, solution, Nikoli.HitoriCell for cell in board.querySelectorAll('.grid-cell') cell.addEventListener 'click', ((evenment) => @toggle evenment.target), false @@ -56,3 +51,10 @@ class Nikoli.Hitori extends Nikoli.Game -1 else parseInt(cell.innerHTML) + +class Nikoli.HitoriCell extends Nikoli.Cell + create: (value) -> + cell = super + cell.innerHTML = value + + cell diff --git a/views/nurikabe.coffee b/views/nurikabe.coffee index 257da94..b7115cb 100644 --- a/views/nurikabe.coffee +++ b/views/nurikabe.coffee @@ -11,7 +11,7 @@ class Nikoli.Nurikabe extends Nikoli.Game for i in [0...solution.length] row = solution[i] for j in [0...row.length] - cell = new Nikoli.Cell(i, j, solution) + cell = new Nikoli.NurikabeCell(i, j, solution) if cell.value < 0 if black_stream.empty() @@ -36,16 +36,7 @@ class Nikoli.Nurikabe extends Nikoli.Game errors generate: (game, solution = false) -> - @game = game if game? - @grid.innerHTML = @game.map((row) -> - '
' + row.map((cell) -> - if cell <= 0 - color_class = 'black' if solution && cell == -1 - "
 
" - else - "
#{cell}
" - ).join('') + '
' - ).join('') + super game, solution, Nikoli.NurikabeCell for cell in board.querySelectorAll('.empty') cell.addEventListener 'click', ((evenment) => @toggle evenment.target), false @@ -71,3 +62,24 @@ class Nikoli.Nurikabe extends Nikoli.Game 0 else parseInt(cell.innerHTML) + +class Nikoli.NurikabeCell extends Nikoli.Cell + create: (value, solution = false) -> + cell = super + + if value <= 0 + cell.classList.add 'empty' + cell.classList.add 'black' if solution && value == -1 + else + cell.classList.add 'white' + cell.innerHTML = value + + cell + + isPool: -> + [ + new NurikabeCell(@x, @y + 1, @game), + new NurikabeCell(@x + 1, @y, @game), + new NurikabeCell(@x + 1, @y + 1, @game), + ].every (cell) => cell.valid(@value) + diff --git a/views/sudoku.coffee b/views/sudoku.coffee index 2400f13..0ad565e 100644 --- a/views/sudoku.coffee +++ b/views/sudoku.coffee @@ -9,7 +9,7 @@ class Nikoli.Sudoku extends Nikoli.Game for i in [0...solution.length] row = solution[i] for j in [0...row.length] - cell = new Nikoli.Cell(i, j, solution) + cell = new Nikoli.SudokuCell(i, j, solution) if cell.value == 0 errors.push {row: i, column: j, message: 'The cell has no value.'} @@ -19,17 +19,7 @@ class Nikoli.Sudoku extends Nikoli.Game errors generate: (game, solution = false) -> - @game = game if game? - @grid.innerHTML = @game.map((row) -> - '
' + row.map((cell) -> - if cell <= 0 - "
" - else - "
#{cell}
" - ).join('') + '
' - ).join('') - - return + super game, solution, Nikoli.SudokuCell toggle: (cell) -> if cell.classList.contains 'black' @@ -51,3 +41,15 @@ class Nikoli.Sudoku extends Nikoli.Game if solution then -value else value else parseInt(cell.innerHTML) + +class Nikoli.SudokuCell extends Nikoli.Cell + create: (value) -> + cell = super + if value <= 0 + cell.classList.add 'empty' + cell.innerHTML = "" + else + cell.classList.add 'white' + cell.innerHTML = value + + cell