Add valid? method

This commit is contained in:
Alberto Venturini 2019-10-29 21:42:13 +02:00
parent 61bbd00f6d
commit 5b01700650
2 changed files with 19 additions and 2 deletions

View file

@ -42,4 +42,15 @@ describe Sudoku::Board do
board.extract_block(3, 1).should eq [[42, 43], [44, 45]]
end
it "validates cells correctly" do
board = Sudoku::Board.new(9, 3)
board.put(1, 0, 0)
board.put(2, 0, 1)
board.put(3, 1, 0)
board.valid?(4, 0, 2).should be_true
board.valid?(4, 2, 0).should be_true
board.valid?(3, 0, 2).should be_false
board.valid?(3, 2, 0).should be_false
end
end

View file

@ -24,8 +24,14 @@ module Sudoku
@grid[y][x]
end
def check_valid(value : Value, x, y)
def valid?(value : Value, x, y)
if value == nil
true
else
extract_row(y).all? { |i| i != value } &&
extract_column(x).all? { |i| i != value } &&
extract_block(x, y).all? { |i| i.all? { |j| j != value } }
end
end
def extract_row(y)