Exemplo n.º 1
0
        /*
         * This method adds a new score to the array.
         * Then it orders the array so that all the scores are at the top.
         */
        public void addScore(string player1, string player2, string score)
        {
            Score toAdd = new Score(player1, player2, score);
            if (toAdd.getDiff() == 0) return;
            for (int i = 0; i < scores.Length; i++)
            {
                // If any of the scores is null, we add the new score there and then return.
                // Then we order the array by the scores we have in the array.
                // This makes sure that there are no errors when trying to order, in case there were any `null` values to order by.
                if (scores[i] == null)
                {
                    scores[i] = toAdd;
                    sortScoreArray(scores, 0, i);
                    return;
                }
            }
            // If the score we're trying to add is greater than the last score (which is the smallest) then we add it.
            if (toAdd.getDiff() > scores[scores.Length-1].getDiff())
            {
                scores[scores.Length-1] = toAdd;
            }

            sortScoreArray(scores, 0, scores.Length-1);
        }
Exemplo n.º 2
0
 /*
  * This method is used to sort the array.
  * Together with `half` it implements a quicksort.
  */
 private void sortScoreArray(Score[] arr, int left, int right)
 {
     int index = half(arr, left, right);
     if (left < index - 1)
         sortScoreArray(arr, left, index - 1);
     if (index < right)
         sortScoreArray(arr, index, right);
 }
Exemplo n.º 3
0
        /*
         * Gets the halfpoint of the array and performs substitutions.
         * Essentially the implementation of the quicksort.
         */
        private int half(Score[] arr, int left, int right)
        {
            int i = left, j = right;
            Score tmp;
            Score pivot = arr[(left + right) / 2];

            while (i <= j)
            {
                while (arr[i].getDiff() > pivot.getDiff())
                {
                    i++;
                }
                while (arr[j].getDiff() < pivot.getDiff())
                {
                    j--;
                }
                if (i <= j)
                {
                    tmp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = tmp;
                    i++;
                    j--;
                }
            }

            return i;
        }