Writing interactive command line apps in JavaScript with Node.js, or how to write the most classic type of software on the most modern framework without frying your brain in the process


Say you want to write a little command line application. For example, a simple app that greets you, asks for your name, and then talks to you by your name. Here’s a sample session showing how your intended app looks like when used (user-typed input shown in red):


But here’s the catch: you want to write this using JavaScript. Maybe JavaScript is your favorite language, or possibly you’re trying to learn it in order to develop web sites. In any case — you start researching, and it seems not-that-easy to use JavaScript outside the web browser. JavaScript was born in and for the web, and it has never felt very at home at the command line. But not all is lost…

Read the rest of this entry »

Defining Intelligence

You don’t understand something until you can recreate it.


Some concepts describe simple phenomena that are easy to define: “gravity is the force of attraction between masses,” ”a prime integer is one which is only divisible by itself and the unity,” “peace is the freedom from disturbance, war or fighting,” etc.

Other concepts describe aspects of more complex systems, and are thus harder to define. “Happiness” or “intelligence” are two such examples.

A widely-acceptable definition of intelligence has so far proven to be an elusive goal. Finding a good definition is not only theoretically interesting, it is also key to building artificial intelligence. At the very least, it will address the issue that “anything stops being considered artificial intelligence as soon as it is made to work.”

A good, acceptable definition of intelligence should:

  • Clarify the minimum system capable of intelligence, which is an exercise unto itself.
  • Describe known instances of intelligence, and reject behavior that, while sophisticated and efficient, we prefer to label as non-intelligent.
  • Ensure that an embodiment of its terms always results in a system acceptable as intelligent. Naturally, the level of intelligence displayed will depend on the sophistication of the components used. Any artificially-built embodiment of this definition is by definition “artificial intelligence.”

In order to build such a definition, let’s start with a clear scope for intelligence: it is an attribute that can only be exhibited by an active stand-alone system evolving over time while embedded in an environment. The intelligent system should be discernible from the environment it is embedded in, and it needs to be able to perform actions to evolve over time — by acting on the environment, by gathering information about it, or, most often, by doing both. By restricting our scope this way, we are only talking about intelligence in the context of a system where there is an environment and a capability of action over time, but this should cover most of the interesting cases of intelligence, if not all of them.

Model intelligence

The key element in any system capable of intelligence seems to be an explicit model of the environment it is embedded in. A system that doesn’t have any concept of where it is, even if it can perform actions and improve its situation, can be efficient, effective, resolutive, and can have many other attributes, but it shouldn’t be called intelligent. Think of passive adaptive temperature control systems, self-restoring stably-balanced systems, etc… all of these are cases of unintelligent, even if interesting, systems.

A model in this sense is as a function that, given the state of the environment in a specific moment and the action the agent performs in that moment, can produce the state of the environment in the subsequent moment.

We would describe any system using a model of the environment to operate in it as using “model intelligence.” This usage and the model may be basic, so this kind of intelligence may or may not result in highly intelligent or even particularly effective behavior.

Goal seeking

The next step up the intelligence food chain would be the ability to pursue goals. We will only study this in the context of systems exhibiting model intelligence, since goal-pursuit without a model of the environment is little more than a local gradient descent algorithm.

In order to pursue a goal, it is necessary to find a way to describe the goal. In the context of modeling the environment, where a language for describing the state of the environment is already available, we could define a scalar function of the state of the environment that increases as the goal is closer to achievement. Since the goal may be bounded (“reach the summit of the Everest”) or unbounded (“collect as much money as possible”), there may or may not be a maximum value for this function.

In this context, even if the system just chooses its next action by trying to maximize the goal function in the next step as predicted by the model, we could say it is exhibiting “intelligent goal seeking behavior,” where the intelligence comes from the model of the environment that is being used.

However, in most cases, such a system will look several steps ahead into the future to choose the best actions to obtain the desired outcome. Since the model will not be completely precise, the information about the environment will be incomplete, and other agents in the environment are probably better modeled as having some freedom (possibly just a result of the previous two facts), the best way to pursue goals in a model-intelligence context is something similar to the minimax algorithm used in game theory.

In all cases, the more precise the model is, the more accurate its predictions will be, and the more effective the agent will be.

Operational intelligence

An algorithm such as minimax actually has a very short attention span; it can choose the best actions for a particular purpose given a model of the environment and its current state, but a system that uses it exclusively does nothing other than explore a tree of states and perform a sophisticated type of gradient descent.

So, the next logical step up from pure game-theory-style goal seeking involves the ability to build and execute plans. In order to be useful, this needs to be built using some abstraction on top of the language describing the environment, which is itself a challenge. But even basic abstraction can provide very efficient tools to solve complex problems, elaborate sophisticated plans, provides useful scope for actions, reduce the computational power required to operate, and be able to recover resiliently from unforeseen changes in the environment and the agent itself.

