Advertisement
Guest User

scorePlayerColorSwap.cs

a guest
Apr 8th, 2020
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class sortTest : MonoBehaviour
  7. {
  8.     public Text[] scoreUI;
  9.  
  10.     public Color[] colors = { Color.black, Color.blue, Color.red, Color.green };
  11.  
  12.     List<int> score = new List<int> { 10, 20, 50, 30 };
  13.     // 10, 20, 30, 50
  14.     List<int> playerOrder = new List<int> { 0, 1, 2, 3 };
  15.     // 0, 1, 3, 2
  16.     private void Start()
  17.     {
  18.         bubbleSort(score);
  19.         foreach (int pscore in score)
  20.         {
  21.             print("score: " + pscore);
  22.         }
  23.  
  24.         foreach (int player in playerOrder)
  25.         {
  26.  
  27.             print("player: " + player);
  28.         }
  29.  
  30.         for (int i = 0; i < scoreUI.Length; i++)
  31.         {
  32.             scoreUI[i].text = "Player" + playerOrder[i] + " - " + score[i];
  33.             scoreUI[i].color = colors[i];
  34.         }
  35.     }
  36.  
  37.     void bubbleSort(List<int> arr)
  38.     {
  39.         int n = arr.Count;
  40.         for (int i = 0; i < n - 1; i++)
  41.             for (int j = 0; j < n - i - 1; j++)
  42.                 if (arr[j] > arr[j + 1])
  43.                 {
  44.                     // swap scores
  45.                     int temp = arr[j];
  46.                     arr[j] = arr[j + 1];
  47.                     arr[j + 1] = temp;
  48.  
  49.                     // swap the player order
  50.                     int ptemp = playerOrder[j];
  51.                     playerOrder[j] = playerOrder[j + 1];
  52.                     playerOrder[j + 1] = ptemp;
  53.  
  54.                     // swap the color order
  55.                     Color ctemp = colors[j];
  56.                     colors[j] = colors[j + 1];
  57.                     colors[j + 1] = ctemp;
  58.                 }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement