Software Design

I'm still writing stuff that is intended to teach programming. Great fun,but very hard work (apparently). I'm up to the bit where I'm trying to make a game more interesting.

BlockBuster
..but how do you detect when all the red bricks have gone?

I am recreating a game I first made many years ago in Lucidata Pascal on a South West Technical Products 6809 based microcomputer.  It is a simple breakout clone with one or two interesting touches as you go through the game. Apparently it was responsible for a lot of lost time in the Psychology department at the time I wrote it, because they had the same computers and spent ages playing it. Chris used to spend entire lunch-hours on it, holding a ruler against the screen to line up really tricky shots....

Anyway, I digress. The place we've got to is where we have a row of blocks and a ball, and we can destroy the blocks with the ball. It gets a bit boring when all the blocks have gone, so our program must detect when the last block is removed. There are essentially two ways you can do this, you can keep a counter of blocks that are left and reduce it each time you remove a block, or you can look through the blocks and see if you can find any which are still visible. But which is better?

Keeping a counter has the virtue of simplicity and makes the smallest program. However it also adds a counter variable which is coupled with the array of blocks. If the counter and the array get out of step for any reason the program will misbehave. If the program checks the array each time there is no question of this happening. In other words one design leaves the system open to bugs that could not occur in the other. I'm trying to get people thinking about the craft of software development and into the habit of worrying about things like this when they write programs.

I often get asked "What is the best way to do this in software?" as if there is an solution which is perfect in every way. I tend to reply that there usually is no such thing a best solution, there will be a fast one, one which doesn't use much memory, one which has the shortest program code and so on. To that you can hopefully add "simplest" which is the one that I tend to go for, unless I'm really worried about performance.