Tag Archives: old code

Some cleanup

Hey there, it’s hard to find time with Dexter and Breaking bad finales approaching! But they’ve already been dealt with for this week, so, more free time! 😀 Getting more tasks done, and not missing midnight, is quite an achievement for me.

Also, made a new commit from some of today and yesterday’s code on the new game, implemented a rough hitbox scheme, and still trying to debug it, works ok on bullets but not on enemies… perhaps it’s not a good approach to override a sprite’s rect() and I should implement my own collision functions, that look for hitboxes instead of them. Still, I can finally hit monsters and kill them (after the brief immunity period)!

I’m thinking about doing some cleanup tomorrow, on older posts, saw some code out of code tags, gonna check it out. I miss the CodeJam times, gotta do some more of those problems. Quite some work ahead!


Project hosting, tough choice

Howdy 🙂

it’s 10PM once again, and I’ve decided to restart posting. Really it’s been really hard to focus energy into posting content. Let’s see how it goes. I was planning a deep analysis into many different services and their offerings, guided by this wikipedia entry, but, as a colleague once said: “who DOESN’T use GitHub nowadays”?

I love Free and Open Source Software and advocate its use, but I don’t really care if the host I’m using holds proprietary software. I work in a closed-source business company (in fact, we use plenty Open Source software to produce our proprietary products), and I don’t think every software MUST be free, but COULD, with a different business model.

I’d like to have a defect control system with it (such as Bugzilla) and I’d love to have my projects featuring a nice front page introduction, perhaps even some examples of its use. A wiki system would also be nice, but I could live without it. I don’t need a build system, mailing lists or forums for my projects. There are also lots of alternatives for team management, so I don’t need it (especially on single-man projects such as mine).

So, not only GitHub matches my needs, but also Sourceforge, Google Code (without web-page hosting), Launchpad (without web and wiki), JavaForge, GNU Savannah and some other less known choices. But, it’s friends’ good experience with it that guides my choice this time, and I can always change if there’s a better solution. 🙂

To GitHub!

Tibia time: Carlin’s Explosion

Well, I’m an old Tibia MMORPG player (Jack Bouer used to be my main character) and even after retiring, I continued to back it’s (at the time vibrant) community for a while, be it by drawing the infamous TibiaNews’ Comic Strips (removed from the original server, re-uploading to my gDrive… soon) and the original TibiaScoop.com (merged with TibiaNews.net, apparently) games that I’ve launched. There was one game that I’d like to start, which was Carlin’s Explosion.

You’re a powerful bloodthirsty player, and clueless newbies are trying to get to the depot at all costs. Magically rewarded with infinite mana points, you decide to stop them by launching waves of the most powerful attack spell (at the time) the Ultimate Explosion! Kill as many newbies as you can before they fill the depot!

It contained an online highscore mechanism (that I can’t host for now, so it is disabled) where site members could compete. 🙂 Beside being based on the original tibia sprites, all artwork was remade by me (CipSoft complained at the time about using the original Tibia artwork on it). So, here it is! The infamous Java-based game, Carlin’s Explosion (hosting is for now a courtesy of Google Drive)! 😀

Java package download (run with hopefully just a double-click, or with a “java -jar CarlinExplosion.jar”).

Since I can’t host the jar with direct access (for an applet version), some screenshots:

Title Screen

Instructions Screen


Game Over Screen

I’ll upload the source code and artwork soon, after some quick review! 😀 Happy gaming!

CodeJam 2009 Qualification B

Aaaaaand here we are!

So, link to the problem entitled Watersheds:


And it says:

Geologists sometimes divide an area of land into different regions based on where rainfall flows down to. These regions are called drainage basins.

Given an elevation map (a 2-dimensional array of altitudes), label the map such that locations in the same drainage basin have the same label, subject to the following rules.

  • From each cell, water flows down to at most one of its 4 neighboring cells.
  • For each cell, if none of its 4 neighboring cells has a lower altitude than the current cell’s, then the water does not flow, and the current cell is called a sink.
  • Otherwise, water flows from the current cell to the neighbor with the lowest altitude.
  • In case of a tie, water will choose the first direction with the lowest altitude from this list: North, West, East, South.

Every cell that drains directly or indirectly to the same sink is part of the same drainage basin. Each basin is labeled by a unique lower-case letter, in such a way that, when the rows of the map are concatenated from top to bottom, the resulting string is lexicographically smallest. (In particular, the basin of the most North-Western cell is always labeled ‘a’.)

So, looks like a path problem. As a path problem, we need to calculate which route to use based on series of rules, like walking through a map and choosing where to go next. Since usually the same deciosion applies to all “map units”, recursion is your friend. 🙂

To the I/O Rules:


