Пример #1
0
 public static void UpdateColumnNeighbours(this Grid grid, int y)
 {
     Console.WriteLine("update column {0}", y);
     for (int i = 0; i < grid.GridSize; i++)
     {
         if (grid.GetCell(i, y).Solved)
         {
             continue;
         }
         grid.GetCell(i, y).Options = grid.UpdateCellOptions(i, y, grid.GetCell(i, y).Square);
         grid.CheckCellForSingleOption(i, y);
     }
 }
Пример #2
0
 public static void UpdateSquareNeighbours(this Grid grid, int s)
 {
     Console.WriteLine("update square {0}", s);
     Cell[] square = grid.GetSquareCells(s);
     for (int i = 0; i < grid.GridSize; i++)
     {
         if (grid.GetCell(square[i].X, square[i].Y).Solved)
         {
             continue;
         }
         grid.GetCell(square[i].X, square[i].Y).Options = grid.UpdateCellOptions(square[i].X, square[i].Y, s);
         grid.CheckCellForSingleOption(square[i].X, square[i].Y);
     }
 }
Пример #3
0
        public static void UpdateRowNeighbours(this Grid grid, int x)
        {
            Console.WriteLine("update row  {0}", x);
            for (int i = 0; i < grid.GridSize; i++)
            {
                if (grid.GetCell(x, i).Solved)
                {
                    continue;
                }

                grid.GetCell(x, i).Options = grid.UpdateCellOptions(x, i, grid.GetCell(x, i).Square);

                grid.CheckCellForSingleOption(x, i);
            }
        }
Пример #4
0
        static void Main()
        {
            grid = new InputService().GetInput();
            // TODO: optimise order of iteration --> maintain record of 'areas' that are well-populated somehow??

            //  grid.UpdateNeighbours(4,2);

            for (int i = 0; i < grid.GridSize; i++)
            {
                for (int j = 0; j < grid.GridSize; j++)
                {
                    if (grid.GetCell(i, j).Value != 0)
                    {
                        continue;
                    }

                    grid.GetCell(i, j).Options = grid.UpdateCellOptions(i, j, grid.GetCell(i, j).Square);
                }
            }


            while (grid.CompletedCells < (grid.GridSize * grid.GridSize))
            {
                // TODO::: UTMOST IMPORTANCE
                // important --> should be called in other locations also? After every update, on every cell --> or only on every dependent cell

                /*     for (int i = 0; i < grid.GridSize; i++)
                 *   {
                 *       for (int j = 0; j < grid.GridSize; j++)
                 *       {
                 *           Cell c = grid.GetCell(i, j);
                 *           // TODO: maintain simple list of unsolved cells
                 *           if (c.Value != 0)
                 *           {
                 *               continue;
                 *           }
                 *
                 *           c.Options = grid.UpdateCellOptions(i, j, c.Square);
                 *           if (c.Options.Count <= 1)
                 *           {
                 *               grid.GetCell(i, j).Value = c.Options[0];
                 *               grid.GetCell(i, j).Options.Clear();
                 *               grid.CompletedCells++;
                 *               Console.WriteLine("Solved a number: [{0},{1}], value = {2}", i, j, c.Value);
                 *           }
                 *       }
                 *   }*/

                //NEXT STEPS: implement http://www.conceptispuzzles.com/index.aspx?uri=puzzle/sudoku/techniques

                grid.SearchRowForSingleCandidates();
                grid.SearchColumnForSingleCandidates();
                grid.SearchSquareForSingleCandidates();


//                for (int i = 0; i < grid.GridSize; i++)
//                {
//                    for (int j = 0; j < grid.GridSize; j++)
//                    {
//                        Console.Write(grid.GetCell(i, j).Value + " ");
//                    }
//                    Console.WriteLine();
//                }
//                Console.WriteLine();
            }

            Console.WriteLine(grid.IsValidSolution());

            // The completed grid!!
            for (int i = 0; i < grid.GridSize; i++)
            {
                for (int j = 0; j < grid.GridSize; j++)
                {
                    Console.Write(grid.GetCell(i, j).Value + " ");
                }
                Console.WriteLine();
            }

            Console.WriteLine("DONE");

            Console.ReadKey();
        }