The necessary key elements are the goal-describing abstraction and some type of memory in the system to store the current main goals, plans, and sub-goals, including the specific details on how these apply to the current state of the environment. When a system uses this type of organization, we would say it uses “operational intelligence.”

An agent using “operational intelligence” can show highly sophisticated behavior. Many common tasks performed by human beings only require this type of intelligence.

Functional intelligence

In a final step towards the general phenomenon known as intelligence, it is necessary to incorporate its cornerstone: learning. For us to accept an agent as intelligent, we expect it to learn about the environment it operates in, while it operates in it. We also expect it to adjust its behavior according to what it learns, to better achieve its goals, to reevaluate them, to possibly find alternatives, and to get better end results using a more informed perspective.

In the framework we have described, the way to achieve learning is to have the system be able to refine over time its model of the environment. The system can use newly gathered information to build new versions of the model representing more precise descriptions of the environment; the system thus becomes progressively better at predicting the result of the its actions. At each moment, the system uses the current model to make decisions, come up with plans, choose actions, and act within the environment. Thereafter, it compares the model’s predictions with the actual results, and builds a new, tweaked and improved model that can be a better predictor, and thus a better decision tool in the future.

A system is using “functional intelligence” if it uses operational intelligence and it also builds new and improved versions of the model of the environment based on its experiences.

The name “functional intelligence” is used due to the model being a synthesized function matching the experience of the environment. A complete “functional intelligence” system is composed of simple operational state and a small number of computable functions, thus removing any need for “magic” for intelligent behavior.

Closing thoughts

To the author, the definition of “functional intelligence” above describes exclusively known and accepted instances of intelligence, and any system built according to its principles will necessarily act with a level of intelligence corresponding to the quality of its building blocks.

If this is indeed the case, the definition above is actually a definition of ”intelligence” in general, and can be directly used to create working artificial intelligence.

In future articles, I will clarify the concepts and show practical examples. If you are interested in these, you can follow me on Twitter.

Technology Happiness

As a little side-project experiment, I’m going to be sharing some simple tips that will help you reach Technology Happiness. What does that mean? It means getting technology to help you be happy, rather than preventing it.

Too many people today are too dependent on technology, and their use of it makes them more stressed rather than less! Technology was meant to make things easier for us so that we would be more productive, access more pleasant experiences, and worry less — but too often, it’s the other way around! Because we want our lives back, I came up with Technology Happiness. It is a set of simple tips that can be applied very easily, that I have been applying and testing, and which I have validated to really help me be happier and more productive.

Here is the first bite for you, on using email:

If you like the tip and you want to receive more like this, you can subscribe to the YouTube channel, or sign up for the Technology Happiness mailing list using the form below:

Subscribe to the Technology Happiness mailing list

* indicates required

With Spacecrash we’ve seen how to complete a game. Here are my conclusions and next steps.

sc-action-screenshotI started the Spacecrash one-week-game series with a question: “Can you write a game in one week?”. But the main point was addressing the reasons why people get stuck trying to complete their first game: be it for lack of technical skills, excessive scope, or a lack of a clear game concept and focus. After dedicating the whole last week to doing the project and sharing it, I’d like to review the thoughts of that first day in the light of what we have done.

[Note: I am not calling this a postmortem because there is one pending thing I still have to do, which is to balance the gameplay difficulty and content generation. I plan to do this in the next few days and do a proper postmortem after that. But I did want to think about and post the conclusions shortly after finishing the series.]

How did Spacecrash help with the main reasons why peple don’t finish games? Here are the details:

Read the rest of this entry »

Spacecrash day 7 of 7: game complete!

Screen Shot 2013-09-01 at 11.04.16 PMOk, so the last day arrived and I had a chance to do the following critical things:

  • Fixed the pending bugs (hopefully all of them)
  • Adjusted gameplay: completely removed fuel, and removed manual accelerating/braking
  • Added sound effects for crashes, explosions, rockets, spaceship engine, etc…
  • Added particle-based explosion effects
  • Added cool techno background music
  • Added score to in-game UI
  • Beautified main menu, completed info & credits
  • Made the game save and restore options and current progress

The end result is that we have something that looks very cool, watch:

Spacecrash: Day 7 of 7 from Jon Beltran de Heredia on Vimeo.

Read the rest of this entry »

Spacecrash day 6 of 7: installers and more

So let’s do the last remaining piece of the puzzle. If you are going to deliver your game to players, you will have to package it nicely for them to download and install. The packaging process is different for newer platforms such as iOS or the Android Google Play marketplaces, but for Windows and OS X, this means building an installer. This will package everything nicely together in a single file that the user can download and double-click, and which will leave the game in the conventional location for installed apps on their system.

Since we are doing Windows and OS X mainly, I will build installers for these. I use package managers for Linux, but I haven’t really looked into how those are built – if anyone can shed some light about this in the comments, together with how to tackle the multiple-incompatible-Linux-package-managers issue, I will be grateful!

