- Home /
Add to High Score Array
I have an array with the top 10 high scores. I know how to add to the array, and sort the array with Sort(); but how do you add a value to the array, lets say that is the 2nd highest score, so I want to add this new 2nd place high score, deleting the current 10th high score and moving the other scores down one place?
Cheers for any ideas
Well, i would just put them in the array in any order, and then sort the list by the highest value. Infact i would use a generic list.
Arrays differ in unity greatly between c# and js, which are you using?
Answer by whebert · Apr 05, 2013 at 02:52 AM
Use generic lists instead. With lists you can Add, Insert, RemoveAt, etc.
C#
using System.Collections.Generic
List<int> scores = new List<int>();
scores.Add(25);
scores.Add(50);
scores.Insert(0, 28);
scores.Sort();
scores.RemoveAt(0);
javascript
import System.Collections.Generic;
var scores : List.<int> = new List.<int>();
scores.Add(25);
scores.Add(50);
scores.Insert(0, 28);
scores.Sort();
scores.RemoveAt(0);
Answer by Chronos-L · Apr 05, 2013 at 03:10 AM
This is a very basic programming exercise.
C#
int [] highScore = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int InsertScore( int score ) {
int i = 0;
//Look for the index to insert score
while( i < highScore.length ) {
if( highScore[i] <= score ) {
break;
}
i++;
}
//Score doesn't make it to top 10
if( i >= highScore.length ) {
return -1;
}
//Push all the scores not higher than score backward
for( int j = highScore.length - 1; j <= i; --j ) {
highScore[j] = highScore[j-1];
}
//Set score
highScore[i] = score;
return i;
}
Answer by fhxgkgdkdjkfzk · Jun 26, 2016 at 06:02 AM
Correction for Chronos-L Answer. [JS version ,, but I guess its correct in C# too].
function recordHighScore(score) {
var i = 0;
while(i < highScores.length) {
if(highScores[i] <= score)
break;
i++;
}
if(i >= highScores.length)
return;
for(var j = highScores.length - 1; j > i; --j) {
console.log(highScores[j - 1]);
highScores[j] = highScores[j - 1];
}
highScores[i] = score;
}
Answer by fhxgkgdkdjkfzk · Jun 26, 2016 at 06:02 AM
Correction for Chronos-L Answer. [JS version ,, but I guess its correct in C# too].
function recordHighScore(score) {
var i = 0;
while(i < highScores.length) {
if(highScores[i] <= score)
break;
i++;
}
if(i >= highScores.length)
return;
for(var j = highScores.length - 1; j > i; --j) {
console.log(highScores[j - 1]);
highScores[j] = highScores[j - 1];
}
highScores[i] = score;
}
Answer by fhxgkgdkdjkfzk · Jun 26, 2016 at 06:02 AM
function recordHighScore(score) {
var i = 0;
while(i < highScores.length) {
if(highScores[i] <= score)
break;
i++;
}
if(i >= highScores.length)
return;
for(var j = highScores.length - 1; j > i; --j) {
console.log(highScores[j - 1]);
highScores[j] = highScores[j - 1];
}
highScores[i] = score;
}
Your answer
Follow this Question
Related Questions
Enemy Wave 1 Answer
Playing multiple sounds on same key input ("e") 1 Answer
Can't add GameObjects to ArrayList 1 Answer
Adding an object to an array of custom objects (JS) 1 Answer
Array of GameObjects - JS 1 Answer