Exemplo n.º 1
0
        /// <summary>
        /// Strong Construction. Start a 'Normal' game
        /// <see cref="Init"/>, then <see cref="InitFX"/>, then <see cref="StartRender"/>
        /// </summary>
        /// <param name="aMap">Puzzle to play</param>
        public GameUI(Puzzle aPuzzle, PuzzleMap aMap, ISoundSubSystem sfx)
            : base(aPuzzle, aMap.Map)
        {
            solution = null;
            puzzleMap = aMap;

            initType = InitTypes.NewGame;

            GameCoords = new GameCoords(this);
            StepCurrent = 0;

            ResourceManager = ResourceFactory.Singleton.GetInstance("Default.GameTiles");
            GameCoords.GlobalTileSize = new SizeInt(32, 32);

            nodes = new List<NodeBase>();
            nodesToRemove = new List<NodeBase>();
            nodesToAdd = new List<NodeBase>();

            // I would like to move this somewhere else
            sound = sfx;
            sfxWelcome = sound.GetHandle("Welcome.wav");
            sfxUndo = sound.GetHandle("sound40.wav");
            sfxRestart = sound.GetHandle("sound31.wav");

            // Add blank bookmarks
            Add((Bookmark)null);
            Add((Bookmark)null);
            Add((Bookmark)null);
            Add((Bookmark)null);
            Add((Bookmark)null);
        }
