2014-12-09 12:30:29 +01:00
|
|
|
class Nikoli.Nurikabe extends Nikoli.Game
|
|
|
|
constructor: (@board, @name = 'nurikabe') ->
|
|
|
|
super @board, @name
|
2014-12-05 12:38:43 +01:00
|
|
|
|
|
|
|
errors: ->
|
|
|
|
solution = @toArray()
|
|
|
|
errors = []
|
2014-12-08 17:33:04 +01:00
|
|
|
black_stream = new Nikoli.Stream(solution)
|
2014-12-05 12:38:43 +01:00
|
|
|
white_walls = []
|
|
|
|
|
|
|
|
for i in [0...solution.length]
|
|
|
|
row = solution[i]
|
|
|
|
for j in [0...row.length]
|
2014-12-12 12:18:22 +01:00
|
|
|
cell = new Nikoli.NurikabeCell(i, j, solution)
|
2014-12-05 12:38:43 +01:00
|
|
|
|
2014-12-09 17:33:32 +01:00
|
|
|
if cell.value < 0
|
2014-12-05 12:38:43 +01:00
|
|
|
if black_stream.empty()
|
2014-12-09 17:33:32 +01:00
|
|
|
black_stream.calculate(cell)
|
|
|
|
else if !black_stream.include(cell)
|
2014-12-05 12:38:43 +01:00
|
|
|
errors.push {row: i, column: j, message: 'The stream must be continuous'}
|
2014-12-09 17:53:23 +01:00
|
|
|
|
|
|
|
if cell.isPool()
|
|
|
|
errors.push {row: i, column: j, message: 'There must be no pools.'}
|
2014-12-09 17:33:32 +01:00
|
|
|
else if cell.value > 0
|
|
|
|
if white_walls.some((wall) -> wall.include(cell))
|
2014-12-05 12:38:43 +01:00
|
|
|
errors.push {row: i, column: j, message: 'Each wall must contain exactly one numbered cell.'}
|
|
|
|
else
|
2014-12-08 17:33:04 +01:00
|
|
|
wall = new Nikoli.Stream(solution)
|
2014-12-09 17:33:32 +01:00
|
|
|
wall.calculate(cell)
|
2014-12-05 12:38:43 +01:00
|
|
|
|
2014-12-09 17:33:32 +01:00
|
|
|
if wall.length() != cell.value
|
2014-12-05 12:38:43 +01:00
|
|
|
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)
|
|
|
|
|
|
|
|
errors
|
|
|
|
|
|
|
|
generate: (game, solution = false) ->
|
2014-12-12 12:18:22 +01:00
|
|
|
super game, solution, Nikoli.NurikabeCell
|
2014-12-05 12:38:43 +01:00
|
|
|
|
|
|
|
for cell in board.querySelectorAll('.empty')
|
|
|
|
cell.addEventListener 'click', ((evenment) => @toggle evenment.target), false
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
toggle: (cell) ->
|
|
|
|
if cell.classList.contains 'black'
|
|
|
|
cell.classList.remove 'black'
|
|
|
|
cell.classList.add 'white'
|
|
|
|
else if cell.classList.contains 'white'
|
|
|
|
cell.classList.remove 'white'
|
|
|
|
else
|
|
|
|
cell.classList.add 'black'
|
|
|
|
|
|
|
|
toArray: ->
|
|
|
|
[].map.call @grid.querySelectorAll('.grid-row'), (row) ->
|
|
|
|
[].map.call row.querySelectorAll('.grid-cell'), (cell) ->
|
|
|
|
if cell.classList.contains('empty')
|
|
|
|
if cell.classList.contains('black')
|
|
|
|
-1
|
|
|
|
else
|
|
|
|
0
|
|
|
|
else
|
|
|
|
parseInt(cell.innerHTML)
|
2014-12-12 12:18:22 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
|