Example #1
0
 public StateEventArgs(SolutionState state) => State = state;
Example #2
0
 private void OnStateEnter(SolutionState state) => StateEnter?.Invoke(this, new StateEventArgs(state));
Example #3
0
        private static void SolvePuzzle()
        {
            var figures = new[]
            {
                new Figure(Color.White, Point.Of(1, 0), Point.Of(1, 1), Point.Of(0, 1)),
                new Figure(Color.Black, Point.Of(1, 0), Point.Of(0, 1), Point.Of(-1, 0), Point.Of(0, -1)),
                new Figure(Color.White, Point.Of(1, 0), Point.Of(2, 0), Point.Of(3, 0), Point.Of(3, 1)),
                new Figure(Color.Black, Point.Of(1, 0), Point.Of(1, 1), Point.Of(0, 1), Point.Of(2, 0)),
                new Figure(Color.Black, Point.Of(1, 0), Point.Of(-1, 0), Point.Of(1, 1), Point.Of(-1, 1)),
                new Figure(Color.White, Point.Of(0, 1), Point.Of(0, 2), Point.Of(1, 2), Point.Of(-1, 2)),
                new Figure(Color.Black, Point.Of(1, 0), Point.Of(2, 0), Point.Of(2, 1), Point.Of(3, 1)),
                new Figure(Color.Black, Point.Of(1, 0), Point.Of(1, 1), Point.Of(2, 1), Point.Of(2, 2)),
                new Figure(Color.Black, Point.Of(-1, 1), Point.Of(0, 1), Point.Of(1, 1), Point.Of(1, 2)),
                new Figure(Color.White, Point.Of(1, 0), Point.Of(2, 0), Point.Of(2, 1), Point.Of(2, 2)),
                new Figure(Color.Black, Point.Of(1, 0), Point.Of(2, 0), Point.Of(2, 1), Point.Of(3, 0)),
                new Figure(Color.White, Point.Of(1, 0), Point.Of(1, 1), Point.Of(1, 2), Point.Of(2, 2)),
                new Figure(Color.Black, Point.Of(1, 0), Point.Of(2, 0), Point.Of(3, 0), Point.Of(4, 0)),
            };

            var initialState = SolutionState.CreateInitial(figures);

            var r = new Resolver();

            long     stateCounter  = 0;
            DateTime startTime     = DateTime.Now;
            DateTime prevPrintTime = default;

            r.StateEnter +=
                (sender, args) =>
            {
                stateCounter++;
                if ((stateCounter & ((2 << 10) - 1)) == 1)         // Проверка каждые 1024 ~= 1000 состояний
                {
                    var now = DateTime.Now;
                    if (now - prevPrintTime > PRINT_INTERVAL)
                    {
                        var elapsed = now - startTime;

                        Console.Clear();
                        Console.WriteLine("State № {0:###,###,###}:", stateCounter);
                        BoardPrinter.Print(args.State.Board);
                        Console.WriteLine("Total elapsed time: {0:hh\\:mm\\:ss\\.fff}", elapsed);
                        Console.WriteLine("Avarage speed: {0} states/sec.", Math.Round(stateCounter / elapsed.TotalSeconds, 2));

                        prevPrintTime = now;
                    }
                }
            };

            bool found = r.SearchSolution(initialState);

            Console.Clear();
            if (found && r.FinalState != null)
            {
                Console.WriteLine("Resolved :)\r\nStates tested:{0}\r\nTime:{1}", stateCounter, DateTime.Now - startTime);
                BoardPrinter.Print(r.FinalState.Board);
            }
            else
            {
                Console.WriteLine("Solution not found :(\r\nStates tested:{0}\r\nTime:{1}", stateCounter, DateTime.Now - startTime);
            }

            Console.ReadLine();
        }