Thursday, July 11, 2013

Creating NIM

When you look at the tag-based syntax that make up a HTML page, it is fairly clear that the format was designed more for static documents rather than interactive games. What you can do with pure HTML is rather limited. To get the interactivity necessary for all but the simplest of games, an additional programming language is need. This can take the form of the scripting language JavaScript which is built into all the major browsers. There are a number of other languages, such as Dart, that compile to JavaScript that can also be used but compiling to JavaScript is not quite the same as writing in JavaScript.

When I got a trollish email saying that JavaScript is not part of HTML5, which is only true on the most purest level, I decided to create a simple game using only HTML. A number of the tags that HTML5 added are clearly designed for use with a scripting language so to utilize HTML5 you really need to utilize a scripting language. Now if the email was complaining about WebGL, I would not have considered it a troll but still hope that Microsoft does start supporting it (as the rumours suggest).

The first problem that needs to be overcome when creating a no-script HTML game is finding a game that is simple enough to implement using only the markup language. This essentially means that the game needs to be representable using static pages with the interactive element switching between pages. While this is a huge restriction,  I actually came up with a number of games that could be created using a number of static pages. These include 3D mazes,  path choosing adventure games, combination puzzles, restricted move games such as Tower of Hanoi, and many of the various NIM style math games. Obviously I chose NIM.

The game must be broken into a number of pages. If we go with the traditional 17 objects, then we will need 17 pages for the player landing zones.  The problem is that the computer needs to make a move. There are at least two ways to deal with this. The obvious solution is to add an additional 16 pages for the computer moves. When you think about it, the only thing that happens during the computer move is that the number of objects it is going to take is revealed so the only thing the player is doing is clicking to continue. While merging the computer results with the landing page adds a slight bit of complexity, it cuts the number of pages necessary in half.

The easiest way of implementing the game is to create the 17 item page, clone it, remove an item and repeat until all 17 pages have been created. A slightly faster, though more complex way of doing this would be to write a script to generate the pages for you. Each page simply consists of images and  links to the lower-numbered three pages so this is really simple to create.  That really is all there is to it. Sorry for boring you this week.

Friday, June 28, 2013

Binary Prerequisites

I recently released an educational game where you play with logic gates. The inspiration for the game came from a JavaScript book that didn't even bother covering logical operations. Logic operations are used to manipulate bytes at the per-bit level making them very useful for optimizing the memory-footprint of data structures. Now that everyone has multiple gigabytes of RAM in their computers, optimizing memory is not as high of priority anymore even if it is fairly easy to do. Instead of preaching about this, I am instead going to give an example of its use and the non-boolean-logic alternative.

