public SudokuGenerator(ISudokuSolver sudokuSolver, ISudokuTransformer sudokuTransformer)
        {
            if (sudokuSolver == null || sudokuTransformer == null)
            {
                throw new ArgumentNullException("SudokuSolver 이나/또는 sudokuTransformer 가 null 입니다!");
            }

            this.sudokuSolver      = sudokuSolver;
            this.sudokuTransformer = sudokuTransformer;

            generatedSudokuBoard = new byte[9][];
            for (int i = 0; i < 9; i++)
            {
                generatedSudokuBoard[i] = new byte[9];
            }

            // 이곳에서는 스도쿠 보드를 생성하되, 빈칸은 0으로 채워져 있습니다.
            // sudokuBoard[행][열]로 저장하여 나중에 저장된 이 byte[][] 배열을 참조하여 다른 스도쿠 보드를 생성합니다.
            // 또한 새롭게 생성된 스도쿠 보드를 또다시 섞어 다른 스도쿠 보드를 만듭니다.
            this.sudokuSolver.SolveSudoku(generatedSudokuBoard);
            sudokuBoardForPlayer = new byte[9][];

            for (int i = 0; i < 9; i++)
            {
                sudokuBoardForPlayer[i] = new byte[9];
            }
        }
예제 #2
0
 public HomeController(IOptions <BoardConfiguration> options, ISudokuGenerator sudokuGenerator,
                       ISudokuValidator sudokuValidator, ISudokuSolver sudokuSolver)
 {
     _options         = options;
     _sudokuGenerator = sudokuGenerator;
     _sudokuValidator = sudokuValidator;
     _sudokuSolver    = sudokuSolver;
 }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Solver = LoadSolver("SampleSolver.dll");

            var workingArea = System.Windows.SystemParameters.WorkArea;

            this.Left = workingArea.Left;
            this.Top  = workingArea.Top - this.Height;
        }
예제 #4
0
        static void Main(string[] args)
        {
            var container = Bootstrap.start();

            ISudokuSolver solver = (ISudokuSolver)container.Resolve(typeof(ISudokuSolver));

            var fileName = ConfigurationManager.AppSettings["defaultFileName"];

            var filesToSolve = new List <string>();

            if (args.Length > 0)
            {
                if (args[0] == "-p")
                {
                    solveProectEuler(args[1], solver);
                }
                else
                {
                    for (var i = 0; i < args.Length; i++)
                    {
                        filesToSolve.Add(args[i]);
                    }
                }
            }
            else
            {
                filesToSolve.Add(fileName);
            }

            foreach (var fileToSolve in filesToSolve)
            {
                var grid = solver.SolvePuzzle(fileToSolve);

                foreach (var row in grid)
                {
                    foreach (var col in row)
                    {
                        if (col.Count == 1)
                        {
                            Console.Write(col.Single());
                        }
                        else
                        {
                            Console.Write('X');
                        }
                    }

                    Console.WriteLine();
                }

                Console.WriteLine();
            }

#if DEBUG
            Console.ReadLine();
#endif
        }
예제 #5
0
        public SudokuUserControl(ISudokuGenerator sudokuGenerator, ISudokuSolver sudokuSolver)
        {
            if (sudokuGenerator == null || sudokuSolver == null)
            {
                throw new ArgumentNullException("SudokuGenerator 과/또는 sudokuSolver 가 null 입니다!");
            }

            this.sudokuGenerator = sudokuGenerator;
            this.sudokuSolver    = sudokuSolver;
        }
예제 #6
0
        public static SudokuBoard NewSudoku(SudokuDifficulty difficulty, ISudokuSolver solverGenerator = null)
        {
            _solverGenerator = solverGenerator ?? new DeepFirstSearchSolver();

            var blankboard = new SudokuBoard(difficulty);

            return(_solverGenerator
                   .Solve(blankboard)
                   .CreateBlankCells());
        }
        public void GenerateGameUsingSolver(ISudokuSolver s, int givens)
        {
            int size = Rows.Count * Rows.Count;

            if (givens < 0 || givens > size * size)
            {
                throw new ArgumentOutOfRangeException("givens", givens, "Invalid number of givens");
            }
            for (int r = 0; r < size; r++)
            {
                for (int c = 0; c < size; c++)
                {
                    Cell cell = this[r, c];
                    cell.ReadOnly = false;
                    cell.Value    = null;
                }
            }
            Random rnd = new Random();
            int    row = rnd.Next(size);
            int    col = rnd.Next(size);

            this[row, col].Value = rnd.Next(size) + 1;
            int?[,] a            = this.ToArray();
            s.Solve(ref a);
            this.FromArray(a);
            for (int i = 0; i < (size * size) - givens; i++)
            {
                for (; ;)
                {
                    int r = rnd.Next(size);
                    int c = rnd.Next(size);

                    Cell cell = this[r, c];
                    if (!cell.Value.HasValue)
                    {
                        continue;
                    }
                    cell.Value    = null;
                    cell.ReadOnly = false;
                    break;
                }
            }
            for (int r = 0; r < size; r++)
            {
                for (int c = 0; c < size; c++)
                {
                    Cell cell = this[r, c];
                    if (cell.Value.HasValue)
                    {
                        cell.ReadOnly = true;
                    }
                }
            }
        }
