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); }
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); }
/// <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(); }
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; }
public override void Play(IClearMine game, FrameworkElement panel) { if (game == null) { throw new ArgumentNullException("game"); } game.GetCell(Column, Row).State = NewState; }
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); } }
/// <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); } }
/// <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); }
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(); } }
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; }
/// <summary> /// /// </summary> public UserOperationMessage(IClearMine game, GameOperation operation, IEnumerable <MineCell> effectedCells) { Source = game; UserOperation = operation; EffectedCells = effectedCells; }
/// <summary> /// /// </summary> /// <param name="game"></param> public GameLoadMessage(IClearMine game) { NewGame = game; }
public void Record(IClearMine gameToRecord, FrameworkElement panelToWatch) { this.game = gameToRecord; this.panel = panelToWatch; this.IsEnabled = true; }
/// <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; }
public abstract void Play(IClearMine game, FrameworkElement panel);