For a number of games under development, I have a skill system where players select their bonus skills. Many of the skills have prerequisites. Each skill gets assigned a bit within  an integer (if there were more than 32 bits, more than one integer could be used, though for me this isn't an issue as skills are grouped into set that contain 16 skills so only 16 bits are needed). Each skill record has an integer for prerequisites. The character class has an integer for each skill set indicating what skills are known. To see if a skill can be learned, a simple logical AND can be done on the prerequisite and the characters skill set. If the result equals the prerequisite, the skill can be learned.

This can be done without binary logic operators by using an array of booleans.  Checking prerequisites the becomes a bit more complicated. First, a meets-prerequisites flag is set to true. A for loop looping through the prerequisites array  checks if the corresponding skill flag is set. If it is set, the array of skills in the character sheet then gets checked. If the character sheet skill is false, the meets-prerequisites flag is set to false and the for loop is exited.

As many languages use integers to represent Boolean variables, this is a potential  waste of 31 bits. In our example, this would be a memory increase of 16 times the amount. Some compilers have optimizations for memory use in which case the compressing of the bits into a single integer will be done by the compiler, but even in that case the compiler will still have to use the looping code for checking prerequisites with the rather significant speed difference as a result. Of course, in addition to having lots of memory, computers also are very fast now so such concerns can be overlooked. But as writing the code to be efficient in the first place is so easy there really is no excuse for such a waste of memory and CPU cycles.

Friday, June 14, 2013

Thoughts on Dart

I have mixed feelings on the new languages that compile to JavaScript such as Dart and CoffeeScript. The problem with these languages is that by producing JavaScript there are people who seem to think they are creating JavaScript projects when they are not. It is possible to take the JavaScript output and edit it as if it were a JavaScript program, but unless those changes are also done in the language source code, those changes are either not part of the project or make the project much harder to maintain. This effectively means that once you start a project in one of these languages you pretty much have to stick with the language. This is not necessarily a bad thing but it does reduce the number of people who can use the source code.

Dart is a bit different from other languages that compile to JavaScript. The compiling to JavaScript is a backwards-compatibility feature designed to allow programs written in Dart to work on browsers that do not support the Dart language. The idea is that eventually browsers will start supporting Dart as one of their script-tag languages. The dart programs then would be run using their native JIT-compiler resulting in better performance. Right now there are not any browsers that support Dart, but Chrome is going to soon as the Dart SDK includes a beta-build of Chrome that has Dart support.

When Dart was first released, I briefly looked at the language but decided I would wait for the 1.0 release before trying it. A Google IO presentation on the language got my curiosity peaked and when I discovered StageXL (which like the CreateJS library is modelled after the Flash API that I am familiar with) I decided that the JavaScript issues I was having with CreateJS might be solved if I used Dart and StageXL. This in fact was the case so I have decided that I will be using Dart to port my Ultimate Retro Project to HTML5.

The Dart language is a proper Object-Oriented language with optional type support. All the type-related issues I have with JavaScript are gone and having proper classes makes working with code great.  My favourite feature surprisingly was the switch statement. Dart requires that each case statement within the switch block has a break, continue or  return statement making the issue of accidentally forgetting a break statement a thing of the past. This is one of my pet-peeves with all the C-like languages so discovering this feature made my day.

While I am still new to the Dart language, I suspect that by the end of the Ultimate Retro Project port I will be close to an expert with Dart. If like me you are coming to JavaScript from real programming languages, Dart may be the solution.  I will be continuing my discussion of Dart as I continue to work on my port, but as this is a side project I am not sure how quick the work will proceed. Perhaps by the end of the project I will have changed my mind, but my current impressions of Dart are exceedingly positive so there will have to be major issues to make me change my mind about the language.

Thursday, May 30, 2013

Towels for Earth Postmortem

As a fan of Douglas Adams since I was a kid, there was very little chance of me not partaking the Towel Day Mini-Ludum Dare. I find it really interesting that Mini-Ludum Dare#42 happened to occur just in time for Towel Day but as Douglas Adams has repeatedly said "42 seems to show up a lot." For those not familiar with The Hitchhikers Guide to the Galaxy, 42 is an important number as it happens to be the answer to "life, the universe, and everything". I decided to take the challenge a bit further and make it a Towel Day challenge instead of a weekend challenge. Here is my postmortem.

What Went Right

Making Towel Day an important part of the project was my personal goal of this challenge. Technically, the theme was "Earth will be Destroyed" to make the challenge accessible to those sad souls who are not Douglas Adams fans.  I managed to incorporate the reason Earth is being destroyed, a two headed alien, adding nutrient enrichment to a towel, towels and 42 . Not too bad for a rushed project.

What Went Right and Wrong

The decision to create the game on towel day was a mixed blessing. A game jam is short enough without artificial restrictions. In this case there simply wasn't enough time to add sound and other minor gameplay enhancements such as an unlimited mode. Time limits and other restrictions do help build creativity and let you refine your skills so the extra self-imposed restrictions are useful. To be perfectly honest, though, the real reason for the extra restrictions were the emotions related to creating a game on Towel Day.

What Went Wrong

When I first heard about the challenge, my plans were to create the game using HTML5. I figured that work porting my GameJam library would be useful as it would make it easier to finish the game in time. As creating the functionality of the Flash libraries in JavaScript would be a lot of work, taking advantage of the existing CreateJS library seemed like a good plan. From my experience with this library, it works great but it is still in development. When I ran into issues with the event handling, it became clear that rethinking the jam plans was in order. While I think these issues are JavaScript issues related to the way I am creating classes, they could be CreateJS bugs or some  other code issues I am unaware of so the time needed to solve or work-around the issues was too random to chance it. With not enough time to solve the issues I resorted to my Plan B and created the game in Flash.

Thursday, May 16, 2013

Plans for porting Java to JavaScript


When I heard that Oracle is changing their Java numbering convention because all the bug-fixes in Java are causing them problems with their existing scheme, the idea of porting my older Java games to HTML5 gained a lot higher of a priority. While the Canvas is simply not quite there yet, for the games that need to be ported it is more than adequate. I started porting the Ultimate Retro Project as a bit of a test project. Right now my results are mixed. I am using the CreateJS library instead of writing my own low-level library. This gives me the advantage of a class library that is similar to the Flash class library that I am familiar with but the disadvantage of a library that is a work-in-progress.

Unfortunately, there are quite a few old Java games that will need to be ported, so this project will take quite a while. At the same time, I am starting to port my GameJam library to HTML5/createJS and will hopefully start working exclusively in HTML 5 for my web games. I am going to be adding quite a bit of functionality to my GameJam library as I port my older games as there are many classes that can be broadened and added to the library.

In addition to playing around with createJS, I am also starting to look into WebGL. Rumours of Microsoft caving in and supporting the standard would be nice if they turn out to be true, but I have a functional canvas ray-caster that I can always fall back on so a certain series of games will yet again be revisited and hopefully this time I will get around to actually finishing the 5th and 6th instalments.

while I still think that ActionScript3 (Flash) is superior to HTML5, it is clear what direction things are heading in so I am thinking that now is the time to start focusing on HTML5. I am not sure how long JavaScript will last as the dominant web language. If the comities in charge of the language get their act together and create a better version of the language for the next iteration then it could last a while.

Thursday, May 2, 2013

Loot Run Postmortem


While I did not particularly like the theme of Ludum Dare #26 as  it was far too general, I did have Saturday free to work on so I created a quick game. To me minimalism means eliminating all that is unnecessary. For games, this means graphics and sound. The idea is to focus on the essential gameplay. Loot Run is a platform game done entirely with text.

What Went Right

Keeping with the theme, I wanted to keep the gameplay as simple as possible while still being fun. While scrolling would have been easy to do, keeping levels limited to a single screen seemed to fit the theme better. The player needs an objective. Collecting money is a very obvious motivation and is something that most players can relate to.  Having just the environment as the obstacle did not seem like enough so monsters were added. While the original plans called for title, instructions, winning and about screens, it became obvious that these things could be integrated right into the game levels.

What was Both Right and Wrong

Using just text was the right decision, but in hindsight it would have been more efficient to use a canvas with the letters as a tile set. This would have resulted in a lot less garbage collection. While this had little impact on the game, knowing that all the needless garbage collection is happening does bother me. For those not familiar with JavaScript, let me briefly explain.

JavaScript strings are immutable. This means that every time you modify a string you are actually creating a new string and essentially dumping the old string onto the garbage heap. When there are enough dead strings lying about the garbage collector gets called and frees up the memory. As generating a frame results in numerous dead strings, this is not very efficient. It is kind of ironic that faking strings by using tile-maps would have been more efficient.

What went Wrong

The controls for the game were probably the biggest problem. The method I used was to track whether keys were up or down and during the main event loop adjusting the player based on the keys. For most games this would be fine but because I am only updating the frame every 150ms this can result in too big of a lag between the players key press and the processing. For instance, if a player hits up to jump and releases the key within the 150ms it is possible that the jump will be missed. What I should have done is had an array for next actions as well as key states. The next action would have been set as soon as a key was pressed, and every interval the actions would be ORed with the keys that are still down.

Thursday, April 18, 2013

Pink unicorns


While I did successfully finish a prototype, I decided that it would be best not to release the game so instead of a postmortem this week, I am going to get on my soapbox. Feel free to skip the rest of this post.

I have noticed that there seems to be an ever-growing number of people who don't seem to know the difference between facts and beliefs. I have written the following to help shed light on this issue.

Let us say that I had an invisible rock. It is just a regular rock with no special properties other than being invisible. Let us say that I believed this invisible rock was an invisible rock. My beliefs would be correct but without evidence they would still be just beliefs.

Let us say that you believed my invisible rock was an invisible pink unicorn. Your beliefs would be wrong. If you got together a number of friends who also believed the invisible rock was an invisible pink unicorn, the invisible rock would still not be an invisible pink unicorn. No matter how much you believed that the invisible rock was an invisible pink unicorn it would not change the fact that the invisible rock was just an invisible rock and not an invisible pink unicorn.

Let us say that everybody in the world believed that the invisible rock was an invisible pink unicorn. That would still not change the fact that the invisible rock was just an invisible rock.

Without compelling evidence a belief is just an opinion. Compelling evidence should be focused, as blindly tested as possible, independently repeatable, and statistically significant.  Compelling evidence is sometimes hard to find and some things are simply not provable even if they may be true. If you can not back up your beliefs with evidence, then they are just your opinions.

In other words, when I say I think the invisible rock is an invisible rock, telling me that my denying the pink unicorn means that I am a follower of the nightmare and that will be denied from entering ponyville is probably not a very compelling argument. A rational response would be to provide evidence supporting your view. If the evidence doesn't exist (or worse, exists but doesn't support your beliefs) then the belief may need more scrutiny.