Create Nikoli.Cell class

master
Guillaume Dott 2014-12-09 17:33:32 +01:00
parent d1fc8c7f58
commit cca0275be9
3 changed files with 37 additions and 50 deletions

View File

@ -1,4 +1,4 @@
window.Nikoli = Nikoli = {}
window.Nikoli = {}
class Nikoli.Game
constructor: (@board, @name, @url) ->
@ -62,56 +62,50 @@ class Nikoli.Game
reset: ->
@generate()
class Nikoli.Cell
constructor: (@x, @y, @game) ->
@value = @game[@x][@y] if @valid()
toString: -> "#{@x};#{@y}"
adjacentCells: ->
[
new Cell(@x + 1, @y, @game),
new Cell(@x - 1, @y, @game),
new Cell(@x, @y + 1, @game),
new Cell(@x, @y - 1, @game)
]
valid: (value) ->
0 <= @x < @game.length && 0 <= @y < @game[@x].length &&
(!value? || value < 0 && @game[@x][@y] < 0 || value >= 0 && @game[@x][@y] >= 0)
class Nikoli.Stream
constructor: (@game) ->
@cells = []
calculate: (cell) ->
value = @game[cell.x][cell.y]
@cells = []
@type = if value < 0 then 'black' else 'white'
@type = if cell.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
@cells.indexOf(cell.toString()) >= 0
length: ->
@cells.length
process: (cell) ->
@cells.push("#{cell.x};#{cell.y}")
@cells.push(cell.toString())
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
cell.adjacentCells().filter((adj_cell) -> adj_cell.valid(cell.value))

View File

@ -11,23 +11,16 @@ class Nikoli.Hitori extends Nikoli.Game
for i in [0...solution.length]
row = solution[i]
for j in [0...row.length]
cell = solution[i][j]
cell = new Nikoli.Cell(i, j, solution)
if cell >= 0
white_stream.calculate({x: i, y: j}) if white_stream.empty()
if cell.value >= 0
white_stream.calculate(cell) if white_stream.empty()
if !white_stream.include({x: i, y: j})
if !white_stream.include(cell)
errors.push {row: i, column: j, message: 'The stream must be continuous'}
# TODO check for duplicates in rows and columns
else
adjacent_cells = [
{x: i+1, y: j},
{x: i-1, y: j},
{x: i, y: j+1},
{x: i, y: j-1}
]
if adjacent_cells.some((el) ->
0 <= el.x < solution.length && 0 <= el.y < solution[el.x].length && solution[el.x][el.y] == -1)
if cell.adjacentCells().some((adj_cell) -> adj_cell.valid(-1))
errors.push {row: i, column: j, message: 'Adjacent filled-in cells'}
errors

View File

@ -5,28 +5,28 @@ class Nikoli.Nurikabe extends Nikoli.Game
errors: ->
solution = @toArray()
errors = []
processed_cells = []
black_stream = new Nikoli.Stream(solution)
white_walls = []
for i in [0...solution.length]
row = solution[i]
for j in [0...row.length]
cell = solution[i][j]
cell = new Nikoli.Cell(i, j, solution)
if cell < 0
if cell.value < 0
if black_stream.empty()
black_stream.calculate({x: i, y: j})
else if !black_stream.include({x: i, y: j})
black_stream.calculate(cell)
else if !black_stream.include(cell)
errors.push {row: i, column: j, message: 'The stream must be continuous'}
else if cell > 0
if white_walls.some((wall) -> wall.include({x: i, y: j}))
# TODO check for pools
else if cell.value > 0
if white_walls.some((wall) -> wall.include(cell))
errors.push {row: i, column: j, message: 'Each wall must contain exactly one numbered cell.'}
else
wall = new Nikoli.Stream(solution)
wall.calculate({x: i, y: j})
wall.calculate(cell)
if wall.length() != cell
if wall.length() != cell.value
errors.push {row: i, column: j, message: 'Each numbered cell is a wall cell, the number in it is the number of cells in that wall.'}
white_walls.push(wall)