Exemplo n.º 1
0
        private bool SearchPossibleValues(SudokuTableItem item)
        {
            if (item.HasAValue)
            {
                return(true);
            }

            RemoveUnpossibleValues(item);

            //check result
            if (item.PossibleValues.Count == 1)
            {
                item.Value = item.PossibleValues.First();
                item.PossibleValues.Clear();
                return(true);
            }

            PossibleValueCheck(item);
            if (item.HasAValue)// si le possible value check a donné quelque chose
            {
                return(true);
            }

            if (item.PossibleValues.Count <= 0)
            {
                throw new Exception("Not valid sudoku");
            }

            return(false);
        }
Exemplo n.º 2
0
        private void RemoveUnpossibleValues(SudokuTableItem item)
        {
//check in block
            var subBlock = SubBlock(item);

            for (int i = 0; i < SubBlockSize; i++)
            {
                for (int j = 0; j < SubBlockSize; j++)
                {
                    if (subBlock[i][j].HasAValue)
                    {
                        item.PossibleValues.Remove(subBlock[i][j].Value);
                    }
                }
            }

            //on parcours la ligne et la collone
            for (int i = 0; i < Table.Lenght; i++)
            {
                if (Table[i, item.PositionX].HasAValue)
                {
                    item.PossibleValues.Remove(Table[i, item.PositionX].Value);
                }
                if (Table[item.PositionY, i].HasAValue)
                {
                    item.PossibleValues.Remove(Table[item.PositionY, i].Value);
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// initialising the array
 /// </summary>
 private void InitTableArray()
 {
     SudokuTableItems = new SudokuTableItem[TableHeight][];
     for (int i = 0; i < TableHeight; i++)
     {
         SudokuTableItems[i] = new SudokuTableItem[TableWidth];
     }
 }
Exemplo n.º 4
0
        private SudokuTableItem[][] SubBlock(SudokuTableItem item)
        {
            int blockX = (int)Math.Floor(item.PositionX / (double)SubBlockSize);
            int blockY = (int)Math.Floor(item.PositionY / (double)SubBlockSize);

            var result = new SudokuTableItem[SubBlockSize][];

            for (int y = 0; y < SubBlockSize; y++)
            {
                result[y] = new SudokuTableItem[SubBlockSize];
                for (int x = 0; x < SubBlockSize; x++)
                {
                    result[y][x] = Table[blockY * SubBlockSize + y, blockX *SubBlockSize + x];
                }
            }
            return(result);
        }
Exemplo n.º 5
0
        private void PossibleValueCheck(SudokuTableItem item)
        {
            var subBlock = SubBlock(item);

            foreach (var possibleValue in item.PossibleValues)
            {
                bool valuePossibleX     = true;
                bool valuePossibleY     = true;
                bool valuePossibleBlock = true;

                //line or column check
                for (int i = 0; i < Table.Lenght; i++)
                {
                    if (item.PositionY != i &&
                        Table[i, item.PositionX].PossibleValues.Contains(possibleValue))
                    {
                        valuePossibleY = false;
                    }
                    if (item.PositionX != i &&
                        Table[item.PositionY, i].PossibleValues.Contains(possibleValue))
                    {
                        valuePossibleX = false;
                    }
                }

                //check in block
                for (int i = 0; i < SubBlockSize; i++)
                {
                    for (int j = 0; j < SubBlockSize; j++)
                    {
                        if (!(item.PositionY == i && item.PositionX == j) &&
                            subBlock[i][j].PossibleValues.Contains(possibleValue))
                        {
                            valuePossibleBlock = false;
                        }
                    }
                }

                if (valuePossibleX || valuePossibleY || valuePossibleBlock)
                {
                    item.Value = possibleValue;
                    item.PossibleValues.Clear();
                    return;
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// fill the array with values
        /// </summary>
        private void GenerateSudokuTableFromString()
        {
            int x = 0, y = 0;
            var lines = _rawFile.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            if (lines.Length != TableHeight)
            {
                throw new Exception("To many lines or not enough lines");
            }

            foreach (var line in lines)
            {
                var values = line.Split(' ');
                if (values.Length != TableWidth)
                {
                    throw new Exception($"To many values or not enough values on line{(x + 1)}");
                }
                x = 0;
                foreach (var value in values)
                {
                    int result = 0;
                    if (int.TryParse(value, out result))
                    {
                        if (result >= 0 && result <= TableWidth)//0 if empty
                        {
                            SudokuTableItems[y][x] = new SudokuTableItem(result, x, y);
                        }
                        else
                        {
                            throw new Exception($"Not valid number on line{(x + 1)} value:{(y + 1)}");
                        }
                    }
                    else
                    {
                        throw new Exception($"Not valid value on line{(x + 1)} value:{(y + 1)}");
                    }
                    x++;
                }
                y++;
            }
        }