Paul Romero playing some HOMM songs on the piano

If you’ve ever played Heroes of Might and Magic, you know how wonderful the soundtracks are. I personally have played only HOMM3 and 6. They both do have amazing songs.

The people responsible for composing these marvelous songs seem to be Paul Romero and Rob King.

There is a Youtube video in which Paul Romero plays some of those songs on the piano. It’s amazing.

Some Thoughts on Meanings of Words

It happened several times to me (enough to catch my attention) that looking up the meaning of a word considerably helped me in understanding it. Not just in a dictionary however, but also in a thesaurus, encyclopedia or even in an etymology dictionary.

As a reader, I imagine you’d be surprised I’m saying such a thing. After all, isn’t it obvious? Dictionaries will tell you the meaning of words, so if you want to understand what a particular word means, look it up. And yes, it is, in that sense, obvious. However, there is another aspect to this issue.

Back when you learned procedural or object-oriented programming, did you look up in some programmer dictionary what “procedure” or “object” means? It’s very unlikely that you did. You probably just learned the concepts behind these topics by explanations in your textbook, but not precise definitions. It’s more than that though. What about the words I’m using in this text right now? How many of then have you actually looked up before?

The point I’m trying to make is that the way I generally learn meanings of words is through some other mechanism other than looking them up in some sort of reference material. And, as it seems to me, this is also true for most other people. In general, for learning meanings, we don’t look up in authoritative sources. We do something else, like learning from examples of use.

Just to be clear, there is nothing wrong with this “doing something else”. However, it leads to some issues. In scientific papers, books, other blog posts, speech and so forth, people use some words whose precise meaning aren’t that simple to be picked up informally.

Do you really know precisely what the following mean?

  • System
  • Model
  • Structure
  • Pattern
  • Object
  • Process
  • Procedure
  • Function
  • Number
  • Integer
  • Abstraction
  • Relationship
  • Predicate
  • Proposition
  • Set
  • Algorithm
  • Program
  • Schedule
  • Dynamic
  • Static

I could list a lot more, by the way.

These are all words I’ve seen enough people not quite getting them right. Some of these words, it was myself who didn’t get precisely right at first, and some up to today. The importance of these terms, for us software people, is that we seem to use them to describe what our software should do, how our algorithms work, how our software is designed, the requirements on our software, and so forth. And, as such, we should know extremely well what they mean.

Now you should get into the habit of looking things up (=D). Writing about them also helps. Making use of multiple sources is highly important too. The various dictionaries, thesaurus, encyclopedias and so forth out there help with this.

Models – 01 – Models, Definitions, Analysis and Design

Introduction

This is the kind of text I write to help myself understand something better. I will also write a little bit about the driving reasons which led me to write about models, which were analysis and design.

It’s generally accepted that a model for something material resembles it. If I tell you I have a model of a plane, you’d usually think of a miniature plane, or some 3D interactive software which simulates the plane performing various movements. Maybe something else. The point is that you usually don’t think about the plane, but about something else which somehow looks like it.

More generally, a model is about something. We should always talk about models mentioning what they’re about. These things are of extreme importance because they form a context for the model.

For something as abstract and general as a model, it’s no surprise that there are many ways for defining one. Back when you were learning, say addition and subtraction, you probably had the “follow the model” kind of exercise. It goes as the following:

Follow the model
1 ? 4 = 5 (+)
7 ? 2 = 5 (-)
1 ? 1 = 2 ( )
9 ? 2 = 7 ( )
5 ? 3 = 8 ( )
2 ? 1 = 1 ( )

To perform the exercise, you’d have to fill in the blanks with the right operator symbols as in the first two cases.

There is a model definition in this exercise. However, as I said before, a model is about something. In this case, what is “the something”? It doesn’t seem obvious, but, in a sense, the model is about how you should solve each item in the exercise. It was defined by two examples of its application. Given the examples, your mind then filled in the blanks and “got it” somehow. It begs the question: which are “the blanks” your mind filled?

A model doesn’t always have to be material (in the sense that it exists outside one’s mind). There are mental models, and those are only thought of. So if you want to communicate them with other people, you must find ways to represent them. That is, you should find a way to communicate something (the representation) that, for your particular purposes, is as good as the actual thing you want to communicate (the model). Representations can be tricky things to understand, but we use them all the time. For example, an angle is different from its measurement, and almost all the time we can talk about one through the other. We can do so because for our purposes, most of the time, an angle is as good as its measurement.

