Example #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);
        }
Example #2
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);
        }
Example #3
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>();
        }
Example #4
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);
            }
        }
Example #5
0
        /// <summary>
        /// Save the new puzzle solution
        /// </summary>
        private void PuzzleCompletedSave()
        {
            // Save
            Solution sol = new Solution(gameUI.PuzzleMap,  gameUI.StartPuzzle.Player);
            sol.FromGame(gameUI.Moves);
            sol.Details = new GenericDescription();
            sol.Details.Name = string.Format("'{0}' Solution", gameUI.Puzzle.Details.Name);
            sol.Details.Date = DateTime.Now;
            sol.Details.DateSpecified = true;
            sol.Details.Comments = gameUI.Stats.ToString();
            sol.Details.Author= new GenericDescriptionAuthor();
            sol.Details.Author.Name = ProfileController.Current.UserName;
            sol.Details.Author.Email = ProfileController.Current.UserEmail;
            sol.Details.Author.Homepage = ProfileController.Current.UserHomepage;
            sol.Details.License = ProfileController.Current.UserLicense;
            gameUI.Puzzle.MasterMap.Solutions.Add(sol);

            // TODO: Save Way points

            // Give feedback
            ShowText("Solution Saved.");
        }
Example #6
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);
            }
        }
Example #7
0
        /// <summary>
        /// Test a solution to see if it is valid
        /// </summary>
        /// <param name="aSolution"></param>
        public bool Test(Solution aSolution, out string FirstError)
        {
            FirstError = null;
            int cc = 0;
            foreach (Direction move in aSolution.ToPath().Moves)
            {
                MoveResult res = Move(move);
                if (res == MoveResult.Invalid)
                {
                    FirstError = string.Format("Invalid move at step {0}", cc);
                    return false;
                }
                if (res == MoveResult.ValidPushWin) return true;

                cc++;
            }

            FirstError = string.Format("Moves do not result in a solution. Check {0} moves.", cc);

            return false;
        }
        private XmlElement CreateSolution(Solution solution)
        {
            XmlElement xml = report.CreateElement("p");
            xml.AppendChild(xml.AppendChild(CreateContentTag("h4", string.Format("{0} steps -- {1}", solution.Steps.Length, solution.Details.Name))));
            if (solution.Details != null)
            {
                xml.AppendChild(CreateGenericDescriptionTable(solution.Details, false));
            }
            XmlElement para = CreateContentTag("code", "");
            para.InnerXml = solution.ToStringDisplay("<br/>");
            xml.AppendChild(para);

            return xml;
        }
        /// <summary>
        /// Invoked (but back on the UI thread) on completeion of the solver
        /// </summary>
        private void SolverComplete()
        {
            try
            {
                if (lastException != null)
                {
                    FormError error = new FormError();
                    error.Exception = lastException;
                    error.ShowDialog();
                    return;
                }

                if (solver == null) return; // Thread Abort.

                // Found a solutions?
                if (solverEvalStatus == EvalStatus.CompleteSolution)
                {
                    DialogResult result = MessageBox.Show(
                        "Solution found, do you want to save it? This will also include a report in the description.",
                        "Solution Found", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {

                        Solution sol = new Solution(Map, Map.Map.Player);
                        sol.Details = new GenericDescription();
                        sol.Details.Name = "SokoSolve Solution";
                        sol.Details.Author = new GenericDescriptionAuthor();
                        sol.Details.Author.Name = ProfileController.Current.UserName;
                        sol.Details.Author.Email = ProfileController.Current.UserEmail;
                        sol.Details.Author.Homepage = ProfileController.Current.UserHomepage;
                        sol.Details.License = ProfileController.Current.UserLicense;
                        sol.Details.Date = DateTime.Now;
                        sol.Details.DateSpecified = true;

                        // Build a description
                        SolverLabelList labels = solver.Stats.GetDisplayData();
                        labels.Add("Machine", string.Format("{0} Running {1}.", DebugHelper.GetCPUDescription(), Environment.OSVersion));
                        labels.Add("SokoSolve", Program.GetVersionString());
                        sol.Details.Description = labels.ToHTML(null, "tabledata");

                        Path path = solver.Strategy.BuildPath(solver.Evaluator.Solutions[0].Data);
                        sol.Set(path);
                        Map.Solutions.Add(sol);
                    }
                }
                else
                {
                    MessageBox.Show("Solver failed: " + solverEvalStatus.ToString(), "No Solution Found",
                                    MessageBoxButtons.OK);
                }

                // Write a solver log report
                BuildSolverLogReport();

                complete = true;
                timer.Enabled = false;
                worker = null;
                UpdateStatus();
            }
            catch (Exception ex)
            {
                lastException = ex;
                FormError error = new FormError();
                error.Exception = lastException;
                error.ShowDialog();
                return;
            }
        }
        protected override void ExecuteImplementation(CommandInstance<ExplorerItem> instance)
        {
            ItemPuzzle itemPuzzle = instance.Context[0] as ItemPuzzle;
            if (itemPuzzle != null)
            {
                Solution newSol = new Solution(itemPuzzle.DomainData.MasterMap, itemPuzzle.DomainData.MasterMap.Map.Player);
                newSol.Details = ProfileController.Current.GenericDescription;
                newSol.Details.Name = string.Format("Manual solution for {0}", newSol.Map.Puzzle.Details.Name);
                newSol.Steps = "";

                itemPuzzle.DomainData.MasterMap.Solutions.Add(newSol);

                // Sync UI
                itemPuzzle.SyncDomain();

                // Refresh enture tree
                Controller.Explorer.SyncUI();
            }
        }
        /// <summary>
        /// Generate the solution from the solver tree
        /// </summary>
        /// <returns></returns>
        private List<Solution> BuildSolutionPath()
        {
            List<INode<SolverNode>> solutions = evaluator.Solutions;
            if (solutions == null || solutions.Count == 0)
            {
                solutions = reverseEvaluator.Solutions;
            }
            if (solutions == null || solutions.Count == 0)
            {
                // No solution forward or reverse
                throw new Exception("A solution was expected, but not found");
            }

            List<Solution> results = new List<Solution>();
            foreach (INode<SolverNode> node in solutions)
            {
                if (node.Data.Status == SolverNodeStates.Solution)
                {
                    if (node.Data.IsForward)
                    {
                        Solution simpleForward = new Solution(PuzzleMap, Map.Player);
                        simpleForward.Set(strategy.BuildPath(node.Data));

                        simpleForward.Details = new GenericDescription();
                        simpleForward.Details.Name = "SokoSolve Solution";
                        simpleForward.Details.Author = new GenericDescriptionAuthor();
                        simpleForward.Details.Date = DateTime.Now;
                        simpleForward.Details.DateSpecified = true;

                        // Build a description
                        SolverLabelList labels = Stats.GetDisplayData();
                        labels.Add("Machine", string.Format("{0} Running {1}.", DebugHelper.GetCPUDescription(), Environment.OSVersion));

                        simpleForward.Details.Description = labels.ToString();

                        results.Add(simpleForward);
                    }
                    else
                    {

                        Solution simpleForward = new Solution(PuzzleMap, Map.Player);
                        simpleForward.Set(reverseStrategy.BuildPath(node.Data));

                        simpleForward.Details = new GenericDescription();
                        simpleForward.Details.Name = "SokoSolve Solution";
                        simpleForward.Details.Author = new GenericDescriptionAuthor();
                        simpleForward.Details.Date = DateTime.Now;
                        simpleForward.Details.DateSpecified = true;

                        // Build a description
                        SolverLabelList labels = Stats.GetDisplayData();
                        labels.Add("Machine", string.Format("{0} Running {1}.", DebugHelper.GetCPUDescription(), Environment.OSVersion));

                        simpleForward.Details.Description = labels.ToString();

                        results.Add(simpleForward);
                    }

                }

                if (node.Data.Status == SolverNodeStates.SolutionChain)
                {
                    if (node.Data.ChainSolutionLink == null) throw new InvalidDataException("ChainSolutionLink must be set");

                    SolverNode forward = null;
                    SolverNode reverse = null;
                    if (node.Data.IsForward)
                    {
                        forward = node.Data;
                        reverse = node.Data.ChainSolutionLink;
                    }
                    else
                    {
                        forward = node.Data.ChainSolutionLink;
                        reverse = node.Data;
                    }

                    // Build the paths
                    Path forwardPortion = strategy.BuildPath(forward);
                    Path reversePortion = this.reverseStrategy.BuildPath(reverse);

                    debugReport.AppendLabel("Forward Chain", "[{0}->{1}] links to {2}", forward.PlayerPositionBeforeMove,forward.PlayerPosition, forward.ChainSolutionLink.PlayerPosition);
                    debugReport.AppendLabel("Reverse Chain", "[{0}->{1}] links to {2}", reverse.PlayerPositionBeforeMove, reverse.PlayerPosition, reverse.ChainSolutionLink.PlayerPosition);

                    debugReport.AppendLabel("Forward", string.Format("[{0}] {1}", forwardPortion.Count, StringHelper.Join(forwardPortion.MovesAsPosition, null, ", ")));
                    debugReport.AppendLabel("Reverse", string.Format("[{0}] {1}", reversePortion.Count, StringHelper.Join(reversePortion.MovesAsPosition, null, ", ")));

                    Path fullPath = new Path(Map.Player);
                    fullPath.Add(forwardPortion);
                    fullPath.Add(reversePortion);

                    // Join them
                    Solution solution = new Solution(PuzzleMap, Map.Player);
                    solution.Set(fullPath);

                    solution.Details = new GenericDescription();
                    solution.Details.Name = "SokoSolve Solution";
                    solution.Details.Author = new GenericDescriptionAuthor();
                    solution.Details.Date = DateTime.Now;
                    solution.Details.DateSpecified = true;

                    // Build a description
                    SolverLabelList labels = Stats.GetDisplayData();
                    labels.Add("Machine", string.Format("{0} Running {1}.", DebugHelper.GetCPUDescription(), Environment.OSVersion));

                    solution.Details.Description = labels.ToString();

                    results.Add(solution);
                }
            }

            // Sanity check
            string firsterror;
            foreach (Solution solution in results)
            {
                if (!solution.Test(map, out firsterror)) throw new Exception("Sanity Check Failed. Solution is not valid: "+firsterror);
            }

            return results;
        }