.

  • VC:MP Beta Tester
  • .
  • Posts: 1,808
Scripting challenges!
« on June 10th, 2015, 11:13 AM »Last edited on June 14th, 2015, 08:06 AM
I decided to be a troll and have some fun with scripters by testing their creativity. So, I will post some challenges and let's see if you can do complete them and how creative is your approach. I will choose the most creative one's based on how elegant the implementation is. Sux we do't have a spoiler tag on the forum :D



RULES

  • >STAFF MEMBERS EXCLUDED! WE ALL KNOW YOU CAN READ A DOCUMENTATION UNLIKE THE USUAL SCRIPTER EVERYONE IS IS FREE TO JOIN!
  • >DON'T POST THE CODE HERE. POST IT ON PASTEBIN OR SOMETHING SIMILAR AND POST THE LINK HERE. POST THE CODE HERE AND USE THE SPOILER TAG
  • >YOU ARE NOT ALLOWED TO LOOK AT SOMEONE ELSE'S  CODE. IF YOU DO THAT THEN YOU'RE DISQUALIFIED AND YOU DON'T DESERVE TO CALL YOURSELF A SCRIPTER.
  • >BE HONEST WITH YOUR IMPLEMENTATION. IF YOU COPY SOMEONE ELSE'S IMPLEMENTATION THEN GO KILL YOUR SELF.
  • >IF YOUR IMPLEMENTATION HAPPENS TO MATCH SOMEONE ELSE'S CODE AND YOU DIDN'T LOOKED AT HIS CODE. THAT'S FINE.
  • >>HAVE PHUN AND TRY TO LEARN SOMETHING.
  • >>>MUST HANDLE CRITICISM!!!

(click to show/hide)
The spoiler tag can be used with  [-spoiler] ... [-/spoiler] (without the '-'). Example of adding code in spoiler tags:

[-spoiler]
 [-code]
  print("This code should be hidden!");
 [-/code]
[-/spoiler]

The '-' is added to allow you to see the tags. Otherwise would be treated as a tag and would be converted to HTML. You don't need he '-'.




CHALLENGES

  • PRINT ALL THE VALUES OF AN ARRAY WITHOUT USING ANY LOOP CONTROL STATEMENTS. (Winner: @Stormeus )
  • IMPLEMENT A TIC-TAC-TOE GAME USING SPRITES THAT CAN BE PLAYED BETWEEN TWO PLAYERS AND SUPPORTS BETTING (Winner: ONGOING... )



ATTENTION: A single post per application is allowed to avoid spam. If you feel like you want to update your code then edit the post and add a [-hr] (without the '-') tag at the bottom and with a bold text saying UPDATE: following the update...

ATTENTION: If your post ends up rather too large. Consider using an external source for the code. The external source must be trustworthy (like pastebin.com) and the source host must be permanent.



LET THE GAMES BEGIN!
.

Stormeus

  • VC:MP Developer
  • oh god how did i get here im not good w computer
  • Posts: 1,128
Re: Scripting challenges!
« Reply #1, on June 10th, 2015, 11:40 AM »
Quote from S.L.C on June 10th, 2015, 11:13 AM
RULES:
  • >STAFF MEMBERS EXCLUDED! WE ALL KNOW YOU CAN READ A DOCUMENTATION UNLIKE THE USUAL SCRIPTER
Damn.

(click to show/hide)
Also we do have a spoiler tag

.

  • VC:MP Beta Tester
  • .
  • Posts: 1,808
Re: Scripting challenges!
« Reply #2, on June 10th, 2015, 11:55 AM »
Quote from stormeus on June 10th, 2015, 11:40 AM
Damn.
You are free to participate but would be unfair to the other scripters ;D I tried that spoiler tag a while a go and didn't work. Perhaps it was added in later updates.
.

EK.IceFlake

  • Hero Member
  • "We are the champions my fellahs" - Sufyan/VK.SuFy
  • Posts: 1,757