Your professor, then, was expecting you’d interpret the representation of the model (the two examples) and realize the model yourself.

Now I think I can go back to the question I’ve asked before. What are the blanks your mind filled? The answer is the model itself. When you interpreted the representation of the model, you conceived it in your mind.

Definition

What is a model? A model is a set of properties about something.

A property of something is a statement which is always true for that thing. For example, “in my game, blood is always drawn with red like colors”. The statement “blood is always drawn with red like colors” is true about “my game”, which means this statement is a property of my game.

Some models are mathematical: their statements are always true, regardless of what (they’re exactly true). For example, part of the model for real numbers is that if you have three of them a,b,c and you have that a=b, then it’s also true that a+c=b+c.

Models Go Mental

You could now be wondering how is a miniature plane a model for a real plane? Is a metal miniature plane a set of properties? It surely doesn’t look like so. One way of looking at this is that models are always mental and “material models” are always representation of (mental) models. Which means that if you think of a model for a plane and construct a miniature plane which looks just like what you’ve thought, then that miniature plane represents the model you have in mind.

The distinction I will make, then, is that a model is restricted to what you think of, but its representation can material (it exists in the real world, outside one’s mind), but not necessarily.

In this sense, it’s more useful to look at a miniature plane as a representation of a model and not as the model itself. In practice, the definition of a model is wide open and includes cases like these too. For now, throughout this article, I am going to use that models are intrinsically mental because I believe that is a more useful way of looking at things (it’s kind of a useful lie for doing thinking on my own).

Usage, Analysis and Design

Curiously, you can have:

  • a geometric model of a plane;
  • a mechanical configuration model of a plane;
  • models for its electric configuration; and
  • many others.

In general, you could conceive several models for the various aspects of a plane. The same goes for laptop computers, software systems, and whatever else you can think of. You can take something S, look at a part P of it, ignore everything else, and build a model for S by only looking at P.
Being able to do such a think helps in analysis. You can, for example, analyze a plane by first looking at its geometry, then at its mechanics, and then at its electronics. Maybe you decide you’ll do this for each individual material part of the plane (whatever you choose those to be). Therefore, from analyzing the parts, you analyze the plane as a whole.

This activity of building a model requires you to think about something so to extract relevant properties out of it.

In general, the process of analyzing something and the process of building a model are connected. In both cases, you accumulate properties about something. For example, when you get the representation of a model, you have to look at it and extract relevant properties so you can build the model you’re expected to. It seems, then, that through analysis, one builds models. This leads us to a better definition of analysis.

Analysis is the process by which given S, you build a model for S. And some times, we talk about the analysis as being the model constructed from the analysis. The distinction should be clear from the context.

Analysis is being thought of extracting properties of something existing. But what about extracting properties of something which doesn’t exist? You can do so by analyzing how this non-existing entity would be. After that, you can derive a model of how to realize that non-existing entity so to make it real. And as it turns out, you can analyze existing things too (how nice!) in order to build models of how they could be realized.

The model of how you could realize/create something is called design. Designing is the process by which you construct a design. So it follows that designing is a creative process.

A good example of analysis and design is in engineering. Engineering is about problem solving as many other activities are. A team of engineers is given a problem. They’ll, then, analyze the problem to build a models (of requirements on the solution, of the environment in which the solution will be inserted in, and so forth). At some point, they have to design the solution or parts of it. Engineering teams are expected to do these analysis and design activities so to solve the problem. For them, analysis and design are major tools which they use to solve problems.

The design is just another model, which means that, by our definition of model, it’s mental. So a design needs representation(s) if you want to communicate it, store it somewhere, etc. In designing, engineers tend to build diagrams, texts, and all sorts of documents representing the design and/or its parts. The same goes for analysis by the way . And there are actually special tools for representing analysis and design models.

Representing models is often useful, but that is another topic. However, it’d be useful if there were documents representing all the produced analysis and designs so that others (and the original engineers themselves) can use those to learn about and/or remember what happened during the project development.

Final Thoughts

In my experience, and belief, it’s often the case that analysis form a cause for design, which means it’s very common to see analysis and designing as two connected activities. I believe it’s common, in the case there are problems in need of solutions, that there is also a need for good solutions design, which are usually expected to be well founded in good analysis.

To summarize, analysis has the idea of taking things apart because you extract properties of something, while design has the idea of combining things together because you model how to realize something.

