private void btnAddToHighscoreList_Click(object sender, RoutedEventArgs e)
        {
            int newIndex = 0;

            if ((this.HighscoreList.Count > 0) && (CurrentScore < this.HighscoreList.Max(x => x.Score)))
            {
                SnakeHighscore justAbove = this.HighscoreList.OrderByDescending(x => x.Score).First(x => x.Score >= CurrentScore);
                if (justAbove != null)
                {
                    newIndex = this.HighscoreList.IndexOf(justAbove) + 1;
                }
            }
            this.HighscoreList.Insert(newIndex, new SnakeHighscore()
            {
                PlayerName = txtPlayerName.Text,
                Score      = CurrentScore
            });
            while (this.HighscoreList.Count > MAX_HIGHSCORE_LIST_ENTRY_COUNT)
            {
                this.HighscoreList.RemoveAt(MAX_HIGHSCORE_LIST_ENTRY_COUNT);
            }

            SaveHighscoreList();
            bdrNewHighscore.Visibility  = Visibility.Collapsed;
            bdrHighscoreList.Visibility = Visibility.Visible;
        }
Beispiel #2
0
        /// <summary>
        /// Add new entry to highscore list from the New Highscore screen
        /// </summary>
        private void AddToHighscoreList()
        {
            // Figure out where new entry should be inserted
            int newIndex = 0;

            if (HighscoreList.Count == 0)
            {
                // List is empty and new entry is the only highscore
                newIndex = 0;
            }
            else if (currentScore <= HighscoreList.Min(x => x.Score))
            {
                // List is not full and new entry is the lowest highscore
                newIndex = HighscoreList.Count;
            }
            else
            {
                // List can be traversed for the correct spot
                SnakeHighscore current = HighscoreList.First(x => x.Score < currentScore);
                if (current != null)
                {
                    newIndex = HighscoreList.IndexOf(current);
                }
            }

            // Create and insert the new entry
            HighscoreList.Insert(newIndex, new SnakeHighscore()
            {
                PlayerName = txtPlayerName.Text,
                Score      = currentScore
            });

            // Make sure number of entries does not exceed maximum
            while (HighscoreList.Count > MaxHighscoreListEntryCount)
            {
                HighscoreList.RemoveAt(MaxHighscoreListEntryCount);
            }

            fileReader.SaveHighscoreList(HighscoreList);

            bdrNewHighScore.Visibility  = Visibility.Collapsed;
            bdrHighscoreList.Visibility = Visibility.Visible;

            txtPlayerName.Clear();
        }