diff --git a/views/application.coffee b/views/application.coffee index 7374176..c800384 100644 --- a/views/application.coffee +++ b/views/application.coffee @@ -87,6 +87,18 @@ class Nikoli.Cell 0 <= @x < @game.length && 0 <= @y < @game[@x].length && (!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 constructor: (@game) -> @cells = [] diff --git a/views/hitori.coffee b/views/hitori.coffee index 82f558c..08aca82 100644 --- a/views/hitori.coffee +++ b/views/hitori.coffee @@ -18,7 +18,9 @@ class Nikoli.Hitori extends Nikoli.Game 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 + + if cell.rowDuplicates() || cell.columnDuplicates() + errors.push {row: i, column: j, message: 'The number appears more than once in the row or column.'} else if cell.adjacentCells().some((adj_cell) -> adj_cell.valid(-1)) errors.push {row: i, column: j, message: 'Adjacent filled-in cells'}