Modeling is not just important for analysis and design, you could think, but these two concepts were the reason I decided to write about modeling.

If you can, please share your thoughts. Comment! =)

Yet on the Space Invaders Game TSpace

In my last post, I “released” a space invaders game written in TCL/TK. It has its own scheduler and task queue, which it uses to find out which thing on the screen has to be updated. It turns out that these things are done by the TCL implementation too, and it’s likely that they’re more efficient than the one I wrote.

So I modified the code to be a much simpler (without save/load, sounds and speeding up or down features) version of the game using the after command instead of implementing my own scheduler/tasks queue.

Just to be clear, I still could have the save/load, sounds and speeding up/down features if I wanted, with as much ease as in the other versions. I removed them simply because I wanted this stripped version of the game. Using the after command doesn’t imply in not being able to have these features, or in having a harder time in having these features. I could have kept them just as easily.

I actually liked TSpace, and I am likely to keep playing it. And I don’t care about sounds, save, load or messing up with the game speed. That’s why I took these out. I wrote them in the first place because I saw that I could, as a direct result of the different designs that I tried.

The interesting thing about this version is that it doesn’t try to re-invent the wheel, but uses what is already built-in in TCL. And it turns out that it ran much faster (notice that the other version may get slower later in the game, when you get lots of points, but it’s mostly because of the amount of sounds that are played with that many enemies missiles being shot at you — if you take the sounds off [not just turning the sound off in your computer, but actually changing the source code], the slowdown is not so much noticeable).

Here is the code: http://pastie.org/pastes/4598169/text.

To run it, you should use the image files from the TSpace game posted in the previous post.

(Much) Improved Space Invaders Game [in TCL/TK] – TSpace

In my last post, I sort of showed that it was very simple to put a space ship image on a black background with some asteroids floating around. Also having the space ship being controlled by the arrow keys and being able to shoot the asteroids.

Well, I decided to evolve that little game. The result was still a little game, but much better. There are screen shots in the end of this post.

The game went through some phases. The first one was that of the last post, which was pretty much a “can I do this?” thing. The second one was using the coroutines feature of TCL 8.6, which made the game much easier to extend. However, this second version only was a re-write of the first one. Then a third one was an evolution of this second one. I took the extendable version using the coroutines feature and started adding things to it.

This third version was a major breakthrough. It allowed pausing the game easily, also increasing its speed, or decreasing (you could use the plus and minus keys to make the stuff move faster or slower on the screen). Adding stuff was also much easier.

The basic idea was to dynamically generate coroutines, each to control an item on the screen. There was a scheduler function, which would go round robin on these coroutines. Pausing the game just meant telling the scheduler to stop looping through the coroutines queue. Messing up with the game speed was basically changing the milliseconds interval between each iteration in the scheduler.

Also, items on the screen were pretty independent, because they each had its own coroutine. So it was quite a nice thing.

However, it was generating a lot (really tons) of coroutines. And I wondered if I could do differently, just to see with what I’d end up with.

Then I had the idea of having a few controller functions which knew how to deal with different types of items on the screen. For example, the ship controller, the enemy controller, the missile controller. These functions would see the current situation of some item it can control, update it and produce its new current situation. This was the fourth, and current phase.

Each situation is actually called a task, because they replace the coroutines. A task has a procedure, which can perform it, and a bunch of other data (such as x,y position, image type, etc.). The scheduler, which used to run coroutines, now deals with a queue of tasks, and at each iteration, it takes a bunch of tasks, and run their controllers (which would be stored in the task’s procedure field) on the task by getting tha task controller and calling it on the task.

The advantage of this, now, is that the data needed to continue the game, from a given point, is more accessible. Which means that saving and loading game is trivial. And so, I wrote the save game and load game procedures. They pretty much saved this tasks queue, along with some other things, but the tasks queue is the major thing. And the load game function pretty much reads the saved data, and re-draw the canvas accordingly. Each task also has a load function associated with it, which could re-draw an item given its current situation, reflected in the task.

An important feature of TCL played a major role here. And it’s the feature that “everything is a string”. Since that’s true, to save the task queue, all I have to do is print it (there is no code for serialization, or any sort of conversion). And to load it back, I just read it (there is no code for parsing). That’s it. I mentioned some posts ago that  things are a little different from “everything is a string”, however, the language gives you that illusion, and you can program as if that was true. A more, it seems, accurate thing to say is that everything has a string representation. Being able to get the variable value by having its name stored in another variable also helped because it allowed for “the list of variables to save and load”. Currently, if I wanna add some more state to the game, then the code that causes that state to be saved/loaded is close to nothing; I just have to add the variables names to that list of things to save and load.

