Esempio n. 1
0
        public IClearMine LoadGame(string path, Type gameType)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(ResourceHelper.FindText("SavedGamePathNotFound"), path);
            }

            if (gameType == null || gameType.GetInterface(typeof(IClearMine).FullName) == null)
            {
                throw new InvalidOperationException(ResourceHelper.FindText("InvalidClearMineGameType", gameType.FullName));
            }

            IClearMine newgame    = null;
            var        gameLoader = new XmlSerializer(gameType);

            using (var file = File.Open(path, FileMode.Open, FileAccess.Read))
            {
                newgame = gameLoader.Deserialize(file) as IClearMine;
            }

            if (newgame.CheckHash())
            {
                MessageManager.SendMessage <GameLoadMessage>(newgame);
                newgame.ResumeGame();
            }
            else
            {
                MessageBox.Show(ResourceHelper.FindText("CorruptedSavedGameMessage"), ResourceHelper.FindText("CorruptedSavedGameTitle"));
            }

            return(newgame);
        }
Esempio n. 2
0
        public string SaveGame(IClearMine game, string path = null)
        {
            if (game == null)
            {
                throw new ArgumentNullException("game");
            }

            if (String.IsNullOrWhiteSpace(path))
            {
                var savePathDialog = new SaveFileDialog();
                savePathDialog.DefaultExt = Settings.Default.SavedGameExt;
                savePathDialog.Filter     = ResourceHelper.FindText("SavedGameFilter", Settings.Default.SavedGameExt);
                if (savePathDialog.ShowDialog() == true)
                {
                    path = savePathDialog.FileName;
                }
                else
                {
                    return(null);
                }
            }

            // Pause game to make sure the timestamp correct.
            game.PauseGame();
            var gameSaver = new XmlSerializer(game.GetType());

            using (var file = File.Open(path, FileMode.Create, FileAccess.Write))
            {
                gameSaver.Serialize(file, game);
            }
            game.ResumeGame();

            return(path);
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="game"></param>
        public static void UpdateStatistics(this IClearMine game)
        {
            if (game == null)
            {
                throw new ArgumentNullException("game");
            }

            var history = Settings.Default.HeroList.GetByLevel(Settings.Default.Difficulty);

            if (history != null)
            {
                if (game.GameState == GameState.Success)
                {
                    var target   = VisualTreeHelper.GetChild(Application.Current.MainWindow, 0) as FrameworkElement;
                    var filePath = Infrastructure.Container.GetExportedValue <IVisualShoot>().SaveSnapshoot(target);

                    history.IncreaseWon(game.UsedTime / 1000.0, DateTime.Now, filePath);
                }
                else if (game.GameState == GameState.Failed)
                {
                    history.IncreaseLost();
                }
                else
                {
                    history.IncreaseUndone();
                }
            }

            Settings.Default.Save();
        }
Esempio n. 4
0
        public string SaveGame(IClearMine game, string path = null)
        {
            if (game == null)
                throw new ArgumentNullException("game");

            if (String.IsNullOrWhiteSpace(path))
            {
                var savePathDialog = new SaveFileDialog();
                savePathDialog.DefaultExt = Settings.Default.SavedGameExt;
                savePathDialog.Filter = ResourceHelper.FindText("SavedGameFilter", Settings.Default.SavedGameExt);
                if (savePathDialog.ShowDialog() == true)
                {
                    path = savePathDialog.FileName;
                }
                else
                {
                    return null;
                }
            }

            // Pause game to make sure the timestamp correct.
            game.PauseGame();
            var gameSaver = new XmlSerializer(game.GetType());
            using (var file = File.Open(path, FileMode.Create, FileAccess.Write))
            {
                gameSaver.Serialize(file, game);
            }
            game.ResumeGame();

            return path;
        }
Esempio n. 5
0
        public override void Play(IClearMine game, FrameworkElement panel)
        {
            if (game == null)
            {
                throw new ArgumentNullException("game");
            }

            game.GetCell(Column, Row).State = NewState;
        }
Esempio n. 6
0
        public override void Play(IClearMine game, FrameworkElement panel)
        {
            if (panel == null)
            {
                throw new ArgumentNullException("panel");
            }

            var absolutePosition = panel.PointToScreen(Position);

            if (!NativeMethods.MoveMouseTo((int)absolutePosition.X, (int)absolutePosition.Y))
            {
                Trace.TraceError("Cannot move mouse to {0}.", absolutePosition);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// You need a seperated thread to call this Play. Because it will takes a long time.
        /// </summary>
        /// <param name="game"></param>
        /// <param name="panel"></param>
        public void Play(IClearMine game, FrameworkElement panel)
        {
            if (game == null)
            {
                throw new ArgumentNullException("game");
            }

            foreach (var record in this.OrderBy(item => item.Timestamp))
            {
                if (game.UsedTime < record.Timestamp)
                {
                    Thread.Sleep(record.Timestamp - game.UsedTime);
                }

                record.Play(game, panel);
            }
        }
Esempio n. 8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="game"></param>
        /// <param name="cell"></param>
        /// <returns></returns>
        public static bool AutoMarkAt(this IClearMine game, MineCell cell)
        {
            if (game == null)
            {
                throw new ArgumentNullException("game");
            }

            if (cell == null)
            {
                throw new ArgumentNullException("cell");
            }

            if (game.GameState == GameState.Initialized || game.GameState == GameState.Started)
            {
                if (cell.State == CellState.Normal)
                {
                    game.TryMarkAt(cell, CellState.MarkAsMine);
                }
                else if (cell.State == CellState.MarkAsMine)
                {
                    if (Settings.Default.ShowQuestionMark)
                    {
                        game.TryMarkAt(cell, CellState.Question);
                    }
                    else
                    {
                        game.TryMarkAt(cell, CellState.Normal);
                    }
                }
                else if (cell.State == CellState.Question)
                {
                    game.TryMarkAt(cell, CellState.Normal);
                }
                else
                {
                    // Do nothing.
                }

                return(true);
            }

            return(false);
        }
Esempio n. 9
0
        private void OnGameLoaded(GameLoadMessage message)
        {
            if (message.NewGame != null)
            {
                if (game == null)
                {
                    game              = message.NewGame;
                    game.TimeChanged += (sender, e) =>
                    {
                        TriggerPropertyChanged("Time");
                        TriggerPropertyChanged("DigitalTime");
                    };
                }
                else
                {
                    game.Update(message.NewGame);
                }

                RefreshUI();
            }
        }
Esempio n. 10
0
        public void Update(IClearMine newValue)
        {
            var game = newValue as ClearMineGame;

            if (game == null)
            {
                throw new ArgumentException(ResourceHelper.FindText("InvalidUpdateGameType", GetType().Name), "newValue");
            }

            int index = 0;

            this.cells.Clear();
            this.cells.Size = game.cells.Size;
            foreach (var cell in game.Cells)
            {
                this.cells.Insert(index++, cell);
            }

            totalMines = game.totalMines;
            PersistantUsedTime(game.UsedTime);
            GameState = game.GameState;
        }
Esempio n. 11
0
        public void Update(IClearMine newValue)
        {
            var game = newValue as ClearMineGame;

            if (game == null)
            {
                throw new ArgumentException(ResourceHelper.FindText("InvalidUpdateGameType", GetType().Name), "newValue");
            }

            int index = 0;

            this.cells.Clear();
            this.cells.Size = game.cells.Size;
            foreach (var cell in game.Cells)
            {
                this.cells.Insert(index++, cell);
            }

            totalMines = game.totalMines;
            PersistantUsedTime(game.UsedTime);
            GameState = game.GameState;
        }
Esempio n. 12
0
        private void OnGameLoaded(GameLoadMessage message)
        {
            if (message.NewGame != null)
            {
                if (game == null)
                {
                    game = message.NewGame;
                    game.TimeChanged += (sender, e) =>
                    {
                        TriggerPropertyChanged("Time");
                        TriggerPropertyChanged("DigitalTime");
                    };
                }
                else
                {
                    game.Update(message.NewGame);
                }

                RefreshUI();
            }
        }
Esempio n. 13
0
 /// <summary>
 ///
 /// </summary>
 public UserOperationMessage(IClearMine game, GameOperation operation, IEnumerable <MineCell> effectedCells)
 {
     Source        = game;
     UserOperation = operation;
     EffectedCells = effectedCells;
 }
Esempio n. 14
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="game"></param>
 public GameLoadMessage(IClearMine game)
 {
     NewGame = game;
 }
Esempio n. 15
0
 public void Record(IClearMine gameToRecord, FrameworkElement panelToWatch)
 {
     this.game      = gameToRecord;
     this.panel     = panelToWatch;
     this.IsEnabled = true;
 }
Esempio n. 16
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="game"></param>
 public GameLoadMessage(IClearMine game)
 {
     NewGame = game;
 }
Esempio n. 17
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="game"></param>
 public GameStateMessage(IClearMine game)
 {
     Source = game;
 }
 /// <summary>
 /// 
 /// </summary>
 public UserOperationMessage(IClearMine game, GameOperation operation, IEnumerable<MineCell> effectedCells)
 {
     Source = game;
     UserOperation = operation;
     EffectedCells = effectedCells;
 }
Esempio n. 19
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="game"></param>
 public GameStateMessage(IClearMine game)
 {
     Source = game;
 }
Esempio n. 20
0
 public abstract void Play(IClearMine game, FrameworkElement panel);