Read the rest of this entry »

Spacecrash day 5 of 7: fonts and menus

mainmenuSo today I took the time to add some text-based menus. Nothing out of the ordinary, but something necessary for any complete game. I spiced up the menu just by having graphics from the game in the background – this is a common trick by all cost-conscious game developers since time immemorial!

KromaskyFirst thing I needed some way to draw text. OpenGL doesn’t provide any built-in way to do so, so you need to do it your own way. The simplest thing is to prepare a texture with your font, and paint pieces of this texture for each character. You can see the texture right next to this text (this is a font by Marc Russell from SpicyPixel – thanks!), and here is the code to do the text drawing using this font:

Read the rest of this entry »

Spacecrash day 4 of 7: multiple levels with cool, varied graphics and gameplay

tiled_background_3I initially planned to have only 3 levels, but turning that into 9 looked more sensible: completing a level is a positive reward for the player, and thus it will help us provide them with a better experience. So let’s get started trying to generate nine levels with varied graphics and progressive difficulty.

Both variety and progression are very important to providing a satisfying game experience. Let’s start with the graphics which are the easiest here, thanks to Dan Cook’s Tyrian graphics. I initially planned on having three “worlds”, thus three main sets of graphics, and three levels in each world with progressing difficulty. But given the Tyrian graphics easily allow more variety, I have decided on having 9 different looks, one for each level.

Level graphics

Having a look at the Tyrian graphics, I saw some interesting things for level background graphics. Have a look at this piece (shown at 4x size):

Read the rest of this entry »

Spacecrash day 3 of 7: gameplay revamped

sc-screenshot-day3After we started to get some gameplay yesterday, let me take a bit of time and recap where we are. During the project, you have to focus and often need to work as fast as possible, but it’s important to raise your head every so often and make sure you know where you are and where you’re heading.

We are starting some gameplay out of the prototype in the current stage. It is sometimes genuinely fun to fly the ship manoeuvring between the rocks. The variety is low, but still it’s proven a bit of a challenge requiring me many attempts to reach the end of this level as posted yesterday: the end of the level is defined by the constant #define RACE_END 100000.f at the top, since each screen is 1500 units high, this means you need to fly 66 screenfuls without dying to complete it.

There are a few gameplay elements that are sure to add to the experience, at least in variety: my idea is to allow the ship to self-repair slowly by spending fuel, and I also want to add shooting rocket, extra clumps of fuel you can pick up, and also the possibility to slow down / accelerate at your own will. Apart from these gameplay elements, there are quite a few things I’m planning to add only for it to look, play and sound cool: sound effects, particle effects, nice background graphics, animations…

But let us not lose focus. All the elements above (both gameplay elements & all special effects) will not “fix” the core experience. That is something that is essential to the core gameplay. And there are some “gotchas” with the current model.

As a player, I'd stop playing after stumbling upon this. Not good.

As a player, I’d stop playing after stumbling upon this. Not good.

The main limitation in the current gameplay is that the totally “random” distribution of rocks creates a very uncontrolled experience. You can fly for quite a while without encountering more than random easy-to-avoid rocks, and suddenly, you can stumble into a barrier formed by several rocks which there literally isn’t any way to avoid. This is tremendously frustrating, and it totally kills the player experience. The player wants to think that this is about their skills, and it’s very important for them to know that, when they fail, it was because of a mistake of them, and that they could fix it next time.

Don’t get me wrong, luck can play a role, even an important one. I have been playing plenty of Candy Crush Saga myself lately… and you can bet I think is more about luck than about anything else… but still tremendously addictive. But the point is that in CCS you still have some doubt about whether luck was the cause or not, and you can at least always play, even if you don’t beat the level! In Spacecrash, when you confront the player with a barrier of rocks without a single gap, they just feel insulted.

How can we address this? I can think of three different ways:

Read the rest of this entry »

Spacecrash day 2 of 7: core gameplay

Screen Shot 2013-08-28 at 12.39.40 AMToday is more “gameplay day” than anything else. We have to try and squeeze some gameplay out of the game concept we are working with. I’ve spent a few hours and it’s starting to show some promise.

First thing: you want to start playing as soon as possible, to get a feel of the gameplay experience. It’s less important to have great sound & graphics feedback, you can easily imagine that. But you need the core control & game result. In this game, I’ve implemented crashing into obstacles and a life/energy bar as soon as possible. After that, it starts to be a game, since you can die!

One concern I have with this design is that, since content generation will be random, it will be quite difficult to control. I will try to use patterns and pattern sequencing to control this better (for example: define a level by sections, and manually control parameters controlling the random generation of each section). But still, you don’t have all control. To me, this means it’s better to make a game which is somewhat “forgiving”: crashing into an obstacle shouldn’t finish the game, since we won’t even be sure we won’t generate situations where it is impossible not to crash into something.

Read the rest of this entry »