Game Development Community

Little quiz for iTorque and T2D users

by Petr Vodak · in iTorque 2D · 10/21/2011 (7:31 am) · 9 replies

Hello!

I have a little quiz for you today. It's really simple to solve.
I have decided to share it with the whole community because Im on really hard way of porting our TGB 1.7.5 game to iTorque 1.5 which is REALLY frustrating process. I will share more things in the future (as I have solved plenty of issues and I have still a lot more to solve) and Im pretty sure that there is a lot of users that would find this useful.

I presume you want to know the quiz question? Well, here it is:

I have function:
function getRandomPermutation(%numberOfElements)
{
	%out = "";
    %counter = 0;

	for(%i = 0; %i < %numberOfElements; %i++)
    {
        %element = getRandomMod(0, %numberOfElements - 1, %out);
		%out = %out SPC %element;
        %counter++;
    }

    echo(%counter);
	return ltrim(%out);
}


And this second function:
function getRandomMod(%min, %max, %forbiddenValues)
{
	%fv = "";
	for(%i=0;%i<getWordCount(%forbiddenValues);%i++)
	{
		%s = getWord(%forbiddenValues,%i);
		%b = 1;
		for(%j=0;%j<getWordCount(%fv);%j++)
			if(getWord(%fv,%j) $= %s) {
				%b = 0;
				break;
			}
		if(%b && %s <= %max && %s >= %min)
			%fv = %fv SPC %s;
	}

	%fv = ltrim(%fv);
	%n = getWordCount(%fv);


	if(%max-%min+1 < %n)
	{
		//echo("getRandomMod - all values are forbidden, returning 0");
		return 0;
	}

	%av = "";
	for(%i=%min;%i<=%max;%i++)
	{
		%b = 1;
		for(%j=0;%j<%n;%j++)
			if(getWord(%fv,%j) == %i){
				%b = 0;
				break;
			}
		if(%b)
			%av = %av SPC %i;
	}
	%av = ltrim(%av);

	%r = getRandom(0,getWordCount(%av)-1);
	return getWord(%av,%r);
}

First function is called in init function of engine:
getRandomPermutation(8);


Do you expect hard question? Not at all.
What is echoed with echo(%count) in the first function when script is launched with T2D 1.7.5 engine and when with brand new shiny iTorque 1.5 engine?

User with first right answer will receive mac (or maybe ipad, if we gonna be lucky enough :)) version of our upcoming game.

#1
10/21/2011 (1:39 pm)
Seeing as there is no other data (input), it should give you 0.
#2
10/21/2011 (1:44 pm)
like.
#3
10/21/2011 (1:57 pm)
@David

Thank you and sorry, thats not the point. But you get the free copy anyway ;).
I added this to my original post:

First function is called in init function of engine:
getRandomPermutation(8);

Everything else is irrelevant :).
#4
10/28/2011 (8:17 am)
It's only going to be 8. What am I missing?

I tend to use this question a lot while interviewing... I like this solution better (I usually pass an array in so you wouldn't have a double for loop - still O(n) solution though)

function getRandomPermutation( %size ) {
  // if you're not passing in the array, make it
  for ( %i=0; %i<%size; %i++ ) {
    %array[%i] = %i;
  }

  for ( %i=0; %i<%size; %i++ ) {
    %p1 = getRandom( 0, %size );
    %p2 = getRandom( 0, %size );
    %tmp = %array[ %p1 ];
    %array[ %p1 ] = %array[ %p2 ];
    %array[ %p2 ] = %tmp;
  }

  return %array;
}
#5
10/28/2011 (9:33 am)
Thank you for your answer.

The point is, that in iTorque 1.5 is huge bug (reported properly here): www.garagegames.com/community/forums/viewthread/128264

but no one cares which seems to be a bit odd.

The right answer would be 8 indeed. For TGB 1.7.5 is this correct.
But in iTorque 1.5, it's just 1.

I call function getRandomMod in the first loop (in getRandomPermutation function) and I use %i there as local variable again, for some strange reason (i call it huge bug until someone explains me), %i doesn't reset and count up, so the first for cycle loops only once.

As we are porting our game from TGB to iTorque, this bug totally stops all our efforts (as this occur on a lot of places in our code), so hopefully your reaction will help to move things a bit... :)
#6
10/29/2011 (2:14 pm)
Really?

I ran it in iTorque1.5 and I got 8. Seems like you might have something else going on there...
#7
11/01/2011 (4:11 pm)
@Joe Interesting. Did you run it on MAC or WIN?
#8
11/02/2011 (10:18 pm)
Hey Petr,

I ran this on a Mac running Lion.
#9
11/04/2011 (5:01 pm)
@Joe Thanks, well, me too.

I went through my code and found this piece suspicious:
for(%i = 1; %i <= $LETTER_TYPES_COUNT; %i++)
{
	$DEFAULT_REWARD_DATA[$REWARDTYPE_LETTER, %i,"p_base"] = 0.1; //%i/100;
	$DEFAULT_REWARD_DATA[$REWARDTYPE_LETTER, %i,"max"] = 2;
}

This code is not in any fuction and is placed almost alone in some.cs file that is processed with exec(some.cs).
When I change local variable %i to anything else that differs from local variable used in getRandomPermutation or getRandomMod functions, than i get 8. Otherwise I get 1. What's wrong with this?