The first line of the input file will contain the number of maps, TT maps will follow, each starting with two integers on a line — H and W — the height and width of the map, in cells. The next H lines will each contain a row of the map, from north to south, each containingW integers, from west to east, specifying the altitudes of the cells.


For each test case, output 1+H lines. The first line must be of the form

Case #X:

where X is the test case number, starting from 1. The next H lines must list the basin labels for each of the cells, in the same order as they appear in the input.

For each problem, there’s an array filled with height values. What we need to do is fill these arrays with letters, starting from ‘a’. The rules are quite simple:

To better explain the rules, I’ll separate the neighboors in two classes: path neighboors and loop neighboors.

  • Path neighboors: map units that belong to the same “route” and are next to each other. The ones we choose to move to.
  • Loop neighboors: map units that our loop tells us to go to. Usually, loops iterate through arrays the same way: through lines, left to right. Our loop neighboor will be the next neighboor which the loop tells us to go.

For each block, do:

  • Check our number. Are we already a letter on the result? If so, move to the loop neighboor, else, go on.
  • Check the 4 neighboors. Is our number bigger than theirs? No? Then we’re a sink. Write our letter on the result, increase the current letter, move to next neighboor. If our number is bigger, keep going… 😉
  • Which neighboor has the lowest number? There’s a tie? Ok, then let’s choose based on the priorities: North, then West, then East and South. If there’s no tie, choose the lowest. Write our letter and go to the chosen neighboor. 🙂

And… here’s the code. It’s old and really bad written, so be careful. Haven’t tested it recently but I guess it works. If it doesn’t, well… tell me! 😀

from numpy import *
from array import *

alphabet = "abcdefghijklmnopqrstuvwxyz"
cur_basin = 0
ma = [['a']]


def rec( m, ma, h, w, l, a, cur_basin):
    lv = 65535
    lh = 0
    lw = 0

    if (l != 0):
        if (m[l-1][a] < lv):
            lh = l-1
            lw = a
            lv = m[lh][lw]
    if (a != 0):
        if (m[l][a-1] < lv):
            lh = l
            lw = a-1
            lv = m[lh][lw]
    if (a != w-1):
        if (m[l][a+1] < lv):
            lh = l
            lw = a+1
            lv = m[lh][lw]
    if (l != h-1):
        if (m[l+1][a] < lv):
            lh = l+1
            lw = a
            lv = m[lh][lw]

    if ma[l][a] < 'a':
        if lv >= m[l][a]:
            ma[l][a] = alphabet[cur_basin]

            if ma[lh][lw] >= 'a':
                ma[l][a] = ma[lh][lw]

                ma[l][a] = alphabet[cur_basin]
                ma[lh][lw] = ma[l][a]
        if lv > m[l][a]:
            if (l != h-1):
                ma,cur_basin = rec(m, ma, h, w, l+1, a, cur_basin)
            elif (a != w-1):
                ma,cur_basin = rec(m, ma, h, w, l, a+1, cur_basin)
            elif (a != 0):
                ma,cur_basin = rec(m, ma, h, w, l, a-1, cur_basin)
            elif (l != 0):
                ma,cur_basin = rec(m, ma, h, w, l-1, a, cur_basin)

        elif lv < m[l][a]:
            if ma[lh][lw] < 'a':
                ma[lh][lw] = ma[l][a]

    return ma,cur_basin

line = raw_input()

cases = int(line)

for i in range(cases):
    header =  str.split(raw_input())
    h = int(header[0])
    w = int(header[1])

    m = zeros((h,w))
    ma = [['a']]

    cur_basin = 0
    maxh= 0

    for l in range(h):
        mapline = str.split(raw_input())
        if l == 0:
        for a in range(len(mapline)):
            m[l][a] = int(mapline[a])
            maxh = max(maxh,m[l][a])

    ma[0][0] = alphabet[cur_basin]

    for l in range(h):
        for a in range(w):
            ma,cur_basin = rec(m, ma, h, w, l, a, cur_basin)

    print "Case #%d:" % int(i+1)
    for l in range(h):
        for a in range(w):
            if a < w-1:
                print "%c" % ma[l][a],
                print "%c" % ma[l][a]


I don’t remember why I decided to use “inverted axis” (at least for me, using “lines, columns” isn’t usual as I rather do “columns, lines” for positioning) and why it’s so bad to read (hurry?).

Well, happy coding, I guess! 😛

Old code does not bring back memories…

Interesting stuff, was reading some of this old pieces of code (Code Jam solved problems) that I began posting yesterday, and I couldn’t understand much of them. It starts with key things I could easily fix, such as bad variable names (blerg, past me!), and goes until I get to the conclusion that I just can’t remember why I coded them that way. Total nonsense inside text files!

It’s being a nice effort to just read some of those pieces of code, looking like they were made by a different person. Perhaps that’s part of evolution, you rebuild the way you think and solve problems. Always getting simpler.


Maybe later :)