예제 #1
0
        public PuzzleGrid(PuzzleSetup setup)
        {
            var cells = new List <Cell>();

            for (byte i = 0; i < setup.Values.Length; i++)
            {
                cells.Add(new Cell(
                              idx: i,
                              rowIdx: (byte)(i / 9),
                              colIdx: (byte)(i % 9),
                              boxIdx: (byte)(((i % 9) / 3) + (3 * (i / 27))),
                              solution: setup.Values[i]));
            }
            for (byte i = 0; i < setup.Values.Length; i++)
            {
                var cell = cells[i];
                cell.SetFriends(
                    rowFriends: CellIdxDictionaries.ROW_FRIENDS[i].Select(idx => cells[idx]).ToArray(),
                    colFriends: CellIdxDictionaries.COL_FRIENDS[i].Select(idx => cells[idx]).ToArray(),
                    boxFriends: CellIdxDictionaries.BOX_FRIENDS[i].Select(idx => cells[idx]).ToArray());
            }
            Cells    = cells.ToArray();
            Rows     = CellIdxDictionaries.ROWS.Select(idxs => idxs.Select(idx => cells[idx]).ToArray()).ToArray();
            Cols     = CellIdxDictionaries.COLS.Select(idxs => idxs.Select(idx => cells[idx]).ToArray()).ToArray();
            Boxes    = CellIdxDictionaries.BOXES.Select(idxs => new Box(idxs.Select(idx => cells[idx]).ToArray())).ToArray();
            AllUnits = Rows.Concat(Cols).Concat(Boxes.Select(b => b.Cells)).ToArray();
            BoxRows  = new BoxRow[]
            {
                new BoxRow(
                    rows: new [] { Rows[0], Rows[1], Rows[2] },
                    boxes: new [] { Boxes[0], Boxes[1], Boxes[2] }),
                new BoxRow(
                    rows: new [] { Rows[3], Rows[4], Rows[5] },
                    boxes: new [] { Boxes[3], Boxes[4], Boxes[5] }),
                new BoxRow(
                    rows: new [] { Rows[6], Rows[7], Rows[8] },
                    boxes: new [] { Boxes[6], Boxes[7], Boxes[8] }),
            };
            BoxCols = new BoxCol[]
            {
                new BoxCol(
                    cols: new [] { Cols[0], Cols[1], Cols[2] },
                    boxes: new [] { Boxes[0], Boxes[3], Boxes[6] }),
                new BoxCol(
                    cols: new [] { Cols[3], Cols[4], Cols[5] },
                    boxes: new [] { Boxes[1], Boxes[4], Boxes[7] }),
                new BoxCol(
                    cols: new [] { Cols[6], Cols[7], Cols[8] },
                    boxes: new [] { Boxes[2], Boxes[5], Boxes[8] }),
            };
        }
예제 #2
0
        private static void Solve(PuzzleSetup puzzleSetup)
        {
            IncorrectGuesses = new Dictionary <int, Candidates>();
            for (var i = 0; i < 81; i++)
            {
                IncorrectGuesses[i] = Candidates.None;
            }
            var puzzle = new PuzzleGrid(puzzleSetup);

            foreach (var cell in puzzle.Cells)
            {
                cell.Initialize();
            }
            puzzle.Print();
            bool solved;
            var  result = Solve(puzzle, out solved);

            Console.WriteLine(solved ? "Solved!" : "Failed");
            result.Print();
        }