Tag Archives: java

Back, and with hitboxes!

Hello there! After a short break, I’m finally getting back on track! This last month has been quite a ride, so many new experiences and, while not busy busy busy, been lazy lazy lazy. 🙂 Truth is, I’ve been resting or running errands when not working, and the little time I had at home I used to relax and rest a bit.

Can’t say it didn’t work out, after all, here I am once again! 😀

I took some time to also beat some S3&K with Super Tails (on the cellphone, touchscreen gaming is hard!) and Warcraft 2 (only did it before on the PlayStation, the PC version seems to be much nicer, although more rough), things I’ve wanted to do for quite a while!

While we’re at it, another game-related thing is my new commit on the MM3 repository! I’ve finally been able to test hitboxes (even the “debug” mode works now!) and it works great! It’s not a JAWS feature, but my implementation will do. I still need to adjust its size for the current animation (different shapes, different boxes), but that’s work for another commit.

Beside everything, now that I’m a full time Java developer, I’m learning more and more about its design patterns and how things go together on large systems. It’s been a wild ride, but I’m quickly getting used to. 🙂 Gives me the creeps to look at some of my old implementations, they desperately need refactoring! 😀

See ya!

Advertisement

JenPop and Genetic Algorithms on the loose

Hey there! I’ve committed a new class on JenPop, and it’s called “Individual”. It was made to represent the rules of typical genetic algorithms:

I’ve read a bit more on the subject and figured that genetic algorithms, although being able to achieve good solutions with little computing, are only suitable for problems where brute-forcing isn’t feasible. For instance, they do a nice job on combinatorial problems such as the classic knapsack problem, where you have a list of items with value v and weight w, and you must figure what’s the maximum value you can carry in a knapsack of capacity wc by combining different amounts of those items.

And that problem was chosen to serve as the first example. At this kind of problem, a naive approach can easily become unfeasible as the input list grows. With an evolutionary approach there’s a bigger probability that it will result in a better solution, given the same time limits. And even if the solution isn’t better, it will be a decent solution. The more time you let it roll, bigger is the chance of getting a great result.

For the classic scenario with a knapsack with wc = 15, and a (v,w) item list of (4, 12), (2, 2), (2, 1), (1, 1), (10, 4), I was able to achieve the following results:

30 iterations: Best result was { 0, 0, 4, 0, 2 } (v 28, w 12)
100 iterations: Best result was { 0, 3, 0, 9, 0 } (v 15, w 15)
500 iterations: Best result was { 0, 7, 1, 0, 0 } (v 16, w 15)
1000 iterations: Best result was { 0, 3, 4, 4, 0 } (v 18, w 14)
3000 iterations: Best result was { 0, 1, 1, 5, 1 } (v 19, w 12)
5000 iterations: Best result was { 0, 0, 5, 2, 2 } (v 32, w 15)

You can see that while none of those runs delivered the optimal solution, which would be { 0, 0, 3, 0, 3 } (v 36, w 15), they got close enough to provide good solutions. Also, it becomes clear that randomism takes a nice part in this, giving us the chance of achieving a great solution with only 30 iterations. These results also show that the more you let it run, bigger is the chance of getting better results.

Anyway, see you! 😀

More on JavaScript classes, function warzone

Just like Python, JavaScript tends to create things once you mention them and they don’t yet exist. In my opinion, that’s a bit more harmful than useful, because there’s danger in accidentally creating new variables and values when you assumed it would be already holding a known value. Coming from a C/C++/Java background, I would expect some kind of error for the operation:

<html>
<body onload="testFunction()">
<div id="content"></div>

<script>
function testFunction() {
    function Test() {
        var somevar=1;
    };

    var test = new Test();
    test.somevar += 10;

    document.getElementById("content").innerHTML = ""+test.somevar;
}
</script>
</body>
</html>

In this context, somevar might not exist yet, and still, you’re adding 10 to its value! By printing it is possible to check that this var is a NaN, and no errors are issued. To properly (as I eventually found out by trying) get and set its value, one must create getter and setter functions, to “publicly interface” in other contexts:

<html>
<body onload="testFunction()">
<div id="content"></div>

