/// <summary> /// This method is executed as a thread. It has the purpose of invoking some main thread code /// after waiting 2 seconds. /// </summary> /// <remarks> /// This thread does not block the main thread with sleep, and the code executed in invoke is called /// within the main thread, so we don't have to manage concurrency and avoid using locks. /// </remarks> private void SleepAndUpdateGame() { Thread.Sleep(2000); Threading.Invoke(() => { if (FlippedCard2.Equals(FlippedCard1)) { Score += 1; FlippedCard1.Remove(); FlippedCard2.Remove(); if (GameEnd()) { RefresDataView(); CheckScoreAndFinish(); } } else { Score -= 1; FlippedCard1.PutFaceDown(); FlippedCard2.PutFaceDown(); } FlippedCard1 = null; FlippedCard2 = null; RefresDataView(); }); }
/// <summary> /// This method shows the final score to the user, and store it in a file if it's a nre record. /// </summary> private void CheckScoreAndFinish() { string newRecord = ""; List <int> recordList = TextTools.GetRecords(); int[] records = recordList.ToArray(); if (records != null && records.Count() > 0) // modify the array to include the new value if it's a record. { if (records.All(x => x < Score)) { recordList.Insert(0, Score); TextTools.SaveRecords(recordList.ToArray()); newRecord = " You got a new record!"; } } else { TextTools.SaveRecords(new int[] { Score }); newRecord = " You got a new record!"; } MessageBox.Show($"You finished the game with a score of: {Score}." + newRecord, "Congratulations!"); Threading.Invoke(() => { NavigationWindow mainWindow = Application.Current.MainWindow as NavigationWindow; mainWindow.NavigationService.Navigate(new StartView()); }); }
/// <summary> /// Thread function to show the logo during a seconds and automatically navigate to the StartWindow. /// </summary> /// <remarks> /// We need to use the Dispatcher because from this thread we can't manage UI thread objects. /// This thread doesn't really shows the logo, as it appears when the page is rendered, but lets the user /// see it during a few seconds before starting the game. /// </remarks> private void SleepAndNavigate() { Thread.Sleep(2000); Threading.Invoke(() => { NavigationService?.Navigate(new StartView()); }); }