NoshBar's Dumping Ground

Once again I find myself building an effigy of my sorry self with the intent of beating it with a poo-covered hindsight stick.

I move to Sweden, finally get settled in, get my first ever iPhone, launch up the AppStore in Swedish and... BAM, two bridge building games in the top charts: Fat Birds and Bridge Constructor.

On the one hand I think "Oh well, I'd probably never have finished mine anyway", on the other hand I think "I can make a better one!", and on my other radioactive hand I think "CURSED OFFSPRING OF FEMALE CANINES!".

It's about time I admit to myself that I will never make a full game, finishing is haaard.

So I figured I'd refocus my efforts and give them a new name, putting a positive spin on things.

"Tutorial" = unfinished product that people appreciate.Hopefully.

If I ever finish it, I will be putting up a tutorial on how to code a simple bridge building game in baby C++ (classes, no STL, blah).

With it, I may as well make an attempt to describe how I personally manage using the same code-base for Windows/Linux/OSX/iOS/Android/WebOS/BlackBerry(QNX).

The worst that can happen is I finally feel like I did something worthwhile, right?

Rambly introductory

Until recently, I had no reason to code something in Python, as I had all my bases covered by C/C++, ObjectPascal, and PHP.Lately work has had me pottering with some utilities written in Python, and even though I don't "know" Python, I could read the code just fine (as I guess is the case with most coders), and it seemed like something that would be good for quick prototypes (especially as my main development machine is now a Mac - it comes with Python pre-installed).

Things I used

Having had some frustrations with getting an XMPP library and its dependencies set up and working with cross-platform C++ compilers, I was delighted to find SleekXMPP, which is easily installed by typing:

python setup.py install

I had Python 2.7 on OSX and Linux, and Python 3.2 on Windows.I figured this would let me target the two different versions (and -hey-, how different could they be? More on that later, SIGH).