<script>
function testFunction() {
    function Test() {
        var somevar=1;

        this.getSomevar = function() {
            return somevar;
        };

        this.setSomevar = function(value) {
            somevar = value;
        };
    };

    var test = new Test();
    test.setSomevar(test.getSomevar() + 10);

    document.getElementById("content").innerHTML = ""+test.getSomevar();
}
</script>
</body>
</html>

But hey, be careful when using “this”, its context is related to the current function, not the “class”, as one would think coming from a Java mindset. Using “this” inside class functions can cause undesired effects, such as defining new variables inside the “method” context and again we’ll run into the NaN problem:

<html>
<body onload="testFunction()">
<div id="content"></div>

<script>
function testFunction() {
    function Test() {
        var somevar=1;

        this.getSomevar = function() {
            return this.somevar;
        };

        this.setSomevar = function(value) {
            this.somevar = value;
        };
    };

    var test = new Test();
    test.setSomevar(test.getSomevar() + 10);

    document.getElementById("content").innerHTML = ""+test.getSomevar();
}
</script>
</body>
</html>

Pushed some updates to the game on github. 🙂

DVenn: venn diagrams with a cup of coffee

Long time ago, at one college night class, I was quite amazed that my teacher would have to draw over and over again Venn diagrams to explain logic problems and exercises. That class had a projector that was mainly used to display PPS slides. Since I was already quite proficient at Java and Swing development, I started to develop an application to help him by rendering the diagrams based on a logic expression without the need to draw the whole thing on the blackboard.

dvenn

That application was called DVenn and I guess it still helps him on classes. It can present up to 4-way diagrams and automatically adjust the render based on the number of variables. We published a paper about it and presented at some event. I’ve put it on github. I guess it’s still in portuguese, but there’s not much to translate even, so it should be easy to port around.

Looking at the code right now, it seems quite bulky for what it does, but I’m OK with it. Perhaps some day I’ll do some refactoring and call it done.

Blogging from my ancient Android tablet

Hey there, this is the first post I’m attempting to deliver from one of my cheap tablets (this one being written from an Eyo net-tab, I guess, the name may vary, also looks nothing like the picture), through the amazing WordPress app. I’m typing at one of those mini USB keyboards residing on an imitation leather cover, and I must say that it’s not that bad…

Lately I’ve been playing with some JavaScript engine called JAWS, and a game is starting to take some form. >:) It’s being hard to blog, sometimes due to lazyness, other times to being quite busy (thank HabitRPG for that), but I’m trying… trust me! 🙂

Also been using Hibernate at a Java SE application, and I can’t say bad things about it (aside reproducing some JPA bugs with floating point numbers at MySQL, but nothing directly related to Hibernate), in fact, it indeed made my life much easier with the Netbeans plugin that does all the “low-level” SQL work leaving you with ready-to-use objects from database tables, and that’s simply wonderful! No need to re-invent the wheel everytime you start a new application. This way I can focus more at UI level programming and doing a much more polished job.

For tonight, that’s it. I’ll be back later.

PS.: The WordPress app sucks at dealing with links at edit level I’ll put the text links later.

Update:  even the wordpress admin panel is not hanling links correctly at WYSIWYG editor, had to place links directly into the post’s HTML.

JenPop: genetic populations in your hands

I’ve started developing a new hobby project (yeah, why not finish the other projects first?), that can create entire generations of nodes based on a single parent, according to the rules and score methods you provide! It’s JenPop (if you didn’t guess, J is for Java :D, Gen is for genetic, Pop is for population) and I already have code to throw at you!

At the moment, only 2 features are available:

  • Create generations and children (of any object) based on the rules you provide, from a parent class extending GeneticNode;
  • Get node scores and best nodes based on the rules you provide;

And as a bonus, a sample of Tic Tac Toe implementing the above features, that will never lose. Can’t lose even when playing against itself! 😀 For the moment, the only way you can download and use it is by downloading the tarball from my Google Drive, but I’ll host the project online as soon as I decide where to.

As the next step, I’m aiming into being able to process the best “path” to use after calculating nodes from many generations, looking at the possibilities from the current state. After that, I think I’ll implement some cross-over methods, so that you can “diverse” your population, it’s useful for some problems.

Needless to say, I’m quite happy with the results at this moment.

See ya!

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

Gameplay

Game Over Screen

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