/// <summary> /// Adds new record to score table /// </summary> /// <param name="scoreRecord">ScoreRecord to add</param> /// <returns>Position where score was placed. Returns -1 when high score was not reached</returns> public int AddRecord(ScoreRecord scoreRecord) { int highScorePos = -1; // Find high score position to insert for (int i = 0; i < Table.Length; i++) { if (scoreRecord.Score > Table[i].Score) { highScorePos = i; break; } } // Move results down and insert new score if (highScorePos > -1) { for (int i = Table.Length - 2; i >= highScorePos; i--) { Table[i + 1] = Table[i]; } Table[highScorePos] = scoreRecord; } return highScorePos; }
/// <summary> /// Adds new record to score table /// </summary> /// <param name="scoreRecord">ScoreRecord to add</param> /// <returns>Position where score was placed. Returns -1 when high score was not reached</returns> public int AddRecord(ScoreRecord scoreRecord) { int highScorePos = -1; // Find high score position to insert for (int i = 0; i < Table.Length; i++) { if (scoreRecord.Score > Table[i].Score) { highScorePos = i; break; } } // Move results down and insert new score if (highScorePos > -1) { for (int i = Table.Length - 2; i >= highScorePos; i--) { Table[i + 1] = Table[i]; } Table[highScorePos] = scoreRecord; } return(highScorePos); }
/// <summary> /// Creates new high score table filled with zero results /// </summary> public HighScoreTable() { Table = new ScoreRecord[5]; for (int i = 0; i < Table.Length; i++) { Table[i] = new ScoreRecord(); Table[i].Score = 0; Table[i].Name = "AAA"; } }
/// <summary> /// Event handler for GameWindws close event /// </summary> /// <param name="sender">Sender object</param> /// <param name="gameStatistics">Game statistics</param> private void GameWindow_OnGameOver(object sender, GameStatistics gameStatistics) { ScoreRecord scoreRecord = new ScoreRecord(); scoreRecord.Score = gameStatistics.Score; // Add score into HighScore table int scoreIndex = parentApp.HighScore.AddRecord(scoreRecord); // Show high score window HighScoreWindow scoreWindow = new HighScoreWindow(parentApp); // if high score has been reached then edit name if (scoreIndex > -1) scoreWindow.EditItem(scoreIndex); }