nikoli/views/hitori.coffee

61 lines
1.8 KiB
CoffeeScript
Raw Permalink Normal View History

2014-12-09 12:30:29 +01:00
class Nikoli.Hitori extends Nikoli.Game
constructor: (@board, @name = 'hitori', @url = null) ->
super @board, @name, @url
2014-12-08 17:48:22 +01:00
errors: ->
solution = @toArray()
errors = []
processed_cells = []
white_stream = new Nikoli.Stream(solution)
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.HitoriCell(i, j, solution)
2014-12-08 17:48:22 +01:00
2014-12-09 17:33:32 +01:00
if cell.value >= 0
white_stream.calculate(cell) if white_stream.empty()
2014-12-08 17:48:22 +01:00
2014-12-09 17:33:32 +01:00
if !white_stream.include(cell)
2014-12-08 17:48:22 +01:00
errors.push {row: i, column: j, message: 'The stream must be continuous'}
if cell.rowDuplicates() || cell.columnDuplicates()
errors.push {row: i, column: j, message: 'The number appears more than once in the row or column.'}
2014-12-08 17:48:22 +01:00
else
2014-12-09 17:33:32 +01:00
if cell.adjacentCells().some((adj_cell) -> adj_cell.valid(-1))
2014-12-08 17:48:22 +01:00
errors.push {row: i, column: j, message: 'Adjacent filled-in cells'}
errors
generate: (game, solution = false) ->
2014-12-12 12:18:22 +01:00
super game, solution, Nikoli.HitoriCell
2014-12-08 17:48:22 +01:00
for cell in board.querySelectorAll('.grid-cell')
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('black')
-1
else
parseInt(cell.innerHTML)
2014-12-12 12:18:22 +01:00
class Nikoli.HitoriCell extends Nikoli.Cell
create: (value) ->
cell = super
cell.innerHTML = value
cell