As before, I used SQLite for the database, using the full-text-search capability it provides.This seems to be enabled in the OSX and Linux distributions I was using (10.7 and 11.10, respectively), but not on Windows Python 3.2.The easy fix is to go the SQLite website and download a new sqlite3.dll and plonk it in the Python32\DLL folder.Well, it WOULD be, but I guess the API has changed (or something, I didn't have the patience to check)... so I did what any impatient prototyper would have done (just me?) and uninstalled Python 3.2, and installed Python 2.7, replaced the DLL as before, and et voila, success!

For an editor/IDE, I initially just edited the code in Xcode, but that provided very little other than syntax highlighting.So then I tried SPE, which was great, I could debug code, set breakpoints, do all the things I'm used to with compiled code... buuut it would sometimes crash, or just act weirdly. Nothing deal-breakery, but my lust for shiny new things had to be met, and it was the perfect to excuse to look for MOOORE.I then used Aptana Studio 3, which has PyDev support built in, and worked pretty well (apart from the eye mangling default theme)... but still didn't feel quite right.

After much elimination and many crying supermodels, I finally chose Python Tools for Visual Studio... what can I say? I absolutely adore that IDE.All the shortcuts I know, incredibly quick stepping through code (with Python 2.7 anyway), easy process killing, warnings in the IDE about mismatched white-spacing... it just felt right for me.

The story so far...

Despite the warnings and dismissals from internet experts and people I know, my opinion is that Python doesn't suck too bad.I know my opinion is worth as much as the rest of the experts, but it boils down to this:Bad code is bad code, how bad it is depends on how much you let yourself abuse the available functionality.

This initial code for HelperMonkeyBot must be horrendous to seasoned (hmm, salty) Python coders, but it's a stepping stone for me, and one I am proud of.Not because I learnt a new language, not because I think it's great code, but simply because I didn't abuse it too much.

I think the thing I like most about Python is the same thing that has me liking Pascal/Delphi still:people who provide examples in Python/Pascal provide code that is understandable, and seemingly want to explain things more than C/++ coders.For years I would search for things like "avi player delphi" because the code would be easy to read, and there would normally be an accompanying article.The great thing is that Python seems way more popular than Delphi, so a whole new world of easy-to-understand code has opened up to me, huzzah!

So, soon I will provide an updated HelperMonkeyBot (I'm testing it at the moment) which -thanks to being in Python with some easy to use helper libraries- sends mail reminders.Not the world's most amazing feature, but considering the C code I'd have to do for that... YAY!

I had a dream. I had a dream about a black and white game where puppies are constantly falling from the sky, their shadows growing as they themselves grow nearer, and it is up to you to not be hit by a single one of them while traversing the level, using those shadows as a guide.

The problem is, when they splatter on the ground with a satisfying squealch, they make the surface slightly slippery, each compounding splatter making that area even more slippery... making it tougher to stop with precision and avoid the growing shadows above you.

The logo would be a spoof of PurpleRain, a movie with Prince in, one I never saw, but read all about in MAD magazine when I was young.

I have never made a "complete" game before, which is lame, and it's sad that it took a dream about being in a 48 hour programming competition, but at least I feel slightly inspired.

I keep claiming it's the worst written code on the planet... and recently someone asked if they could have the source to "play around with" (more likely it would have its way with them).

After being quite happy that I had lost the source a while back and could not prove that theory... well, curiosity got the better of me and I played with some undelete tools on an old hard-drive I had.

So here it is, the source code for my 15 minutes of fame, released under the "don't tell anyone I wrote it unless they think it's awesome" license.

I've been hesitant to release it in the past, because it is truly awful, from over a decade ago, before I could program, or even knew what the "Object" in Object-Pascal meant (which should be quite obvious).I'd hate for this ever to hurt my chances of anyone ever employing me based on this code, so I would just like to state that I am infinitely better now.Infinitely.But also, keep in mind that somehow I managed to keep working on this, understanding the mess, knowing exactly where in the bazillions of lines of code things happened... only a smart person could do that. Nod nod nod. Did I write smart? I meant to type "loony".

So anyway, use this at your own peril, and do NOT come asking for me help... especially not if it starts sucking your soul out...It used to compile with Delphi 6 Personal Edition, and somewhere along the line I managed to use the ... Delphi... Studio... something... I don't remember what it was called, all I remember is that it required .NET 1.1, was incredibly bloated and slow, and crashed more than TISFAT.

And some of the many things I learnt doing this:

Never put code directly in an event handler, make it call an objects function or something

$I including files in Delphi is not smart, it's evil, and made debugging almost impossible (back in Delphi 5 days at least)

Delphi objects... well, you don't need to allocate memory for them in the traditional malloc() way, nor do you need to use pointers to them... instance := object.Create() works juuust fine, and is a lovely way of doing things.

When designing a file format, do just that, design it! The amount of fudges in the TISFAT format just to cater for tiny variances in previous versions... geez.

/*
This simple program was thrown together to see if tossing thousands of sausages at lines can be used to calculate PI, described here:
http://www.wikihow.com/Calculate-Pi-by-Throwing-Frozen-Hot-Dogs
Basically you take a sausage of length N, draw a bunch of lines that are distance N apart, and throw some sausages towards the line.
For every throw, increment a throw counter TC, and for every hotdog that comes to rest crossing a line, increment a cross counter CC.
After you're tired of manhandling sausages, calculate PI by doing: pi = TC * (2 / CC)
There are many better ways to do this, this was simply the quickest way I knew to code it up for instant gratification.
For what it's worth, this had PI accurate to 6 digits after a few million iterations (and was no better off after a few trillion)
NOTE: It turns out that adjusting the hotdogGirth to be anything but 0.0f tosses the estimate/convergence out quite a bit.
so for now, we're throwing _really_ thin needles instead.
(instead of a width of 0, I guess you could make a really long and thin needle too)
*/
#define _USE_MATH_DEFINES //to calculate some things, and compare our answer to M_PI, we need to set this define, so that math.h exposes it
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <stdint.h> //for int64 support
int main(int argc, char *argv[])
{
uint64_t tosses = 0;//total number of tosses uint64_t crosses = 0;//how many hotdogs came to rest crossing a line double hotdogGirth = 0.0f;//the width of the hotdog, defines the radius of the cap at the end double capRadius = hotdogGirth / 2.0f;//the radius of the round bits/cap at the end double shaftLength = 1.0f;//the length of the hotdog shaft, that is, only of the straight parts, excluding the curved ends double halfShaft = shaftLength / 2.0f;//we're working with circles centered around 0, so it makes sense to have some radius's pre-calculated double totalLength = shaftLength + (2 * capRadius);//the total length is the length of the shaft, plus both caps on the end int output = 0;//a toggle we use to show output
double best = 0;//the best estimate of PI so far, could be a random one off chance, but meh double bestDifference = 1000;//the difference between the best PI and real PI, start high so that our first hit should be lower uint64_t bestToss = 0;//the iteration the best mostly-PI happened on //kick off the random number generator
srand(time(NULL));
while (1)
{ //pick a random position
double x = (rand() / (double)RAND_MAX) * totalLength;
double y = (rand() / (double)RAND_MAX) * totalLength; //pick a random angle, where angle = (random percent * PI), seeing as cos and sin use radians.
double angle = (rand() / (double)RAND_MAX) * M_PI; //we center our hotdog around 0 initially, meaning that the tip of one end rotated at our random angle will be at cos(angle) and sin(angle)
//we only use halfShaft as the length, we will cater for the caps later
double xOffset = (cos(angle) * halfShaft);
double yOffset = (sin(angle) * halfShaft); //seeing as we only calculated one tip, we need to find the opposite one. as we were rotating around zero, we can simply invert the points in space by multiplying them by -1.
//also in this step, we move them to our random position
double x1 = x - xOffset;
double y1 = y - yOffset;
double x2 = x + xOffset;
double y2 = y + yOffset; //now we have the positions of the two ends of the _shaft_, from these points, we can simply add the radius of the caps either way and see if they cross a line
if ( (x1 + capRadius >= totalLength) || (x2 + capRadius >= totalLength) || (x1 - capRadius <= 0) || (x2 - capRadius <= 0) )
crosses++; //now work our sort-of PI out using the magic formula
double kindaPi = tosses * (2 / (double)crosses);
double difference = M_PI - kindaPi; //if this is our best PI yet, store it for safe keeping
if (fabs(difference) < fabs(bestDifference))
{
best = kindaPi;
bestDifference = difference;
bestToss = tosses - 1;
}
tosses++;
output++;
if (output == 10000000)
{
printf("tosses\t\t= %lld\n", tosses);
printf("crosses\t\t= %lld\n", crosses);
printf("~pi\t\t= %.15f\n", kindaPi);
printf("pi\t\t= %.15f\n", M_PI);
printf("difference\t= %f\n", difference);
printf("best so far\t= %.15f (difference: %.15f @ %lld)\n", best, fabs(bestDifference), bestToss);
printf("\n");
output = 0;
}
}
return 0;
}