Beispiel #1
0
            private Result EliminateValuesSingle(int value)
            {
                Result result = Result.NoChange;
                //Iterate over column and find if this value is present only in one cell then set value for that cell.
                Cell uniqueCell = null;

                for (int x = 0, y = Y; x < Size; x++)
                {
                    Cell currCell = AllCells[x][y];
                    if (currCell.PossibleValues.Contains(value))
                    {
                        if (uniqueCell != null)
                        {
                            uniqueCell = null;
                            break;
                        }

                        uniqueCell = currCell;
                    }
                }
                if (uniqueCell != null)
                {
                    result = uniqueCell.SetCellValue(value);
                }
                if (result == Result.Failed)
                {
                    return(Result.Failed);
                }

                //Iterate over rows
                uniqueCell = null;
                for (int x = X, y = 0; y < Size; y++)
                {
                    Cell currCell = AllCells[x][y];
                    if (currCell.PossibleValues.Contains(value))
                    {
                        if (uniqueCell != null)
                        {
                            uniqueCell = null;
                            break;
                        }

                        uniqueCell = currCell;
                    }
                }
                if (uniqueCell != null)
                {
                    result = uniqueCell.SetCellValue(value);
                }
                return(result);
            }
Beispiel #2
0
        private static Result ApplySimpleRules(Lane lane)
        {
            int clue = lane.Clue;
            int size = lane.Cells.Count;

            if (clue == 0)
            {
                return(Result.NoChange);
            }
            Result result = Result.NoChange;

            if (clue == 1)
            {
                return(lane.Cells[0].SetCellValue(lane.Cells[0].PossibleValues.Max()));
            }

            if (clue == size)
            {
                for (int i = 0; i < size; i++)
                {
                    Cell cell = lane.Cells.ElementAtOrDefault(i);
                    if (cell != null)
                    {
                        result = cell.SetCellValue(cell.PossibleValues.Min()) | result;
                        if (Result.Failed == (result & Result.Failed))
                        {
                            return(Result.Failed);
                        }
                    }
                }

                return(result);
            }

            List <int> possibleValuesInLane = lane.Cells.SelectMany(c => c.PossibleValues).Distinct().ToList();

            if (possibleValuesInLane.Count != size)
            {
                return(Result.Failed);
            }
            possibleValuesInLane.Sort();

            if (clue == 2 && size > 1)
            {
                result = lane.Cells[1].RemoveValue(possibleValuesInLane[size - 2]);
                if (Result.Failed == result)
                {
                    return(Result.Failed);
                }
            }

            for (int indexToBeRemoved = size; indexToBeRemoved > size - clue + 1; indexToBeRemoved--)
            {
                for (int i = 0; i < clue + indexToBeRemoved - size - 1; i++)
                {
                    Cell cell = lane.Cells.ElementAtOrDefault(i);
                    if (cell != null)
                    {
                        int cellCount = lane.Cells.Count;
                        result = cell.RemoveValue(possibleValuesInLane[indexToBeRemoved - 1]) | result;
                        if (Result.Failed == (result & Result.Failed))
                        {
                            return(Result.Failed);
                        }
                        if (cellCount != lane.Cells.Count)
                        {
                            return(result);
                        }
                    }
                }
            }

            return(result);
        }