Esempio n. 1
0
        private static Position[] ReadFile(string fileName)
        {
            Position[] sudoku = new Position[9 * 9];
            using (StreamReader input = new StreamReader(new FileStream(fileName, FileMode.Open)))
            {
                string line;
                int lineCount = 0;
                while ((line = input.ReadLine()) != null)
                {
                    if (lineCount >= 9)
                    {
                        throw new Exception("More than 9 lines found in puzzle.");
                    }

                    if (line.Length != 9)
                    {
                        throw new Exception(String.Format("Line {0} is length {1}, expected 9.", lineCount, line.Length));
                    }

                    for (int i = 0; i < line.Length; ++i)
                    {
                        int num;
                        if (!int.TryParse(line[i].ToString(), out num))
                        {
                            throw new Exception(String.Format("Found non-integer in puzzle at line {0} pos {1}", lineCount, i));
                        }

                        sudoku[(lineCount * 9) + i] = new Position(num);
                    }

                    ++lineCount;
                }

                return sudoku;
            }
        }
Esempio n. 2
0
        private static bool IsSolved(Position[] sudoku)
        {
            foreach (Position p in sudoku)
            {
                if (!p.IsSolved)
                {
                    return false;
                }
            }

            return true;
        }
Esempio n. 3
0
        private static void SetColForPos(Position[] sudoku, int pos, int num)
        {
            int col = pos % 9;

            for (int i = 0; i < 9; ++i)
            {
                int candidate = (i * 9) + col;
                if (candidate != pos)
                {
                    sudoku[candidate].RemovePossible(num);
                }
            }
        }
Esempio n. 4
0
        private static void SetBlockForPos(Position[] sudoku, int pos, int num)
        {
            int startRow = RoundToThree(pos / 9);
            int startCol = RoundToThree(pos % 9);

            for (int row = startRow; row < startRow + 3; ++row)
            {
                for (int col = startCol; col < startCol + 3; ++col)
                {
                    int candidate = (row * 9) + col;
                    if (candidate != pos)
                    {
                        sudoku[candidate].RemovePossible(num);
                    }
                }
            }
        }
Esempio n. 5
0
        private static void SetRowForPos(Position[] sudoku, int pos, int num)
        {
            int row = pos / 9;

            for (int i = 0; i < 9; ++i)
            {
                int candidate = (row * 9) + i;
                if (candidate != pos)
                {
                    sudoku[candidate].RemovePossible(num);
                }
            }
        }