This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| new:array [2018/02/20 22:06] – korshun | new:array [2018/02/21 11:57] (current) – korshun | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| <note warning> | <note warning> | ||
| + | |||
| + | Array sorting and shuffling functions use script names as callbacks. | ||
| ===== SortArray ===== | ===== SortArray ===== | ||
| Line 13: | Line 15: | ||
| The sorted array may actually be an array of objects with multiple fields. '' | The sorted array may actually be an array of objects with multiple fields. '' | ||
| + | |||
| + | <note important>'' | ||
| Example: sorting a simple array of numbers: | Example: sorting a simple array of numbers: | ||
| Line 40: | Line 44: | ||
| </ | </ | ||
| - | Example: making a scoreboard sorted by score: | + | Example: making a scoreboard sorted by score (uses [[swap|swap()]] for readability): |
| < | < | ||
| Line 73: | Line 77: | ||
| function void BuildScoreboard(void) | function void BuildScoreboard(void) | ||
| { | { | ||
| + | // Clear scoreboard | ||
| + | NumPlayers = 0; | ||
| + | |||
| // Fill scoreboard without sorting | // Fill scoreboard without sorting | ||
| for (int player = 0; player < MAX_PLAYERS; | for (int player = 0; player < MAX_PLAYERS; | ||
| Line 87: | Line 94: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | ===== ShuffleArray ===== | ||
| + | |||
| + | '' | ||
| + | |||
| + | Randomly reorders elements of an array with indices in range [begin, end). '' | ||
| + | |||
| + | Example: | ||
| + | |||
| + | < | ||
| + | int array[10]; | ||
| + | |||
| + | script " | ||
| + | { | ||
| + | int temp = array[a]; | ||
| + | array[a] = array[b]; | ||
| + | array[b] = temp; | ||
| + | } | ||
| + | |||
| + | function void ShuffleTheArray(void) | ||
| + | { | ||
| + | ShuffleArray(0, | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||