• entries
    1212
  • comments
    1738
  • views
    1140399

Still working

Sign in to follow this  
johnhattan

93 views

Still working on the Shi Sen daily puzzle. Toughest part to do is the search to check for valid moves. Thankfully, I already wrote that in C++ a buncha years ago for my C++ version.

C++ to ActionScript conversion is both easy and hard. And, interestingly, it's exactly the opposite of what I'd expect. In the past when I had to convert C++ to something else (like Pascal or some high-level database language), the bugaboos popped up with the nasty little bit-fiddling operations that C++ handles beautifully but higher-level languages consider to be the realm of nerdy assembly-monks cloistered in towers on the Microsoft campus. So converting something like this. . .

X = (Y & 0x000f) << (B ? 1 : 2);

(which means "Mask off all but the last four bits of Y, then shift it either one or two bits depending on whether B is true, then store the whole mess in X")


. . .into something that just didn't handle bits or non-decimal bases was just nasty and often required the whole thing to be stretched out over several lines, maybe with a few temp variables thrown in.

Thankfully, ActionScript (i.e. ECMAScript) was hoped to be a scripting language with which engineers could hack together solutions easily, so it handles the above bit-fiddling just fine.

The problem comes with the "big picture" stuff like variable scoping. ECMAScript variables (unlike C++ variables) are static by default. Nonstatic variables must be declared by the nonintuitive "var" declaration.

myFunc = function(x)
{
y = x;
var otherY = x;
}

// now call it
myFunc(3);

// and see what we have
trace(y);
trace(otherY);

The output will be:

3
undefined

Unlike C++, y is now the caller's namespace rather than disappearing when the function goes out of scope. If you want your variable to go out of scope, you must "var" it.

It's just something to watch for. Especially when you're making a recursive function, like my Shi Sen search. You WANT your variables to stack themselves and dutifully go out of scope when the function completes, so plan to watch your scope when converting.

That is all for now.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now