From 01b59ef59db839b33d26a9b905cf575cc5febd99 Mon Sep 17 00:00:00 2001 From: Guillaume Dott Date: Wed, 10 Dec 2014 12:52:00 +0100 Subject: [PATCH] Check for duplicates in rows and columns for Hitori --- views/application.coffee | 12 ++++++++++++ views/hitori.coffee | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) 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'}