Example #1
0
        /// <summary>
        /// Initializes a new <see cref="Main"/> instance.
        /// </summary>
        public Main()
        {
            _languageManager = new LanguageManager();
            Game = new Game(1);
            _needSaveGamePrompt = false;

            _languageManager.PropertyChanged += LanguageManager_PropertyChanged;
        }
Example #2
0
        /// <summary>
        /// Loads a previously saved game from a file chosen by the user.
        /// </summary>
        /// <param name="game">The game which has been loaded from the chosen file.</param>
        /// <returns><see langword="true"/> if the user has chosen a valid saved game file to load
        /// and the game has been loaded into <paramref name="game"/> successfully.</returns>
        /// <exception cref="Exception">
        /// The chosen saved game file is damaged or does not contain a valid Untangle saved game.
        /// 
        /// -or-
        /// The chosen saved game file was created by a newer version of the game.
        /// </exception>
        public static bool LoadGame(out Game game)
        {
            string fileName;
            if (!PromptForFileToLoad(out fileName))
            {
                game = null;
                return false;
            }

            // Load the saved game object from the specified file
            SavedGame savedGame = LoadFromFile(fileName);

            // Verify saved game version
            if (savedGame.Version > CurrentVersion)
                throw new Exception(ExceptionMessages.SavedGameVersionNotSupported);

            // Verify that game level vertices are saved
            if (savedGame.Vertices == null)
                throw new Exception(ExceptionMessages.DamagedSavedGame);

            // Create the game and return it
            GameLevel gameLevel = GameLevel.Create(savedGame.Vertices);
            game = new Game(gameLevel, savedGame.LevelNumber);
            return true;
        }
Example #3
0
        /// <summary>
        /// Starts a new game of Untangle from scratch.
        /// </summary>
        /// <remarks>
        /// <para>The user will be prompted to save his current game progress, if needed.</para>
        /// </remarks>
        public void NewGame()
        {
            if (!PromptForSaveGame())
                return;

            Game = new Game(1);
            _needSaveGamePrompt = false;
        }
Example #4
0
        /// <summary>
        /// Saves a game to a file chosen by the user.
        /// </summary>
        /// <param name="game">The game to be saved.</param>
        /// <returns><see langword="true"/> if the user has chosen to save the game and the saved
        /// game file has been created successfully.</returns>
        public static bool SaveGame(Game game)
        {
            string fileName;
            if (!PromptForFileToSave(out fileName))
                return false;

            // Create saved vertex objects
            var savedVertices = new Dictionary<ViewModels.Vertex, Vertex>();
            int idCounter = 0;
            foreach (ViewModels.Vertex vertex in game.Level.GameObjects.OfType<ViewModels.Vertex>())
            {
                var savedVertex = new Vertex
                {
                    Id = idCounter++,
                    X = vertex.X,
                    Y = vertex.Y,
                };
                savedVertices[vertex] = savedVertex;
            }

            // Attach connected vertex IDs to saved vertex objects
            foreach (KeyValuePair<ViewModels.Vertex, Vertex> pair in savedVertices)
            {
                pair.Value.ConnectedVertexIds = pair.Key.ConnectedVertices
                    .Select(d => savedVertices[d].Id)
                    .ToArray();
            }

            // Create saved game objects
            var savedGame = new SavedGame
            {
                Version = CurrentVersion,
                CreationDate = DateTime.Now,
                LevelNumber = game.LevelNumber,
                VertexCount = game.Level.VertexCount,
                IntersectionCount = game.Level.IntersectionCount,
                Vertices = savedVertices.Values.ToArray(),
            };

            // Save the saved game object to the specified file
            SaveToFile(savedGame, fileName);

            return true;
        }