Check for duplicates in rows and columns for Hitori

master
Guillaume Dott 2014-12-10 12:52:00 +01:00
parent 9c14be4f3c
commit 01b59ef59d
2 changed files with 15 additions and 1 deletions

View File

@ -87,6 +87,18 @@ class Nikoli.Cell
0 <= @x < @game.length && 0 <= @y < @game[@x].length && 0 <= @x < @game.length && 0 <= @y < @game[@x].length &&
(!value? || value < 0 && @game[@x][@y] < 0 || value >= 0 && @game[@x][@y] >= 0) (!value? || value < 0 && @game[@x][@y] < 0 || value >= 0 && @game[@x][@y] >= 0)
duplicatesIn: (array) ->
array.filter((cell) => cell == @value).length > 1
columnDuplicates: ->
column = []
column.push @game[i][@y] for i in [0...@game.length]
@duplicatesIn column
rowDuplicates: ->
@duplicatesIn @game[@x]
class Nikoli.Stream class Nikoli.Stream
constructor: (@game) -> constructor: (@game) ->
@cells = [] @cells = []

View File

@ -18,7 +18,9 @@ class Nikoli.Hitori extends Nikoli.Game
if !white_stream.include(cell) if !white_stream.include(cell)
errors.push {row: i, column: j, message: 'The stream must be continuous'} errors.push {row: i, column: j, message: 'The stream must be continuous'}
# TODO check for duplicates in rows and columns
if cell.rowDuplicates() || cell.columnDuplicates()
errors.push {row: i, column: j, message: 'The number appears more than once in the row or column.'}
else else
if cell.adjacentCells().some((adj_cell) -> adj_cell.valid(-1)) if cell.adjacentCells().some((adj_cell) -> adj_cell.valid(-1))
errors.push {row: i, column: j, message: 'Adjacent filled-in cells'} errors.push {row: i, column: j, message: 'Adjacent filled-in cells'}