Re: Scripting challenges!
« Reply #3, on June 10th, 2015, 12:15 PM »Last edited on June 10th, 2015, 12:20 PM
This is a pretty horrible way but
(click to show/hide)
Code: [Select]
local array = ["This", "And", "That", "Why", "Not"];
if (array.len() <= 1) print(array[0]);
if (array.len() <= 2) print(array[1]);
if (array.len() <= 3) print(array[2]);
if (array.len() <= 4) print(array[3]);
if (array.len() <= 5) print(array[4]);
//And so on...

.

  • VC:MP Beta Tester
  • .
  • Posts: 1,808
.

EK.IceFlake

  • Hero Member
  • "We are the champions my fellahs" - Sufyan/VK.SuFy
  • Posts: 1,757

DizzasTeR

  • VC:MP Beta Tester
  • Infinite loops defy infinity by overflowing memory
  • Posts: 1,212
Re: Scripting challenges!
« Reply #6, on June 10th, 2015, 12:34 PM »
(click to show/hide)
Code: [Select]
// Print all the values of an array without using any loop

local array = [];

local _index1 = array.push( "a" );
local _index2 = array.push( "b" );
local _index3 = array.push( "c" );

print( "Array contains index " + _index1 );
print( "Array contains index " + _index2 );
print( "Array contains index " + _index3 );
print( "Lets hope SLC doesn't disagree with this" );
[-/code]
[-/spoiler]

.

  • VC:MP Beta Tester
  • .
  • Posts: 1,808
Re: Scripting challenges!
« Reply #7, on June 10th, 2015, 12:38 PM »Last edited on June 10th, 2015, 01:03 PM
Come on people. Is this the best you ca do. I already got two approaches while I was eating ;D

