public function __construct($line, $column, $value, $status, $blockSize) { $this->line = $line; $this->column = $column; $this->value = $value; $authorizedValue = array(); $this->status = $status; $this->block = 0; $this->block += (int) ($this->line / $blockSize) * $blockSize; $this->block += (int) ($this->column / $blockSize); // For empty cell => initialize authorized values (with all existing values for sudoku's size) if ($value == null || $value == 0) { $this->authorizedValue = SudokuTool::buildDefaultAuthorizedValue($blockSize * $blockSize); } }
/** * Allows to check if one authorized value is defined on once line/column in cell's block. * In this case, we remove this value from cell defined in other blocks and on same line/column. * @param array $unresolvedBlockCellList Block cells used to find single value on once line/column in block (as SudokuCell[]). * @param SudokuGrid $sudoku Full sudoku grid containing cell to process. */ private function removeSingleValueFromBlock(array $unresolvedBlockCellList, SudokuGrid $sudoku) { foreach ($unresolvedBlockCellList as $cellToProcess) { // Browse all authorized values for cell to process foreach ($cellToProcess->getAuthorizedValue() as $value) { $uniqueLineValue = true; $uniqueColumnValue = true; // Check if value is unique inside block's line/column foreach ($unresolvedBlockCellList as $blockCell) { if (!$blockCell->isSameCell($cellToProcess) && in_array($value, $blockCell->getAuthorizedValue())) { if ($uniqueLineValue && $blockCell->getLine() != $cellToProcess->getLine()) { $uniqueLineValue = false; } if ($uniqueColumnValue && $blockCell->getColumn() != $cellToProcess->getColumn()) { $uniqueColumnValue = false; } } } // If value is unique in block's line => remove this value from other line's cell (in other blocks) if ($uniqueLineValue) { // Get all cells on same line that doesn't belongs to current cell's block $otherLineCellList = $sudoku->getLine($cellToProcess->getLine(), false); // SudokuCell[] SudokuTool::removeSudokuCellListFromList($unresolvedBlockCellList, $otherLineCellList); foreach ($otherLineCellList as $otherLineCell) { $otherLineCell->removeAuthorizedValue($value); } } // If value is unique in block's column => remove this value from other column's cell (in other blocks) if ($uniqueColumnValue) { // Get all cells on same line that doesn't belongs to current cell's block $otherColumnCellList = $sudoku->getColumn($cellToProcess->getColumn(), false); // SudokuCell[] SudokuTool::removeSudokuCellListFromList($unresolvedBlockCellList, $otherColumnCellList); foreach ($otherColumnCellList as $otherColumnCell) { $otherColumnCell->removeAuthorizedValue($value); } } } } }