Roguelike Languages.
JavaScript
The column »Roguelike Languages« introduces several well-known or not-so-well-known programming languages in which somebody can write a roguelike. It is assumed that you already have some basic knowledge about programming techniques.
Introduction
JavaScript is a language specifically designed for dynamically controlling elements in an HTML document. When you move your mouse over an image on a browser page, and the image changes, you are seeing JavaScript at work. Although it may not be the first language you would think of for writing something as complex as a roguelike game, JavaScript is a surprisingly versatile language, and it does offer some unique advantages.
As the name implies, it is a scripting language; it is not compiled to machine code, rather the source is interpreted by the JavaScript interpreter each time your program is run. Its name would also imply that it is somehow related to Java, but this is not the case. JavaScript is no more related to Java than csh is to C. That being said, there are things about JavaScript which will look very familar to the Java programmer. The syntax of the language is similar to Java, C or C++. JavaScript's String, Math, and Date objects have similar behavior to the equivalent Java classes.
Netscape/Mozilla’s JavaScript and Microsoft’s JScript are technically two different dialects of a language called ECMAScript, but it is safe to just treat them as a single language.
JavaScript is a Turing-complete programming language. It is a loosely typed or »duck typed« language. Variable types are not declared, but are set by implication based on the value that is assigned to the variable. If you assign a value of a different type to an existing variable, that variable’s type will change accordingly. The types are number, string, object (which includes arrays and maps), and function.
The syntax of the language allows for procedural or object-oriented programming, or a combination of both. Although you can create objects in JavaScript, many of the facilities normally associated with OOP are not supported by the language. There is no direct syntax for such things as inheritance, method overloading, method overriding, etc. That being said, the language is flexible enough that you can achieve the equivalent functionality without much difficulty. JavaScript is not multithreaded. It does have garbage collection.
You might expect that a language written specifically to run in a browser environment would have some rich networking capabilities. But until recently it was painfully difficult to write JavaScript code that communicated directly with a server. A recent innovation to JavaScript is called AJAX (Asynchronous JavaScript And XML). Basically, it is an object that allows JavaScript to send POSTs or GETs to web servers, and receive an answer synchronously or asynchronously, without reloading the browser screen. This small innovation has greatly enhanced JavaScript's usefulness as a language for writing rich clients in multi-tier applications. Even though they call it AJAX, you don't have to use XML as your data interchange format. JSON, or JavaScript Object Notation is a great format for sending information between a server and a JavaScript program. Any JavaScript object can easily be serialized and deserialized to/from a JSON string. JSON parsers are available for most common server-side languages.
Trying it Out
Of course, the thing that really sets JavaScript apart from other languages is that it operates within the environment of a web browser, and its purpose is to manipulate the elements in an HTML page. The HTML page is represented within JavaScript by an object called document. You will generally use both HTML and JavaScript together to define your program. User manipulation of HTML elements (like buttons, links, or text boxes) generates events which become the input to your JavaScript functions. Those functions can then add, remove, or change elements on the page to create the output of your program.
There are JavaScript IDEs and debuggers available on the web, but all you really need to start using JavaScript is a text editor and a browser. You can place the JavaScript code directly in the html file, or separately in a .js file that is referenced from the html. As your program gets more complex, you can divide into many .js files. (You may want to concatenate all of them into one file again when you deploy your program; you will save a lot of overhead when the user visits your page.)
Different browsers handle syntax errors in your JavaScript in different ways. If you forego a JavaScript debugger, Firefox’s JavaScript console will give you the clearest information about your errors. Firefox’s parser is more forgiving than IE's, so make sure you test your program in at least these two browsers.
JavaScript for Roguelikes
As mentioned at the beginning of the article, you might not think of JavaScript as a good language for writing a roguelike, and there are indeed a number of challenges that it presents. But there are also a number of advantages that using JavaScript will afford you.
First, the challenges:
1. There is no console to output to. There is no equivalent of a curses library for JavaScript. Output is accomplished by manipulating HTML elements. While this can be used to great effect, it is a very different paradigm from console output; you will have to change your way of thinking.
2. As a scripting language, JavaScript is slow. For much of your program, this will not be an issue, but you will have to be very careful writing computation intensive functions to be as efficient as possible.
3. If you want to add sound to your game, JavaScript is a bad choice. For some reason, there is no built-in support for playing sound files, and the makeshift solutions that are available do not work well.
4. JavaScript has no access to the operating system. For example, you cannot open, read, or write files with JavaScript. Which means …
5. Options for saving games are limited. Of course, most any roguelike will need a save game feature (see Saving Games below).
6. You can’t hide your source code. In order for your program to run, your JavaScript files need to be read by the browser, which means they can be read by anyone who comes to your website. There is no object format for JavaScript files. There are JavaScript obfuscators which will make your code harder to read (and smaller).
So why even consider JavaScript? Here are some reasons:
1. JavaScript is free and you already have it. There is nothing you need to download or buy to start using it.
2. JavaScript is an easy language to learn and use. There are plenty of resources on the web to show you how it's done, and many examples that you can look at for ideas.
3. Your game will have the widest potential audience. Anyone who has a web browser installed on their computer (which is just about everyone with a computer) will immediately be able to play your game without having to download or install anything.
4. Players will always be playing the latest version. If you find a bug, fix it, upload the fix to your website, and you are done. You don’t need to get patch releases out to everyone who has the game.
5. You can track your game’s popularity. Most likely, your website already gives you statistics for how much traffic each page is getting. You can use this information to see how many people are playing your game. With a little work, your game could use AJAX to communicate additional statistics; you could track which monsters are doing most of the killing, which level is the most vorpal, etc.
6. A browser is a very rich user interface engine. Creating your GUI in HTML can be a lot easier than a lot of the GUI tools available for other languages.
7. If you want to write a multi-player, client/server roguelike, JavaScript, with AJAX and JSON, is an ideal technology for creating the client.
Saving Games
One nice feature of JavaScript is the easy serialization and deserialization of objects. This makes creating save information, and restoring from that information, quite simple. However, persisting that information to long term storage is a bit trickier.
There are a couple of different strategies for saving and restoring a JavaScript game. The most straightforward is to use cookies. A cookie is a text string that a browser uses to store information on a client machine, associated with a particular web site. You are limited to 20 cookies total for your domain, and each cookie must be at most 4K. So in theory, you could save up to 80K of data in the browser. Another option is to use Ajax to communicate to a CGI application on your web site that will save the game there. This is of course more work, since you will have to write a server-side application, for example in PERL or PHP. Additionally, this means that everyone's saved games will live on your machine instead of their own.
Summary
In spite of its weaknesses, you will find JavaScript to be very flexible, easy to use, and a lot of fun to play with. It is quite possible to create a full, feature-rich roguelike using the language. Then you can immediately share that roguelike with all of your friends, even the ones who don't have the same operating system as you do.
Comment #1 by Oliver Wong (2007-04-12)
You write »JavaScript is a touring-complete programming language«. The correct spelling is »Turing Complete« Alan Turing was an Englishman.
Of course, thank you. We meant Turing. It’s fixed now.
Comment #2 by Kesipyc (2007-04-14)
There are some ways to save file in browser … (not sure is it still JavaScript). They do it on TiddlyWiki.
If you would like to comment on this article, use the form below. Your comment will be reviewed and activated as soon as possible.