deviantART facebook google myspace

Magic Squares

I've gotten a little lazy lately. I've been spending too much time playing Minions of Mirth, watching television and series DVDs and hadn't done any programming. I decided it was time to see what I could still do. I was originally thinking of doing an implementation of Tic Tac Toe, but opted for Magic Square because it has been a while and the logic is simpler.

Magic Square was a game on Merlin, the Electronic Wizard, which was released by Parker Brothers sometime in the late 1970s. It is played on a three by three matrix.

The object was to light all squares but the center square.

I actually learned to program before GUIs were popular and have the most experience writing for the command line. I opted to start with C++ for the console on an Intel based Mac Mini running OS X 10.5 (Leopard). By the time I realized I needed to use the CURSES library I had already started. The C++ I used is little more than bastardized C, and should compile on any machine which has a curses library available. Porting to C is trivial. I had to research how to use the curses library, read up on makefiles and refresh my memory on namespace, and would have saved time if I had just opted for C.

The C++ should probably be implemented using a board class, but I was always more into top down design as opposed to object oriented. I might rewrite if I get around to creating a CGI or Java version.

I assumed you would use your numeric keypad for input. However, it uses the numbers, so if you don't have the keypad, 123 is the bottom row. Any other input will quit the current game and insult you appropriately. The game requires at least 19 rows and 51 columns on your terminal, but could be modified to run on a much smaller screen by editing mgksqrout.cpp.

All the downloads below contain the full source. All have the executable file for the indicated platform if any. I keep /usr/local/bin in my PATH, and 'sudo make install' will copy the executable magicSquare to that directory on my Mac and my Ubuntu machine. Modify the Makefile as appropriate, or just copy the executable file to a directory in your path. This is currently a command line program and should be launched from the command line or terminal - it may or may not work if you double click on it in a GUI depending on your environment.


I decided to do a javascript version. I'm unfamiliar with programming in javascript and I did a lot of experimenting. It's not the cleanest code around, but it gets the job done. It should work in any browser with javascript enabled. The rewrite is from scratch, and I opted for a quick hack to initialize which does have a bug. There is no test to see if it was initialized with the winning screen. The probability is of this occurring is about 1:512. The artwork was created using 'The Gimp'.


I didn't bother adding a readme or license to the source. Though I'd appreciate a mention or a link back to my site if you distribute it, the programs and source may be distributed freely.

Browse source files


Friday, 22 December 2017   Michael J. Chappell   Contact me at: mcsuper5@freeshell.org