c# - Array sorting efficiency... Beginner need advice -
i'll start saying beginner programmer, first real project outside of using learning material. i've been making 'simon says' style game (the game repeat pattern generated computer) using c# , xna, actual game complete , working fine while creating it, wanted create 'top 10' scoreboard. scoreboard record player name, level (how many 'rounds' they've completed) , combo (how many buttons presses got correct), scoreboard sorted combo score. led me xml, first time using it, , got point of having xml file recorded top 10 scores. xml file managed within scoreboard class, responsible adding new scores , sorting scores. gets me point... i'd feedback on way i've gone sorting score list , how have done better, have no other way gain feedback =(. know .net features array.sort() wasn't sure of how use it's not single array needs sorted. when new score needs entered scoreboard, player name , level have added. these stored within 'array of arrays' (10 = 'top 10' scores)
scoreboardcombodata = new int[10]; // combo scoreboardtextdata = new string[2][]; scoreboardtextdata[0] = new string[10]; // name scoreboardtextdata[1] = new string[10]; // level string
the scoreboard class works follows:
- checks see if 'scoreboard.xml' exists, if not creates it
- initialises above arrays , adds player data scoreboard.xml, previous run
- when addscore(name, level, combo) called sort begins
- method can called populates xml file above array data
the sort checks see if new score (combo) less or equal recorded scores within scoreboardcombodata array (if it's greater score, moves onto next element). if so, moves scores below score less or equal down 1 element, removing last score , places new score within element below score less or equal to. if score greater recorded scores, moves scores down 1 , inserts new score within first element. if it's score, adds first element. when new score added, name , level data added relevant arrays, in same way. tongue twister. below addscore method, i've added comments in hope makes things clearer o_o. can actual source file here. below method example of quickest way add score follow through debugger.
public static void addscore(string name, string level, int combo) { // if scoreboard has not yet been filled, adds 'active' // array element each time new score added. actual array size // defined within populatescoreboard() (set 10 - 'top 10' if (totalscores < scoreboardcombodata.length) totalscores++; // scoreboard need sorting? if (totalscores > 1) { (int = totalscores - 1; > - 1; i--) { // check see if score (combo) greater score stored in // array if (combo > scoreboardcombodata[i] && != 0) { // if continue next element continue; } // check see if score (combo) less or equal element 'i' // score && element not last in // array, if score not need added scoreboard else if (combo <= scoreboardcombodata[i] && != scoreboardcombodata.length - 1) { // if score lower element 'i' , greater last // element within array, needs added scoreboard. achieved // moving each element under element 'i' down element. new score inserted // array under element 'i' (int j = totalscores - 1; j > i; j--) { // name , level data moved down in relevant arrays scoreboardtextdata[0][j] = scoreboardtextdata[0][j - 1]; scoreboardtextdata[1][j] = scoreboardtextdata[1][j - 1]; // score (combo) data moved down in relevant array scoreboardcombodata[j] = scoreboardcombodata[j - 1]; } // new name, level , score (combo) data inserted relevant array under element 'i' scoreboardtextdata[0][i + 1] = name; scoreboardtextdata[1][i + 1] = level; scoreboardcombodata[i + 1] = combo; break; } // if method gets point, means score greater scores within // array , therefore cannot added in above way. not less score within // array. else if (i == 0) { // names, levels , scores moved down within relevant arrays (int j = totalscores - 1; j != 0; j--) { scoreboardtextdata[0][j] = scoreboardtextdata[0][j - 1]; scoreboardtextdata[1][j] = scoreboardtextdata[1][j - 1]; scoreboardcombodata[j] = scoreboardcombodata[j - 1]; } // new number 1 top name, level , score, added first element // within each of relevant arrays. scoreboardtextdata[0][0] = name; scoreboardtextdata[1][0] = level; scoreboardcombodata[0] = combo; break; } // if methods point, combo score not high enough // on top10 score list , therefore needs break break; } } // totalscores < 1, current score first added. therefore no checks need made // , name, level , combo data can entered directly first element of relevant // array. else { scoreboardtextdata[0][0] = name; scoreboardtextdata[1][0] = level; scoreboardcombodata[0] = combo; } } }
example adding score:
private static void initialize() { scoreboarddoc = new xmldocument(); if (!file.exists("scoreboard.xml")) generatexml("scoreboard.xml"); populatescoreboard("scoreboard.xml"); // add test scores here! addscore("example", "10", 100); addscore("example2", "24", 999); populatexml("scoreboard.xml"); }
in it's current state source file used testing, initialize called within main , populatescoreboard handles majority of other initialising, nothing else needed, except add test score.
i thank time!
as programming tip, should replace 3 different arrays single array of score objects, each of these objects have 3 properties mentioned (name, level , score). can create comparator-like function (based on score attribute) can used sort via arrays.sort()
method.
if want keep current 3 arrays can sorting algorithms here: http://en.wikipedia.org/wiki/sorting_algorithm#comparison_of_algorithms , make changes done 3 arrays simultaneously instead of 1 one (since keep data in them synchronized index).
Comments
Post a Comment