Scripting challenges!

Started by ., Jun 10, 2015, 10:13 AM

Previous topic - Next topic

.

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!!!

[spoiler]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 '-'.

[/spoiler]



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

Quote from: S.L.C on Jun 10, 2015, 10:13 AMRULES:
  • >STAFF MEMBERS EXCLUDED! WE ALL KNOW YOU CAN READ A DOCUMENTATION UNLIKE THE USUAL SCRIPTER

Damn.

[spoiler]Also we do have a spoiler tag[/spoiler]

.

Quote from: stormeus on Jun 10, 2015, 10:40 AMDamn.

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

#3
This is a pretty horrible way but
[spoiler]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...
[/spoiler]

.

.

EK.IceFlake


DizzasTeR

[spoiler]
// 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]

.

#7
Come on people. Is this the best you ca do. I already got two approaches while I was eating ;D

Hints (don't read!)
[spoiler]Hint about the first approach: Recursive...
Hint about the second approach: Delegates...[/spoiler]
.

Kratos_


** 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 ,

[spoiler]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);

}
[/spoiler]
In the middle of chaos , lies opportunity.

.

#9
Quote from: Kratos_ on Jun 10, 2015, 11:58 AM** 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

[spoiler]function PrintArray(array, num=0)
{
print(array[num]);
num+=1;
if(array.len() != num) PrintArray(array, num);
}[/spoiler]
My English is not good.

.

#11
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

[spoiler]
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);
[/spoiler]

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

EK.IceFlake

Well you said that we cannot use loops but said absolutely nothing about using timers, so here it is!
[spoiler]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++;
}
[/spoiler]

.

#13
Quote from: NE.CrystalBlue on Jun 10, 2015, 03:33 PMWell 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

#14
Got a newer one it's much more efficient (hint it uses the function arraytostring anti-hint I made that function myself)
[spoiler]
local ra = ["Cinderella", "ate", "nuttella", "then", "blah"];
print(arraytostring(ra, ", "));
And that function
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]));
}
[/spoiler]