diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 0cea426..60d1ec9 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,7 +4,7 @@ { "label": "build only", "type": "shell", - "command": "sampctl package build", + "command": "sampctl build", "group": { "kind": "build", "isDefault": true @@ -19,7 +19,7 @@ { "label": "build watcher", "type": "shell", - "command": "sampctl package build --watch", + "command": "sampctl build --watch", "group": "build", "isBackground": true, "presentation": { @@ -31,7 +31,7 @@ { "label": "run tests", "type": "shell", - "command": "sampctl package run", + "command": "sampctl run", "group": { "kind": "test", "isDefault": true @@ -46,7 +46,7 @@ { "label": "run tests watcher", "type": "shell", - "command": "sampctl package run --watch", + "command": "sampctl run --watch", "group": "test", "isBackground": true, "presentation": { diff --git a/array_util.inc b/array_util.inc index 9902cda..3d37bca 100644 --- a/array_util.inc +++ b/array_util.inc @@ -169,5 +169,26 @@ stock array_sort(array[], left = 0, right = sizeof array) ArraySort(array, left, right); } +/** + * + * Shuffles an array, using the Fisher-Yates algorithm. + * + * The array to shuffle. + * Size of the array. + * + * Caution! Do not pass a string into this function. Doing so will corrupt it + * because the NUL terminator is moved to a different position in the array. + * + */ +stock ArrayShuffle(array[], size = sizeof array) +{ + for (new i = size - 1; i > 0; i--) + { + new j = random(i + 1); + new tmp = array[j]; + array[j] = array[i]; + array[i] = tmp; + } +} // ----------------------------------------------------------------------------- // EOF diff --git a/test.pwn b/test.pwn index dd6ebeb..111b2b2 100644 --- a/test.pwn +++ b/test.pwn @@ -7,6 +7,7 @@ main() TestUnshift(); TestInArray(); TestSort(); + TestShuffle(); } TestShift() @@ -78,6 +79,22 @@ TestSort() PrintArray(arrayToSort); } +TestShuffle() +{ + printf("\nArrayShuffle"); + printf("------------"); + + new arrayToShuffle[] = {13, 92, 85, 64, 17, 55, 33, 60, 58, 95}; + + printf("before shuffle:"); + PrintArray(arrayToShuffle); + + ArrayShuffle(arrayToShuffle); + + printf("after sort:"); + PrintArray(arrayToShuffle); +} + PrintArray(const arr[], size = sizeof arr) { new output[128];