From 43247f40ea66a7ae00454b7e503fa79e4262ab07 Mon Sep 17 00:00:00 2001 From: Guillaume Dott Date: Mon, 8 Dec 2014 17:33:04 +0100 Subject: [PATCH] Move Nikoli.Stream class into another file --- nikoli.rb | 1 + views/application.coffee | 55 +++++++++++++++++++++++++++++++++++++ views/layout.slim | 1 + views/nurikabe.coffee | 58 ++-------------------------------------- 4 files changed, 59 insertions(+), 56 deletions(-) create mode 100644 views/application.coffee diff --git a/nikoli.rb b/nikoli.rb index 73cb521..8a35645 100755 --- a/nikoli.rb +++ b/nikoli.rb @@ -10,6 +10,7 @@ require 'sass' GAMES = %i{nurikabe sudoku} get('/application.css') { scss :application } +get('/application.js') { coffee :application } get('/') { slim :index, locals: {games: GAMES} } diff --git a/views/application.coffee b/views/application.coffee new file mode 100644 index 0000000..0f683ad --- /dev/null +++ b/views/application.coffee @@ -0,0 +1,55 @@ +window.Nikoli = Nikoli = {} + +class Nikoli.Stream + constructor: (@game) -> + @cells = [] + + calculate: (cell) -> + value = @game[cell.x][cell.y] + @cells = [] + @type = if value < 0 then 'black' else 'white' + + cell = {x: cell.x, y: cell.y, value: value} + cells_to_process = [cell] + + while cells_to_process.length > 0 + cell = cells_to_process.pop() + cells_to_process = cells_to_process.concat @process(cell) unless @include(cell) + + + @cells + + checkCell: (cell, value) -> + 0 <= cell.x < @game.length && 0 <= cell.y < @game[cell.x].length && + (value < 0 && @game[cell.x][cell.y] < 0 || value >= 0 && @game[cell.x][cell.y] >= 0) + + empty: -> + @cells.length == 0 + + getCell: (cell, value) -> + {x: cell.x, y: cell.y, value: @game[cell.x][cell.y]} if @checkCell(cell, value) + + include: (cell) -> + @cells.indexOf("#{cell.x};#{cell.y}") >= 0 + + length: -> + @cells.length + + process: (cell) -> + @cells.push("#{cell.x};#{cell.y}") + + x = cell.x + y = cell.y + value = cell.value + + cells_to_add = [] + tmp_cell = @getCell({x: x+1, y: y}, value) + cells_to_add.push tmp_cell if tmp_cell? + tmp_cell = @getCell({x: x-1, y: y}, value) + cells_to_add.push tmp_cell if tmp_cell? + tmp_cell = @getCell({x: x, y: y+1}, value) + cells_to_add.push tmp_cell if tmp_cell? + tmp_cell = @getCell({x: x, y: y-1}, value) + cells_to_add.push tmp_cell if tmp_cell? + + cells_to_add diff --git a/views/layout.slim b/views/layout.slim index 387fb39..b51492e 100644 --- a/views/layout.slim +++ b/views/layout.slim @@ -5,6 +5,7 @@ html meta charset="utf-8" meta name="viewport" content="initial-scale=1.0, user-scalable=yes" link rel="stylesheet" media="all" href="/application.css" + script type="text/javascript" src="/application.js" body div#main == yield diff --git a/views/nurikabe.coffee b/views/nurikabe.coffee index 48d903a..6800ea9 100644 --- a/views/nurikabe.coffee +++ b/views/nurikabe.coffee @@ -31,7 +31,7 @@ window.Nurikabe = class Nurikabe solution = @toArray() errors = [] processed_cells = [] - black_stream = new Stream(solution) + black_stream = new Nikoli.Stream(solution) white_walls = [] for i in [0...solution.length] @@ -48,7 +48,7 @@ window.Nurikabe = class Nurikabe if white_walls.some((wall) -> wall.include({x: i, y: j})) errors.push {row: i, column: j, message: 'Each wall must contain exactly one numbered cell.'} else - wall = new Stream(solution) + wall = new Nikoli.Stream(solution) wall.calculate({x: i, y: j}) if wall.length() != cell @@ -97,57 +97,3 @@ window.Nurikabe = class Nurikabe 0 else parseInt(cell.innerHTML) - -class Stream - constructor: (@game) -> - @cells = [] - - calculate: (cell) -> - value = @game[cell.x][cell.y] - @cells = [] - @type = if value < 0 then 'black' else 'white' - - cell = {x: cell.x, y: cell.y, value: value} - cells_to_process = [cell] - - while cells_to_process.length > 0 - cell = cells_to_process.pop() - cells_to_process = cells_to_process.concat @process(cell) unless @include(cell) - - - @cells - - checkCell: (cell, value) -> - 0 <= cell.x < @game.length && 0 <= cell.y < @game[cell.x].length && - (value < 0 && @game[cell.x][cell.y] < 0 || value >= 0 && @game[cell.x][cell.y] >= 0) - - empty: -> - @cells.length == 0 - - getCell: (cell, value) -> - {x: cell.x, y: cell.y, value: @game[cell.x][cell.y]} if @checkCell(cell, value) - - include: (cell) -> - @cells.indexOf("#{cell.x};#{cell.y}") >= 0 - - length: -> - @cells.length - - process: (cell) -> - @cells.push("#{cell.x};#{cell.y}") - - x = cell.x - y = cell.y - value = cell.value - - cells_to_add = [] - tmp_cell = @getCell({x: x+1, y: y}, value) - cells_to_add.push tmp_cell if tmp_cell? - tmp_cell = @getCell({x: x-1, y: y}, value) - cells_to_add.push tmp_cell if tmp_cell? - tmp_cell = @getCell({x: x, y: y+1}, value) - cells_to_add.push tmp_cell if tmp_cell? - tmp_cell = @getCell({x: x, y: y-1}, value) - cells_to_add.push tmp_cell if tmp_cell? - - cells_to_add