예제 #8
0
        void WindowLoaded(object sender, RoutedEventArgs e)
        {
            TitleReflect.Fill       = new VisualBrush(TitleText);
            Solver                  = LoadSolver("SampleSolver.dll");
            TimerControls.IsEnabled = false;
            TimerControls.Opacity   = 0.25;
            //NewGameClicked(null, null);

            //DoubleAnimation da = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(5)));
            //da.RepeatBehavior = RepeatBehavior.Forever;
            //StopwatchControl.BeginAnimation(Stopwatch.CurrentTimeProperty, da);
        }
예제 #9
0
파일: Sudoku.cs 프로젝트: 3391916/Sudoku
        public Sudoku(List <SudokuCell> cells)
        {
            if (cells == null)
            {
                throw new ArgumentNullException("cells can't be equal to null");
            }

            _cells = cells;

            _sudokuSolver = ServiceLocator.Current.GetInstance <ISudokuSolver>();
            _sudokuComplexityEstimator = ServiceLocator.Current.GetInstance <ISudokuComplexityEstimator>();
            _sudokuDifficultyLevel     = _sudokuComplexityEstimator.EstimateComplexityLevel(this);
        }
예제 #10
0
        public Sudoku(ISudokuSolver solver, int[] table)
        {
            if (table.Length != 81)
            {
                throw new ArgumentException("table size must be 81", "table");
            }
            if (table.Any(x => x < 0 || x > 9))
            {
                throw new ArgumentException("Each value in table must be between 0 and 9");
            }

            _table  = table.Copy();
            _solver = solver;
        }
예제 #11
0
 public SudokuGenerator(IOptions <BoardConfiguration> options, ISudokuSolver sudokuSolver)
 {
     _sudokuSolver  = sudokuSolver;
     _boardSize     = options.Value.BoardSize;
     _blockSize     = options.Value.BlockSize;
     _mixStrategies = new List <Func <byte?[, ], byte, byte, byte, byte?[, ]> >
     {
         BoardExtensions.Transpose,
         BoardExtensions.SwapRow,
         BoardExtensions.SwapRowBlock,
         BoardExtensions.SwapColumn,
         BoardExtensions.SwapColumnBlock
     };
     _random = new Random();
 }
예제 #12
0
        public SudokuViewModel(IWpfThread thread, ISudokuSolver solver)
        {
            SolveAnimatedCommand = new RelayCommand(StartSolvingAnimated);
            SolveCommand         = new RelayCommand(Solve);
            ResetCommand         = new RelayCommand(Reset);

            _backingArray = new SudokuViewModelItem[81];
            for (int i = 0; i < _backingArray.Length; i++)
            {
                Cells.Add(_backingArray[i] = new SudokuViewModelItem());
            }

            _thread = thread;
            _solver = solver;
        }
예제 #13
0
        public static float testSolution(ISudokuSolver solver, String sudoku)
        {
            var   watch = Stopwatch.StartNew();
            float elapsedMs;

            solver.Sudoku.newSudoku(sudoku);
            watch = Stopwatch.StartNew();
            solver.Solve();
            watch.Stop();
            elapsedMs = watch.ElapsedMilliseconds;
            if (!solver.Sudoku.validationSudoku())
            {
                solver.Sudoku.showTwoSudoku();
                return(-1);
            }
            //solver.Sudoku.showTwoSudoku();
            return(elapsedMs);
        }
