Exemple #1
0
        internal static void Play(PuzzleSource source, DirectoryInfo directory, int id, bool verbose)
        {
            try
            {
                GameState gameState = GetPuzzle(source, directory, id, verbose);

                throw new NotImplementedException();
            }
            catch (MissingDataException exception)
            {
                Logging.Error(exception.Message);
            }
            catch (NotImplementedException)
            {
                Logging.Error("Feature not implemented.");
            }
            catch (Exception exception)
            {
                #if DEBUG
                Logging.Error("Unexpected exception");
                Console.WriteLine(exception);
                #else
                Logging.Error("Unknown");
                #endif
            }
        }
Exemple #2
0
        internal static void Solve(PuzzleSource source, SolvingMethod method, DirectoryInfo directory, int id, int threads, int timeout, bool verbose)
        {
            try
            {
                GameState gameState = GetPuzzle(source, directory, id, verbose);

                Solver solver;
                double timeElapsed;

                switch (method)
                {
                case SolvingMethod.Sequential:
                    solver = new SequentialSolver(gameState, timeout);
                    Logging.Message($"Attempting to solve using {method} solver...", verbose);
                    break;

                case SolvingMethod.Parallel:
                    solver = new ThreadPoolSolver(gameState, timeout, threads);
                    Logging.Message($"Attempting to solve puzzle #{id} using {method} solver ({threads} threads)...");
                    break;

                default:
                    throw new NotImplementedException();
                }

                timeElapsed = TimeSolverAverage(solver, 1);

                Logging.Message($"{solver.Solutions.Count( x => true )} solution(s) found in {timeElapsed:0.00E+00} seconds:");

                foreach (GameState solution in solver.Solutions)
                {
                    Console.WriteLine();
                    solution.Print();
                }

                Console.WriteLine();
            }
            catch (NonogramException exception)
            {
                Logging.Error(exception.Message);
            }
            catch (TimeoutException exception)
            {
                Logging.Error(exception.Message);
            }
            catch (NotImplementedException)
            {
                Logging.Error("Feature not implemented.");
            }
            catch (System.Exception exception)
            {
                #if DEBUG
                Logging.Error("Unexpected exception");
                Console.WriteLine(exception);
                #else
                Logging.Error("Unknown");
                #endif
            }
        }
Exemple #3
0
        private static GameState GetPuzzle(PuzzleSource source, DirectoryInfo directory, int id, bool verbose)
        {
            InitializeDirectories(directory, new string[] { $"resources/{source.ToString().ToLower()}" }, verbose);

            FileInfo puzzleFile = new FileInfo($"{directory.FullName}resources/{source.ToString().ToLower()}/{id}.xml");

            FetchPuzzle(source, id, puzzleFile, verbose);

            return(LoadPuzzle(puzzleFile, verbose));
        }
Exemple #4
0
        private static void FetchPuzzle(PuzzleSource source, int id, FileInfo fileStore, bool verbose)
        {
            Scrapper scrapper = new Scrapper();

            Logging.Message($"Fetching puzzle from source ({source})...", verbose);

            if (!fileStore.Exists)
            {
                if (source.Equals(PuzzleSource.Local))
                {
                    throw new MissingDataException($"File not found: {fileStore.FullName}\'");
                }

                using (StreamWriter writer = new StreamWriter(fileStore.FullName))
                {
                    scrapper.GetFromSource(source, id, writer);
                }
            }
        }
Exemple #5
0
        public void GetFromSource(PuzzleSource source, int id, TextWriter writer)
        {
            switch (source)
            {
            case PuzzleSource.WebPBN:
                request             = (HttpWebRequest)WebRequest.Create("https://webpbn.com/export.cgi");
                request.Credentials = CredentialCache.DefaultCredentials;
                request.Method      = "POST";

                string postData  = $"fmt=xml&go=1&id={id}";
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);

                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = byteArray.Length;

                Stream dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();

                WebResponse response = request.GetResponse();

                // Display the status. TODO: Turn this into some kind of error check...
                //Console.WriteLine(((HttpWebResponse)response).StatusDescription);

                using (dataStream = response.GetResponseStream())
                {
                    StreamReader reader             = new StreamReader(dataStream);
                    string       responseFromServer = reader.ReadToEnd();

                    if (responseFromServer.Contains("does not exist"))
                    {
                        throw new MissingDataException($"Puzzle {id} from \'{source}\' does not exist.");
                    }

                    writer.WriteLine(responseFromServer);
                }
                response.Close();

                break;
            }
        }
Exemple #6
0
 public MyGridItemModel(PuzzleItemType type, PuzzleSource puzzleSource)
 {
     Type = type;
     this.puzzleSource = puzzleSource;
 }
Exemple #7
0
        internal static void Benchmark(PuzzleSource source, SolvingMethod[] methods, DirectoryInfo directory, string idSet, int trials, int timeout, bool verbose)
        {
            List <BenchmarkData> benchmarkData = new List <BenchmarkData>();

            foreach (int id in ParseID(idSet))
            {
                GameState gameState;

                try
                {
                    gameState = GetPuzzle(source, directory, id, verbose);

                    foreach (SolvingMethod method in methods)
                    {
                        Solver solver;
                        double time;

                        switch (method)
                        {
                        case SolvingMethod.Sequential:
                            solver = new SequentialSolver(gameState, timeout);

                            Logging.Message($"Attempting to solve puzzle #{id} using {method} solver...");

                            time = TimeSolverAverage(solver, trials);
                            benchmarkData.Add(new BenchmarkData(id, gameState.Width, gameState.Height, method, 1, time));
                            break;

                        case SolvingMethod.Parallel:
                            for (int threads = 1; threads <= Environment.ProcessorCount; threads++)
                            {
                                solver = new SequentialSolver(gameState, timeout);

                                Logging.Message($"Attempting to solve puzzle #{id} using {method} solver ({threads} threads)...");

                                time = TimeSolverAverage(solver, trials);
                                benchmarkData.Add(new BenchmarkData(id, gameState.Width, gameState.Height, method, threads, time));
                            }
                            break;

                        default:
                            throw new NotImplementedException();
                        }
                    }
                }
                catch (NonogramException exception)
                {
                    Logging.Error(exception.Message);
                }
                catch (TimeoutException exception)
                {
                    Logging.Error(exception.Message);
                }
                catch (NotImplementedException)
                {
                    Logging.Error("Feature not implemented.");
                }
                catch (System.Exception exception)
                {
                    #if DEBUG
                    Logging.Error("Unexpected exception");
                    Console.WriteLine(exception);
                    #else
                    Logging.Error("Unknown");
                    #endif
                }
            }

            DateTime dateTime = DateTime.Now;

            FileInfo benchmarkFile = new FileInfo($"{directory.FullName}benchmark_{DateTime.Now:dd-MM-yyyy_hh-mm}.csv");

            Logging.Message($"Saving benchmark data to file: {benchmarkFile.Name}");

            using (StreamWriter writer = new StreamWriter(benchmarkFile.FullName))
            {
                writer.WriteLine(BenchmarkData.HeaderString(trials));
                foreach (BenchmarkData data in benchmarkData)
                {
                    writer.WriteLine(data);
                }
            }
        }