private static int CompareHighScores(HighScoreEntry x, HighScoreEntry y) { if (x == null) { if (y == null) { // If x is null and y is null, they're equal. return 0; } else { // If x is null and y is not null, y is greater. return -1; } } // If x is not null... else { // ...and y is null, x is greater. if (y == null) { return 1; } // ...and y is not null, compare the sum of score and time. else { int xValue = x.DurationSeconds + x.FinalScore; int yValue = y.DurationSeconds + y.FinalScore; return xValue.CompareTo(yValue); } } }
private void SaveHallOfFameEntry() { int score = m_gameDurationSeconds; int scoreAdditions = 0; int previousBestTime = m_gamePlayer.BestTime(m_gameDifficulty); int previousGamesPayed = m_gamePlayer.NumberOfGamesPlayed(m_gameDifficulty); float newAverageTime = 0; float previousAverageTime = m_gamePlayer.AverageTime(m_gameDifficulty); string message = string.Empty; foreach (GameHelper helper in m_gameHelpers) { scoreAdditions += m_gameDurationSeconds * ((int)helper / 100); } int finalScore = score + scoreAdditions; if (m_gameDifficulty != Difficulty.Unknown && m_gameDifficulty != Difficulty.Restarting && m_gameDifficulty != Difficulty.UserSupplied) { if (m_gameDurationSeconds < previousBestTime || previousBestTime == 0) { m_gamePlayer.SetBestTime(m_gameDifficulty, m_gameDurationSeconds); } if (previousGamesPayed == 0) { newAverageTime = m_gameDurationSeconds; m_gamePlayer.SetNumberOfGamesPlayed(m_gameDifficulty, 1); m_gamePlayer.SetAverageTime(m_gameDifficulty, m_gameDurationSeconds); } else { newAverageTime = ((previousAverageTime * previousGamesPayed) + m_gameDurationSeconds) / (previousGamesPayed + 1); m_gamePlayer.SetAverageTime(m_gameDifficulty, newAverageTime); m_gamePlayer.SetNumberOfGamesPlayed(m_gameDifficulty, previousGamesPayed + 1); } } HighScoreEntry newHighScoreEntry = new HighScoreEntry(); newHighScoreEntry.GameTime = DateTime.Now; newHighScoreEntry.PlayerName = m_gamePlayer.PlayerName; newHighScoreEntry.GameDifficulty = m_gameDifficulty; newHighScoreEntry.DurationSeconds = m_gameDurationSeconds; newHighScoreEntry.FinalScore = finalScore; if(m_highScores.ContainsKey(m_gameDifficulty)) { m_highScores[m_gameDifficulty].Add(newHighScoreEntry); } else { List<HighScoreEntry> highSocreList = new List<HighScoreEntry>(10); highSocreList.Add(newHighScoreEntry); m_highScores.Add(m_gameDifficulty, highSocreList); } WriteHighScores(); message = string.Format("Congratulations, you have solved the puzzle in {0} with a score of {1}.", GetTimeText(m_gameDurationSeconds), finalScore.ToString()); if (m_gameDurationSeconds < previousBestTime) { message += string.Format("\r\nBeating your previous best time of {0}.", GetTimeText(previousBestTime)); } if (newAverageTime < previousAverageTime) { message += string.Format("\r\nImproving your average time from {0} to {1}.", GetTimeText((int)previousAverageTime), GetTimeText((int)newAverageTime)); } else { message += string.Format("\r\nYour average time is {0}.", GetTimeText((int)newAverageTime)); } MessageBox.Show(message); }