Hints (don't read!)
(click to show/hide)
Hint about the first approach: Recursive...
Hint about the second approach: Delegates...
.

Kratos_

  • Jr. Member
  • Mutaros Apotheoses
  • Posts: 86
Re: Scripting challenges!
« Reply #8, on June 10th, 2015, 12:58 PM »

** Kratos_ was returning to home . ( Just a 5 min drive to home )
** Kratos_ checked mobile . ( Thinks, hmm ok. Something to post )
** Kratos_ reached home & opened lappy . Writes some bunches of code .
** Kratos_ opened forum & boom all fun destroyed . ( S.L.C disclosed )

Btw here it is ,

(click to show/hide)
Code: [Select]
function onScriptLoad()
{
_test_();
}

// function for defining & printing array entities
function _test_(ctr=0)
{
// sample array holding the entities
local array = [ "mercury", "venus", "earth", "mars", "jupiter", "saturn", "uranus", "neptune" ];
// printing the array slot indexed at 'ctr'
print(array[ctr]);
// updating the counter by 1
++ctr;
// checking if the updated counter goes beyond the range of array slots ( 0 - n-1 )
if( ctr >= array.len() ) return 0;
// calling the function with updated counter [ Tail Recursion ]
_test_(ctr);

}
In the middle of chaos , lies opportunity.

.

  • VC:MP Beta Tester
  • .
  • Posts: 1,808
Re: Scripting challenges!
« Reply #9, on June 10th, 2015, 01:01 PM »Last edited on June 10th, 2015, 02:08 PM
Quote from Kratos_ on June 10th, 2015, 12:58 PM
** Kratos_ opened forum & boom all fun destroyed . ( S.L.C disclosed )
I haven't fully disclosed :D Was just a single word hint. And besides, you did the code before you saw the hint :P Waiting for more approaches. I'll do a review tonight when the first challenge ends. So far you have the approach that works. The only thing that's bugging me the fact that you declare that array on each recursion.

If you want to have fun keep looking for the other approaches ;D
.

vcmptr

  • Jr. Member
  • This user novice scripter.
  • Posts: 60
Re: Scripting challenges!
« Reply #10, on June 10th, 2015, 02:08 PM »
(click to show/hide)
function PrintArray(array, num=0)
{
print(array[num]);
num+=1;
if(array.len() != num) PrintArray(array, num);
}
My English is not good.

.

  • VC:MP Beta Tester
  • .
  • Posts: 1,808
Re: Scripting challenges!
« Reply #11, on June 10th, 2015, 02:25 PM »Last edited on June 10th, 2015, 02:46 PM
Kudos to @vcmptr for having a clean approach. Here's another stupid way to get it all in one string with the format function. This isn't recommended and there's better ways :D

(click to show/hide)
Code: [Select]
local arr = [2, 354, "foo", 43, 5.234, 675, "bar", true, {a = 23, b = 34.546}];

// Nethod 3 --------------------------------------------------------

function VarargsApproach(arr)
{
    // Insert the format string in the argument pack
    arr.insert(0, "");

    // Generate the format string
    local fmt = function(arr, idx = 0)
    {
        if (idx < arr.len())
        {
            switch (typeof arr[idx])
            {
                case "integer": arr[0] += "%d\n"; break;
                case "float": arr[0] += "%f\n"; break;
                case "string": arr[0] += "%s\n"; break;
                default:
                    arr[0] += "%s\n";
                    try {
                        arr[idx] = arr[idx].tostring();
                    } catch (e) {
                        arr[idx] = "(null : 0x00000000)";
                    }
                break;
            }
            // Go down the rabbit hole
            callee().call(this, arr, ++idx);
        }
    }

    // Skip the format string
    fmt(arr, 1);

    // Insert the 'this' environment
    arr.insert(0, this);

    // Print the f*er
    ::print(::format.acall(arr));
}

// We make a shallow copy of the array
//  Otherwise we edit the original
VarargsApproach(clone arr);

I'm just curious to see what other methods I can get.
.

EK.IceFlake

  • Hero Member
  • "We are the champions my fellahs" - Sufyan/VK.SuFy
  • Posts: 1,757
Re: Scripting challenges!
« Reply #12, on June 10th, 2015, 04:33 PM »
Well you said that we cannot use loops but said absolutely nothing about using timers, so here it is!
(click to show/hide)
Code: [Select]
function onScriptLoad()
{
local array = ["once", "upon", "a", "time", "Cinderella", "was", "going", "somewhere"];
loopn <- 0;
NewTimer("PrintAllOfThese", 1, array.len(), array);
}
function PrintAllOfThese(array)
{
print(array[loopn]);
loopn++;
}

.

  • VC:MP Beta Tester
  • .
  • Posts: 1,808
Re: Scripting challenges!
« Reply #13, on June 10th, 2015, 04:41 PM »Last edited on June 10th, 2015, 04:48 PM
Quote from NE.CrystalBlue on June 10th, 2015, 04:33 PM
Well you said that we cannot use loops but said absolutely nothing about using timers, so here it is!
I only said loops are forbidden. Any other trick is yours to use :D BTW, I think your code needs the new timers to work. Which are not yet compiled.

We have recursive, we have timers, we have format. I'm still waiting for someone to find the last approach :D

The first challenge ends in about 2 hours. Impress me :P
.

EK.IceFlake

  • Hero Member
  • "We are the champions my fellahs" - Sufyan/VK.SuFy
  • Posts: 1,757
Re: Scripting challenges!
« Reply #14, on June 10th, 2015, 05:00 PM »Last edited on June 10th, 2015, 05:02 PM
Got a newer one it's much more efficient (hint it uses the function arraytostring anti-hint I made that function myself)
(click to show/hide)
Code: [Select]
local ra = ["Cinderella", "ate", "nuttella", "then", "blah"];
print(arraytostring(ra, ", "));
And that function
Code: [Select]
function arraytostring(xarray, xsplitter, xstarting=0, xcurrent="")
{
if (xstarting >= xarray.len()) return xcurrent;
else return (arraytostring(xarray, xsplitter, xstarting+1, xcurrent + ((xstarting == 0) ? "" : xsplitter) + xarray[xstarting]));
}