Add functions to extract rows, colums and blocks

This commit is contained in:
Alberto Venturini 2019-10-28 21:33:45 +02:00
parent 895a9dd01b
commit 61bbd00f6d
2 changed files with 64 additions and 12 deletions

View file

@ -1,27 +1,50 @@
module Sudoku
class BoardException < Exception
end
class Board
alias Value = Nil | Int8
enum State
Valid
Invalid
end
alias Value = Nil | Int32
@grid : Array(Array(Value))
getter grid
def initialize(@width : Int8, @height : Int8)
@grid = Array(Value).new(@width * @height, nil)
def initialize(@size : Int32, @block_size : Int32)
raise BoardException.new() unless @size.divisible_by?(@block_size)
@grid = Array(Array(Value)).new(@size) { Array(Value).new(@size, nil) }
end
def put(value : Value, x, y)
@grid[y * @width + x] = value
@grid[y][x] = value
end
def get(x, y)
@grid[y * @width + x]
@grid[y][x]
end
def check_valid(value : Value, x, y)
end
def extract_row(y)
@grid[y]
end
def extract_column(x)
(0..@size - 1).map { |i| get(x, i) }
end
def extract_block(x, y)
block_start_x = x - (x % @block_size)
block_start_y = y - (y % @block_size)
(block_start_y..block_start_y + @block_size - 1).map { |j|
(block_start_x..block_start_x + @block_size - 1).map { |i|
get(i, j)
}
}
end
end