예제 #14
0
        private static void solveProectEuler(string projectEulerFileName, ISudokuSolver solver)
        {
            var file = new StreamReader(projectEulerFileName);

            var line = "";

            while ((line = file.ReadLine()) != null)
            {
                Console.WriteLine(line);

                var grid = new List <List <HashSet <int> > >();

                for (var i = 0; i < 9; i++)
                {
                    var row = new List <HashSet <int> >();

                    line = file.ReadLine();

                    foreach (var box in line)
                    {
                        if (box == 'X' || box == '0')
                        {
                            row.Add(new HashSet <int>()
                            {
                                1, 2, 3, 4, 5, 6, 7, 8, 9
                            });
                        }
                        else
                        {
                            row.Add(new HashSet <int> {
                                int.Parse(box.ToString())
                            });
                        }
                    }

                    grid.Add(row);
                }

                var result = solver.SolvePuzzle(grid);

                printGrid(result);
                Console.WriteLine(isValid(result));
            }
        }
예제 #15
0
        void SolveClicked(object sender, RoutedEventArgs e)
        {
            ISudokuSolver sol = SolverList.SelectedItem as ISudokuSolver;

            Board.IsEnabled = true;
            Storyboard sb = this.Resources["TimerAnimation"] as Storyboard;

            sb.Stop(this);
            MinNumber.Tag              = 59;
            SecNumber.Tag              = 59;
            SubSecNumber.Tag           = 59;
            TimerControls.Opacity      = 0.25;
            TimerControls.IsEnabled    = false;
            StopwatchControl.IsChecked = false;
            ShowWait();

            int?[,] a = Board.GameBoard.ToArray();
            bool solved = false;

            solverWorker = new BackgroundWorker();

            solverWorker.DoWork += new DoWorkEventHandler(delegate(object dwsender, DoWorkEventArgs dwe)
            {
                solved = sol.Solve(ref a);
            });
            solverWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object rwcsender, RunWorkerCompletedEventArgs rwce)
            {
                MessageLayer.IsEnabled = false;
                if (!solved)
                {
                    ShowMessage("No Solution!");
                }
                else
                {
                    Board.GameBoard.FromArray(a);
                }
            });
            solverWorker.RunWorkerAsync();
        }
예제 #16
0
        static void Main(string[] args)
        {
            var types = AppDomain.CurrentDomain.GetAssemblies()
                        .SelectMany(s => s.GetTypes())
                        .Where(p => typeof(SudokuLib.ISudokuSolver).IsAssignableFrom(p));

            foreach (Type item in types)
            {
                Sudoku        sud         = new Sudoku();
                ISudokuSolver localSolver = (ISudokuSolver)Activator.CreateInstance(item);

                var sud_one = sud.ParseFile(@"C:\Users\K\Documents\Work\CSharp\Sudoku_General\Benchmark\sudoku_files\Easy.txt")[0];

                var watch = System.Diagnostics.Stopwatch.StartNew();
                localSolver.Solve(sud_one);
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;

                Console.WriteLine(item.Name);
                Console.WriteLine(elapsedMs);
                Console.ReadKey();
            }
        }
예제 #17
0
 public SudokuController(ISudokuSolver sudokuSolver, ISudokuGenerator sudokuGenerator)
 {
     SudokuGenerator = sudokuGenerator;
     SudokuSolver    = sudokuSolver;
 }
예제 #18
0
 public void Init(Type t)
 {
     solver = Activator.CreateInstance(t) as ISudokuSolver;
 }
예제 #19
0
 //使用解题器的随机生成方法
 /// <summary>
 /// 通过解题器生成游戏
 /// </summary>
 public void GenerateGameUsingSolver(ISudokuSolver s, int givens)
 {
     int size = Rows.Count * Rows.Count;
     if (givens < 0 || givens > size * size)
     {
         throw new ArgumentOutOfRangeException("givens", givens, "给定的生成已知数数目数目不合法");
     }
     for (int r = 0; r < size; r++)
     {
         for (int c = 0; c < size; c++)
         {
             Cell cell = this[r, c];
             cell.ReadOnly = false;
             cell.Value = null;
         }
     }
     Random rnd = new Random();
     int row = rnd.Next(size);
     int col = rnd.Next(size);
     this[row, col].Value = rnd.Next(size) + 1;
     int?[,] a = this.ToArray();//将谜题放入数组
     s.Solve(ref a); //判断能否求得谜题答案
     this.FromArray(a);
     for (int i = 0; i < (size * size) - givens; i++) //挖掉数字
     {
         for (; ; )
         {
             int r = rnd.Next(size);
             int c = rnd.Next(size);
             Cell cell = this[r, c];
             if (!cell.Value.HasValue)//判断是否为空值
                 continue;
             cell.Value = null;
             cell.ReadOnly = false;
             break;
         }
     }
     for (int r = 0; r < size; r++)
     {
         for (int c = 0; c < size; c++)
         {
             Cell cell = this[r, c];
             if (cell.Value.HasValue)
                 cell.ReadOnly = true;
         }
     }
 }