In an OO style, you could have a task interface, which has a load and iterate methods. And each screen item would have its controller, which would be classes implementing this interface. The task queue would then be a queue of tasks, and the call to iterate and load would perform dynamic dispatches. Obviously, you don’t need classes to do this, so a non-classic OO system also would allow this. And, in TCL, since you can use the value of a variable as the name of a function to call, you can just generate the name dynamically and call that (getting dynamic dispatch in effect). So, it may be that, in TCL, doing the OO actually complicated the program. Anyway, I didn’t do it this way also because idk how to yet. I still haven’t learned how to use OO systems in TCL.

That being said, if you wanna take a look at the game. I uploaded it on 4shared.

http://www.4shared.com/rar/HQHHcoAg/tspace.html

Here is how you play the game:

  • The arrows move around.
  • S saves the game.
  • L loads the game.
  • + and – and 0 deal with game speed.
  • P pauses the game.
  • F is the simple shot.
  • D is the triple shot.

To run the game, you need a TCL implementation with the snack extension. An easy way to get it is by getting active tcl, and installing snack after done that. Here is how to:

  1. Go to the downloads page, http://www.activestate.com/activetcl/downloads
  2. Get the 8.6 version of active tcl (it cannot be the other older ones). You should also get the 32bit one, because iirc, snack is not available for 64bit. Even if you run a 64bit windows, you can download the 32bit version and it should all work (which I just found out recently).
  3. Install active tcl.
  4. To install snack, go to the command line and type “teacup install snack” and wait.
  5. A friend of mine had the problem of not getting the Img package out of the box, and he had to install it. So you may need to. Do it by typing “teacup install Img”.

That should be it.

To run the game, extract it and double clicking on the tspace.tcl should do fine on windows. On some linux, you probably will run on the command line with tclsh.

And as a side note, I also found out I cannot spell words very well: I’ve been writing ammOnition and missIle (you will find this in the whole source code).

The images and sounds of the game were not created by me. I got them from sources I could use for learning purposes. Here are the credits (which you can also see in the sources.txt file in the .rar file of the game).

If you find bugs or that something is missing, or strange, post as a comment please. I just found out I forgot to put two required files in the rar archive (which is now fixed). So, if anything, write a comment please.

Updage: I found some bugs after I wrote this post. So if you find a bug, maybe re-downloading the package (which I just updated) may help. But comment anyway =).

Here are some screen shots:

Simple Space Ship Game in TCL/TK

As I mentioned in my last post, I just started learning TCL and TK.

Well, yesterday I decided I would see if I could put a image of a space ship in a black baground, and make that image be controled by the arrow keys. After one hour or two I did that. And I thought “well, this was really easy; maybe I can get it to shot if I press F too”. Then I went on, got the image of a small missle, and made it move from the ship to the top of the screen if I press F. After that, I found asteroids images and put them rolling and moving in the screen and made a space ship game for shooting asteroids.

I started making (learning how to) the game 7 or 8 pm yesterday and finished 9 am today (yes, over night).

And it was really simple. It’s worth pointing out that the code is pretty much a bunch of stuff on top of eachother: not a well tought design. It works because the space ship game is small and simple.

If you want to play the game, you can get Active TCL from here, and then install it. After installing it, do these things:

  1. Get the game source code here.
  2. Save the game source code in your computer, name it prog.tcl (or anything, but I will assume you named it prog.tcl, and put it in some folder, X).
  3. Save these images in X, with the indicated names:
    1. http://i50.tinypic.com/21mzq07.gif – asts.gif
    2. http://i49.tinypic.com/256e1cw.gif – missle_1_1.gif
    3. http://i45.tinypic.com/2ptc7c0.gif – ship.gif
  4. To run the game, install Active TCL.
  5. Open cmd.exe on your windows machine.
  6. Go to the folder X.
  7. Run from the command line, in folder X, “tclsh prog.tcl”
  8. Have fun.

On linux, install TCL from your package manager, follow step 1 to 3 and run the game by following step 7.

The game is really primitive. There are no sounds, no explosion effets, nothing fancy. But it is very cool =).

Here are some pictures.