Learn Programming in Java


<<Previous | ToC | Next >>

Event-Driven Software

Computer-Human Interaction: Who's In Control?

In all of the programs we have worked on so far, the computer is in full control. It waits for human input, but other than that, the human must conform to what the computer is expecting. This is backwards. Humans are the masters, computers are just servants. It will be a very long time -- probaby never -- before computers have the intelligence and self-image and will to do anything other than what the programmer intended them to do, like a computer with a mind, will, and emotions, in other words, to be a person.

Until then, the computer does just what its program tells it to do, neither more nor less, and the program is what you told the computer to do, neither more nor less. So when the computer is rigid, controlling, or abusive, it reflects back on you. Today we will look at how to make the computer more polite, more willing to let the (human) user be in control. It's the difference between linear programming (Unix) and event-(human)-driven (original Mac) software. Most of that is appearance, because of course the computer still only can do what the programmer told it to do, but did the programmer tell it to demand "My way or the highway!" or did the programmer allow the human user to decide what to do next?

When you use a word processing or painting program, who decides what to do next, you or the computer? You do! You choose a tool, you decide where on the canvas or in the document to apply it, you decide what words to type or what color to add it, you the human. These programs were invented for the (original) Mac. The idea of a "human interface" didn't exist before 1984 (perhaps you saw the original Mac SuperBowl commercial).

Now, as a programmer, you have the same freedom, but at a second-order level: you get to write a program that lets the human be human. Or you can push people down to the level of the machine, allowed to do only what they are told. Superstar programmers lift everybody up.

Do you remember the first of the Six Principles? Sequence, doing things in order. With events we take that to a new level, where the human (real world) supplies the order. Your program responds to events and does what that event calls for.
 

Converting Flat to Event-Driven

The short answer is, you can't. When your program is driving the interaction you can take all kinds of shortcuts that are not available when you do it right. That's why the less-skilled programmers prefer to do it that way, it's a lot less work. I call it the Conservation of Complexity: either you (the programmer) do it, or your users do it every time they use the program, somebody needs to deal with all the messiness of the Real World. Better if you do it once.

I was going to except the Calculator because it has a single while-loop around the whole program, the theory being that you replace the while-loop with event handling. It works for the calculator, but not generally. In fact it wouldn't work for the calculator either, except that you only have two inputs, and they are very different in nature (1-letter command vs a bunch of digits for a number). You still need to add extra code to cope with deviations from the required sequence, operator before number, and how do you know the number is done? That's why an ordinary calculator has an Equal key; you didn't need it in our text version of the calculator, because BlueJ required an Enter key which served a double purpose.

The calculator is still pretty easy, so you can learn the GameEngine doing that.

Or you can make Pong your first GameEngine program (there are fewer widgets, but more things to do with them). The Pong discussion is built into the GameMaker documentation, so just go from there. You can use the Back button on your browser to resume here where you left off.

Other games that you already have working, which you can try your hand at making them event-driven -- plus they look much better in the GameEngine than with ASCII graphics:

Event-Driven Calculator
Rock-Paper-Scissors with animation
Hangman with better graphics


Next: Tic-Tac-Toe

<<Previous | ToC | Next >>

Revised: 2020 November 4