예제 #20
0
 public SudokuSolverService(ISudokuSolver sudokuSolver)
 {
     this.sudokuSolver = sudokuSolver;
 }
예제 #21
0
 private Sudoku(ISudokuSolver solver, ISudokuGenerator generator)
 {
     _solver      = solver;
     _generator   = generator;
     _loadFactory = new DataLoadFactory();
 }
예제 #22
0
 public Sudoku(ISudokuSolver solver)
 {
     _table  = generateNewSudoku();
     _solver = solver;
 }
예제 #23
0
 public SudokuService()
 {
     sudokuFieldBuilder = new SudokuFieldBuilder();
     sudokuValidator    = new SudokuValidator();
     sudokuSolver       = new SudokuSolver();
 }
예제 #24
0
        void NewGameClicked(object sender, RoutedEventArgs e)
        {
            Board.GameBoard = new Board((int)BoardSize.SelectedItem);
            Storyboard s = this.Resources["TimerAnimation"] as Storyboard;

            if (FastGenRadio.IsChecked == true)
            {
                Board.GameBoard.GenerateGame((int)BoardSize.SelectedItem * 2);
                Board.GameBoard.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(GameBoard_PropertyChanged);
            }
            else
            {
                Board         b      = Board.GameBoard;
                int           givens = (int)BoardSize.SelectedItem * 2;
                ISudokuSolver sol    = SolverList.SelectedItem as ISudokuSolver;
                solverWorker = new BackgroundWorker();
                ShowWait();
                solverWorker.DoWork += new DoWorkEventHandler(delegate(object dwsender, DoWorkEventArgs dwe)
                {
                    b.GenerateGameUsingSolver(sol, givens);
                });
                solverWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object rwcsender, RunWorkerCompletedEventArgs rwce)
                {
                    MessageLayer.IsEnabled = false;
                    s.Begin(this, true);
                    Board.GameBoard.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(GameBoard_PropertyChanged);
                });
                solverWorker.RunWorkerAsync();
            }



            s.Stop(this);

            if (NoTimerRadio.IsChecked == true)
            {
                MinNumber.Tag              = 59;
                SecNumber.Tag              = 59;
                SubSecNumber.Tag           = 59;
                TimerControls.Opacity      = 0.25;
                TimerControls.IsEnabled    = false;
                StopwatchControl.IsChecked = false;
            }
            else
            {
                Int32 length;
                if (EasyTimerRadio.IsChecked == true)
                {
                    length = 15;
                }
                else if (MediumTimerRadio.IsChecked == true)
                {
                    length = 10;
                }
                else
                {
                    length = 5;
                }
                //s.CutoffTime = TimeSpan.FromMinutes(length);
                s.Children[0].Duration               = new Duration(TimeSpan.FromMinutes(length));
                s.Children[1].Duration               = new Duration(TimeSpan.FromMinutes(length));
                s.Children[2].RepeatBehavior         = new RepeatBehavior(TimeSpan.FromMinutes(length));
                s.Children[3].RepeatBehavior         = new RepeatBehavior(TimeSpan.FromMinutes(length));
                ((Int32Animation)s.Children[1]).From = length - 1;
                StopwatchControl.IsChecked           = true;
                MinNumber.Tag           = length - 1;
                TimerControls.Opacity   = 1;
                TimerControls.IsEnabled = true;
                if (FastGenRadio.IsChecked == true)
                {
                    s.Begin(this, true);
                }
            }

            Board.IsEnabled = true;
        }
예제 #25
0
 public void Setup()
 {
     SudokuSolver = new SudokuSolver();
 }
예제 #26
0
 public void Setup()
 {
     SudokuSolver    = new SudokuSolver();
     SudokuGenerator = new SudokuGenerator(SudokuSolver);
 }
예제 #27
0
 public ConcurrentSudokuGame(ISudokuGenerator generator, ISudokuSolver solver, ISessionMapper <Guid> sessionMapper) : base(generator, solver, sessionMapper)
 {
 }
예제 #28
0
        public SolvingManager(string input)
        {
            var sudoku = new Sudoku(input);

            solver = new SudokuSolver(sudoku);
        }
예제 #29
0
 protected AbstractSudokuGame(ISudokuGenerator generator, ISudokuSolver solver, ISessionMapper <Guid> sessionMapper)
 {
     Generator          = generator;
     Solver             = solver;
     this.SessionMapper = sessionMapper;
 }
예제 #30
0
 public SudokuGenerator(ISudokuSolver sudokuSolver)
 {
     SudokuSolver = sudokuSolver;
 }