Exemplo n.º 2
0
        public void TestRoom()
        {
            string[] puzzle = new string[]
                {
                    "~~###########",
                    "~##.....#..P#",
                    "###.X.XX#...#",
                    "#.##X....XX.#",
                    "#..#..X.#...#",
                    "######.######",
                    "#OO.OOX.#$##~",
                    "#.OO....###~~",
                    "#..OO#####~~~",
                    "#########~~~~"
                };

            SokobanMap map = new SokobanMap();
            map.SetFromStrings(puzzle);

            PuzzleMap pMap = new PuzzleMap((Puzzle)null);
            pMap.Map = map;

            SolverController solver = new SolverController(pMap);
            solver.Init();
        }
        private static int TestSimplePuzzle()
        {
            SokobanMap map = new SokobanMap();
            map.setFromStrings(new string[]
                                   {
            "~~~###~~~~~",
            "~~##.#~####",
            "~##..###..#",
            "##.X......#",
            "#...PX.#..#",
            "###.X###..#",
            "~~#..#OO..#",
            "~##.##O#.##",
            "~#......##~",
            "~#.....##~~",
            "~#######~~~"
                                   });

            PuzzleMap pMap = new PuzzleMap(null);
            pMap.Map = map;

            SolverAPI api = new SolverAPI();
            List<INode<SolverNode>> results = api.Solve(pMap);

            if (results == null || results.Count== 0)
            {
                Console.WriteLine("No Solutions found.");
                return -1;
            }

            return 0;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Strong Construction. Start a 'Normal' game
        /// <see cref="Init"/>, then <see cref="InitFX"/>, then <see cref="StartRender"/>
        /// </summary>
        /// <param name="aMap">Puzzle to play</param>
        public GameUI(Puzzle aPuzzle, PuzzleMap aMap, ISoundSubSystem sfx)
            : base(aPuzzle, aMap.Map)
        {
            solution = null;
            puzzleMap = aMap;

            initType = InitTypes.NewGame;

            GameCoords = new GameCoords(this);
            StepCurrent = 0;

            ResourceFactory = ResourceController.Singleton.GetInstance("Default.GameTiles");
            GameCoords.GlobalTileSize = new SizeInt(48, 48);

            nodes = new List<NodeBase>();
            nodesToRemove = new List<NodeBase>();
            nodesToAdd = new List<NodeBase>();

            // I would like to move this somewhere else
            sound = sfx;

            // Add blank bookmarks
            Add((Bookmark)null);
            Add((Bookmark)null);
            Add((Bookmark)null);
            Add((Bookmark)null);
            Add((Bookmark)null);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Add a temp image, by creating a temp image.
 /// </summary>
 /// <param name="Image"></param>
 public void Add(PuzzleMap PuzzleMap, Image Image, string style)
 {
     string tmpFile = ImageFileCache.Singleton.GetImageURL(PuzzleMap);
     if (tmpFile == null)
     {
         tmpFile = ImageFileCache.Singleton.AddSaveImage(PuzzleMap, Image);
     }
     Add("<img src=\"{0}\" style=\"{1}\" alt=\"SokoSolve\"/>", tmpFile, style);
 }
Exemplo n.º 6
0
 public PuzzleMap(PuzzleMap clone)
 {
     puzzle = clone.puzzle;
     map = new SokobanMap(clone.map);
     solutions = new List<Solution>(clone.solutions);
     details = new GenericDescription(clone.details);
     rating = clone.rating;
     mapID = clone.mapID;
     isMasterMap = clone.IsMasterMap;
 }
Exemplo n.º 7
0
        /// <summary>
        /// Strong Contruction
        /// </summary>
        /// <param name="puzzleMap"></param>
        public SolverController(PuzzleMap puzzleMap)
        {
            this.puzzleMap = puzzleMap;
            debugReport = new SolverReport();
            attempted = false;
            stats = new SolverStats(this);
            exitConditions = new ExitConditions();

            // TODO: This should be configured, perhaps via a factory pattern
            strategy = new SolverStrategy(this);
            evaluator = new Evaluator<SolverNode>(true);
        }
Exemplo n.º 8
0
        protected int SolveForSolutions(PuzzleMap puzzle)
        {
            SolverResult results = Solve(puzzle.Map);
            if (!results.HasSolution)
            {
                System.Console.WriteLine("No Solutions found.");
                return 0;
            }

            System.Console.WriteLine("Solutions " + results.Solutions.Count);
            return results.Solutions.Count;
        }
Exemplo n.º 9
0
        /// <summary>
        /// Add or Replace an image, this will save/create a new file in the temp files folder.
        /// Disposal of this class will remove the files.
        /// </summary>
        /// <param name="PuzzleMap"></param>
        /// <param name="NewImage"></param>
        /// <returns></returns>
        public string AddSaveImage(PuzzleMap PuzzleMap, Image NewImage)
        {
            string id = MakeCompoundImageID(PuzzleMap);

            if (images.ContainsKey(id))
            {
                // Already exists
                File.Delete(images[id]);
                images.Remove(id);
            }

            string tmpFile = string.Format("{0}\\{1}.png", Path.GetTempPath(), id);
            NewImage.Save(tmpFile, ImageFormat.Png);
            images[id] = tmpFile;
            return tmpFile;
        }
Exemplo n.º 10
0
        public void TestReverseStrategyCoreSimple()
        {
            CodeTimer timer = new CodeTimer("");
            timer.Start();

            try
            {
                SokobanMap map = new SokobanMap();
                map.SetFromStrings(new string[]
                                   {
             "~##~#####",
              "##.##.O.#",
              "#.##.XO.#",
              "~##.X...#",
              "##.XP.###",
              "#.X..##~~",
              "#OO.##.##",
              "#...#~##~",
              "#####~#~~"
                                   });

                PuzzleMap pMap = new PuzzleMap((Puzzle)null);
                pMap.Map = map;

                SolverController controller = new SolverController(pMap);
                controller.Init();
                controller.State = SolverController.States.Running; // Manually set state, as we are not using the controller; but the strategy uses the controller to check if it should exit

                ReverseStrategy rev = new ReverseStrategy(controller);

                Evaluator<SolverNode> eval = new Evaluator<SolverNode>();
                EvalStatus result =  eval.Evaluate(rev);

                Assert.AreEqual(EvalStatus.CompleteSolution, result, "Should find a solution");
            }
            finally
            {
                timer.Stop();
                System.Console.WriteLine("Total Time: " + timer.Duration(1));
            }
        }
Exemplo n.º 11
0
        private void SolvePuzzle(PuzzleMap puz, string Name)
        {
            using (SolverController ctrl = new SolverController(puz))
            {
                ctrl.ExitConditions.MaxDepth = 1000;
                ctrl.ExitConditions.MaxItterations = 10000000;
                ctrl.ExitConditions.MaxTimeSecs = (float) ArgMaxTime;

                SolverResult res = ctrl.Solve();
                if (res.Exception != null)
                {
                    controller.Display(res.Exception);
                }

                controller.DisplayLable("Name", Name);
                controller.DisplayLable("Result", res.StatusString);
                controller.DisplayLable("Summary", res.Summary);
                controller.DisplayLable("Exit Conditions", ctrl.ExitConditions.ToString());

                if (res.HasSolution)
                {
                    controller.DisplayLable("Solution", res.Solutions[0].Steps);
                }

                if (reportFile != null)
                {
                    reportFile.WriteLine("###########################################################################");
                    reportFile.WriteLine(string.Format("{0} ===> {1}", Name, res.Summary));
                    reportFile.WriteLine("###########################################################################");
                    reportFile.WriteLine(res.Info.ToString());
                    reportFile.WriteLine(res.DebugReport.ToString(new DebugReportFormatter()));
                    reportFile.WriteLine("---------------------------------------------------------------------------");
                    reportFile.WriteLine("");
                }
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Playback a solution in the game client
        /// </summary>
        /// <param name="puzzle"></param>
        /// <param name="map"></param>
        /// <param name="solution"></param>
        /// <param name="returnMode"></param>
        public void StartGameSolution(Puzzle puzzle, PuzzleMap map, Solution solution, Modes returnMode)
        {
            Mode = Modes.Game;
            if (gameControl != null)
            {
                gameControl.ReturnMode = returnMode;

                ProfileController.Current.LibraryLastPuzzle = puzzle.PuzzleID;
                gameControl.StartGameSolution(puzzle, map, solution);
            }
        }
Exemplo n.º 13
0
 public void Solve(PuzzleMap map)
 {
     Mode = Modes.Solver;
     solverControl.Map = map;
 }
Exemplo n.º 14
0
        /// <summary>
        /// Start the game
        /// </summary>
        /// <param name="puzzle"></param>
        /// <param name="map"></param>
        public void StartGameSolution(Puzzle puzzle, PuzzleMap map, Solution solution)
        {
            try
            {
                puzzleMap = map;
                gameUI = new GameUI(puzzle, map, solution);
                gameUI.OnExit += new EventHandler(gameUI_OnExit);
                gameUI.OnGameWin += new EventHandler<NotificationEvent>(gameUI_OnGameWin);
                Game_Resize(null, null);
                timerAnimation.Enabled = true;

                gameUI.StartSolution();
            }
            catch (Exception ex)
            {
                HandleGameException("During Game Startup", ex);
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// Start the game
        /// </summary>
        /// <param name="puzzle"></param>
        /// <param name="map"></param>
        public void StartGame(Puzzle puzzle, PuzzleMap map)
        {
            try
            {
                puzzleMap = map;
                gameUI = new GameUI(puzzle, map, new WindowsSoundSubSystem());
                gameUI.OnExit += new EventHandler(gameUI_OnExit);
                gameUI.OnGameWin += new EventHandler<NotificationEvent>(gameUI_OnGameWin);
                gameUI.OnGameEvents += new EventHandler<NotificationEvent>(gameUI_OnGameEvents);

                Game_Resize(null, null);
                timerAnimation.Enabled = true;

                gameUI.Start();
            }
            catch (Exception ex)
            {
                HandleGameException("During Game Startup", ex);
            }
        }
Exemplo n.º 16
0
 public static Image DrawPuzzle(PuzzleMap map)
 {
     return drawing.Draw(map.Map);
 }
Exemplo n.º 17
0
 public Solution(PuzzleMap parent, VectorInt startPosition)
 {
     this.map = parent;
     this.startPosition = startPosition;
 }
Exemplo n.º 18
0
        protected override Library ImportImplementation(string FileName)
        {
            SokoSolve.Core.Model.Library lib = new SokoSolve.Core.Model.Library(Guid.NewGuid());

            XmlDocument import = new XmlDocument();

            import.Load(FileName);
            lib.Details = new GenericDescription();

            if (import.DocumentElement.LocalName != "SokobanLevels")
                throw new Exception("Expected node 'SokobanLevels'");

            if (import.DocumentElement["Title"] != null)
            {
                lib.Details.Name = import.DocumentElement["Title"].InnerText;
            }

            if (import.DocumentElement["Description"] != null)
            {
                lib.Details.Description = import.DocumentElement["Description"].InnerText;
            }

            if (import.DocumentElement["Email"] != null)
            {
                if (lib.Details.Author == null) lib.Details.Author = new GenericDescriptionAuthor();

                lib.Details.Author.Email = import.DocumentElement["Email"].InnerText;
            }

            if (import.DocumentElement["Url"] != null)
            {
                if (lib.Details.Author == null) lib.Details.Author = new GenericDescriptionAuthor();

                lib.Details.Author.Homepage = import.DocumentElement["Url"].InnerText;
            }

            lib.Categories.Root.Data.Details = new GenericDescription(lib.Details);

            if (import.DocumentElement["LevelCollection"] != null)
            {
                lib.Details.License = import.DocumentElement["LevelCollection"].GetAttribute("Copyright");

                XmlElement xmlLevelCollection = (XmlElement)import.DocumentElement["LevelCollection"];
                int levelcount = 1;
                foreach (XmlElement xmlLevel in xmlLevelCollection.ChildNodes)
                {
                    if (xmlLevel.LocalName == "Level")
                    {
                        Puzzle newPuzzle = new Puzzle(lib);
                        newPuzzle.PuzzleID = lib.IdProvider.GetNextIDString("P{0}");
                        newPuzzle.Details = new GenericDescription();
                        newPuzzle.Details.Name = string.Format("Puzzle No. {0}", lib.Puzzles.Count);
                        newPuzzle.Details.Description = string.Format("Imported SLV puzzle");
                        newPuzzle.Category = lib.Categories.Root.Data;
                        newPuzzle.Order = levelcount++;

                        PuzzleMap newMap = new PuzzleMap(newPuzzle);
                        newMap.MapID = lib.IdProvider.GetNextIDString("M{0}");
                        newPuzzle.Maps.Add(newMap);

                        List<string> rows = new List<string>();
                        foreach (XmlElement xmlRow in xmlLevel.ChildNodes)
                        {
                            rows.Add(xmlRow.InnerText);
                        }
                        newMap.Map = new SokobanMap();
                        newMap.Map.setFromStrings(rows.ToArray(), "?# $.*@+");

                        newMap.Map.ApplyVoidCells();

                        lib.Puzzles.Add(newPuzzle);
                    }
                }
            }

            return lib;
        }
Exemplo n.º 19
0
 public string GetImageURL(PuzzleMap PuzzleMap)
 {
     string id = MakeCompoundImageID(PuzzleMap);
     if (!images.ContainsKey(id)) return null;
     return images[id];
 }
Exemplo n.º 20
0
        public static HtmlBuilder Report(PuzzleMap item, StaticImage drawing)
        {
            HtmlBuilder sb = new HtmlBuilder();

            sb.Add(Report(item.Details));

            if (drawing != null)
            {
                sb.Add(item, drawing.Draw(item.Map), null);
            }

            if (item.HasSolution)
            {
                sb.AddLabel("Solutions", item.Solutions.Count.ToString());
            }

            return sb;
        }
Exemplo n.º 21
0
 public SolverController(PuzzleMap map)
     : this(map.Map)
 {
     PuzzleMap = map;
 }
Exemplo n.º 22
0
        /// <summary>
        /// Start the solver controller on a map
        /// </summary>
        /// <param name="map"></param>
        public void Solve(PuzzleMap map)
        {
            if (map != null)
            {
                solverControl.Library = map.Puzzle.Library;
                solverControl.Puzzle = map.Puzzle;
            }
            else
            {
                solverControl.Library = libControl.CurrentLibrary;
            }

            Mode = Modes.Solver;
        }
Exemplo n.º 23
0
        /// <summary>
        /// Strong Construction. Replay a solution
        /// </summary>
        /// <param name="aPuzzle"></param>
        /// <param name="aMap"></param>
        /// <param name="aSolution"></param>
        public GameUI(Puzzle aPuzzle, PuzzleMap aMap, Solution aSolution)
            : base(aPuzzle, aMap.Map)
        {
            solution = aSolution;
            puzzleMap = aMap;

            initType = InitTypes.SolutionReplay;

            GameCoords = new GameCoords(this);
            StepCurrent = 0;

            ResourceManager = ResourceFactory.Singleton.GetInstance("Default.GameTiles");
            GameCoords.GlobalTileSize = new SizeInt(32, 32);

            nodes = new List<NodeBase>();
            nodesToRemove = new List<NodeBase>();
            nodesToAdd = new List<NodeBase>();
        }
Exemplo n.º 24
0
        private static string MakeCompoundImageID(PuzzleMap PuzzleMap)
        {
            if (PuzzleMap.Puzzle.Library.LibraryID == null) throw new ArgumentNullException("LibraryID");
            if (PuzzleMap.MapID == null) throw new ArgumentNullException("MapID");

            return string.Format("SS-L{0}{1}", PuzzleMap.Puzzle.Library.LibraryID, PuzzleMap.MapID);
        }