Public/IsSubgridPlacementValid.ps1
<# .SYNOPSIS Determines if a number placement is valid .DESCRIPTION Based on a subgrid defined by row and column, determines if the number placement is valid. .PARAMETER SudokuGrid A 2D array representing the Sudoku grid .PARAMETER Row The row of the placement to attempt, this must be in the range 1-9 .PARAMETER Column The column of the placement to attempt, this must be in the range 1-9 .PARAMETER Number The number to be tried, must be in the range 1-9 .EXAMPLE IsSubGridValidPlacement -SudokuGrid $Grid -Row 1 -Column 1 -Number 2 .INPUTS Takes in a Sudoku grid, a row, a column, and a number .OUTPUTS Returns a boolean #> Function IsSubgridPlacementValid { param ( [parameter(Mandatory=$true)] [System.Object]$SudokuGrid, [parameter(Mandatory=$true)] [ValidateRange(1, 9)] [int32]$Row, [parameter(Mandatory=$true)] [ValidateRange(1, 9)] [int32]$Column, [parameter(Mandatory=$true)] [ValidateRange(1, 9)] [int32]$Number ) $CalcRow = $Row - 1 $CalcCol = $Column - 1 [int32]$SubgridRowStart = [Math]::Floor(($CalcRow / 3)) * 3 [int32]$SubgridColumnStart = [Math]::Floor(($CalcCol / 3)) * 3 for ($i = $SubgridRowStart; $i -lt $SubgridRowStart + 3; $i++) { for ($j = $SubgridColumnStart; $j -lt $SubgridColumnStart + 3; $j++) { if ($SudokuGrid[($i)][($j)] -eq $Number) { return $false } } } return $true } |