Month: December 2020

First new C Tutorial has been added

First new C Tutorial has been added

Atomes boardAs promised, I’ve added the first C Tutorial using the Atoms (aka Chain Reaction) game. However this is just a very easy intro to the Atoms game and doesn’t have a line of C code in it. We’ll save that for the next tutorial.

The planned tutorials for this are:

  1. Intro to the Atoms game.
  2. Drawing the Atoms board.
  3. Reading the keyboard and making a move.
  4. Handling explosions and checking if the game is over.
  5. Programming the computer player AI.
  6. Drawing a high-score table.

These will each include a C listing and an explanation of what the new C code does.


Thinking about future C tutorials

Thinking about future C tutorials

Image by Anand Kumar from Pixabay

The tutorials I’ve done have followed a fairly “standard” model. Learn a feature, learn a new feature and so on. But what if this isn’t the best way to try and teach C?

I can’t remember how I learnt C. I’m not actually sure that I did which sounds weird but I learnt C++ nearly 28 years ago (I bought a book on C++ programming while on a weekend holiday in York). I learnt more C++ four years later when I was working on a football game. That project lasted six months and then the designer of the game decided to visit India and never came back and the whole thing fell though.

Fast forward to 2006 when I started writing the About C, C++ and C# column for Because I knew C++, I sort of knew C without explicitly having learnt it, Back then C++ was a superset of C. It still mostly is but there is the odd divergence.

This morning I was reading this thread on reddit. “What do you guys think its the best way to improve your coding?” and it got me thinking about the C tutorials. I know that I like to learn by working on small projects. Most games are too big for a tutorial (heck you can get a book out of them).

So I got to thinking, what is a simple game that would make for a good set of C tutorials? I even dug out the two books BASIC Computer games and More BASIC Computer games for inspiration. However in the end I decided on the game atoms. Here are the rules.

Rules for Atoms (aka Chain Reaction)

This takes place on an 8 x 8 board. Each turn you and then your opponent. Add 1 to any cell on the board. At the start all cells are empty with a value of 0. When you add 1 to a cell, it turns red or blue if the computer is playing. If a cell reaches 4 then it is cleared to 0 and all four cells around it (horizontal and vertical) have 1 added to them and become the player’s colour.  Cells in the corner only need to reach 2 to explode while cells at edges need 3.

When there are enough atoms on the board, a chain reaction can take place and you win if your atoms replace your opponents or lose in the opposite case.

To keep the tutorials simple this will be a console game. Each cell will be blank or show the number of atoms in the cell followed by a C (for computer) or P for Player instead of colours.  I’ll start the first tutorial tomorrow.


New tutorial on C loop statements published

New tutorial on C loop statements published

Mandala loops
Image by Renate Anna Becker from Pixabay

There are four types of loop statements in C, for loops, while loops, do-while loops and goto statements. The first two are much more popular; In my Windows asteroids game, I used 54 for-loops, six while-loops, three do-while loops and no gotos.

I found the same thing in Delphi and Turbo Pascal. The only difference is that Pascal uses repeat .. until instead of do-while and I prefer repeat-until.  The logic is slightly clearer I feel as well. You repeat the body of the loop until a condition becomes true. In the C do-while statement, you repeat the body of the loop as long as the condition is true.

Anyway I’ve published tutorial eleven on C loop statements. Have fun. I’ll get it added to the tutorials page shortly..

GitHub Gameoff-500 entries

GitHub Gameoff-500 entries

Game Off 2020Between November 1st and December 1st, GitHub organised the Game Off 2020 where programmers submitted their own original games on the theme moonshot. There were 500 submitted and you can view them ordered by score here.

The top scoring entries are shown here on GitHub which also explains what programming language and toolkits were used. It’s interesting that of the 500 entries more than half were for Windows and approximately the same number are browser playable. The breakdown is Browser playable (262), Windows (252), macOS (108), Linux (96) and Android (8).

The GitHub link shows the programming language and C# + Unity seems to make up a large proportion as well as Godot +GdScript. But I haven’t been through them all and it would be nice to see if there are any that are C + SDL.


Some thoughts about game AI

Some thoughts about game AI

Image by Dimitris Vetsikas from Pixabay

When I say AI, I mean control logic for computer played units in a game, not Ai as in Machine Learning or Artificial Intelligence. For instance in the Empire type game, that I started (On the Games source link) which has ships and units fighting on land and sea, capturing cities and building new units.

An AI for an Empire type game has a lot of logic to be implemented. I thought about this quite a bit and came up with a task based Ai system. A neutral city is spotted by an exploring ship. If its a transport with armour on infantry then it just attacks the city and hopefully captures it.

If not a task is created. This comprises a number of steps.

  1. Locate units nearest to the city that aren’t allocated to the task.
  2. If the units are on the same continent then just send them to attack the city.
  3. If not Locate the nearest transport and send it to rendezvous with the units at the nearest point to them. Load the units.
  4. If there aren’t any units free then locate the nearest friendly city that’s not tasked with anything and task it to build the units.
  5. If there’s no transport, have the city build a transport after building the armour/infantry units.

That’s just one for task but what if a friendly city is threatened and the tasked units are the nearest. Care has to be taken so that units aren’t flip flopped, getting orders one turn then being pulled away three turns later to a different task.

Each task should be given a time-to-live (say 30 turns), long enough to acquire the resources (units it needs) to do the job and reach the target. The rule then is no diverting units on a task unless a dire emergency (like threatening one of its cities) and those are the nearest.

There’s nothing worse than allocating units to a task then seeing enemy units sail right past unopposed. Writing code to deal with different situations and priorities is not an easy task


An amazing repository of articles on Chess programming

An amazing repository of articles on Chess programming

Chess pieces
Image by Steve Buissinne from Pixabay

When I went to University, I stayed in the halls of residence and there was a final year student there who was into chess programming- heady stuff for a first year student. It’s always held a bit of a fascination for me – I’ve been playing chess since the age of 11 though never particularly well. I’ve found most chess programs could beat me unless I take a great deal of care and spend a long time thinking.

The Chess Programming Wiki has almost 4,000 articles on all aspects of chess programming across 7500 pages. If you are interested in chess programming and unfamiliar with this then you are missing out.  This uses the mediawiki software (same as Wikipedia) so can be a little opaque. I’ve found if you click the Special pages link on the left then All pages that it gives a much wider overview.

A bit of searching found the programming languages page and disappointingly there is no entry for C although there is a page on C in the Wiki!  This links to CFish, a C port of the Stockfish open source chess engine which is mostly C++. If you can write a program to beat beat Stockfish then you are indeed an awesome programmer!

Of course, AlphaZero has recently dominated play in chess, Go and Shogi. What makes this different is that its mostly self taught using an AI technique called reinforcement learning. You just tell it the rules of chess. This contrasts with “traditional” chess programming where moves are determined ahead and evaluation routines called.



Pacman in C99 that runs in the browser

Pacman in C99 that runs in the browser

PacmanSuch a simple game yet still incredibly popular. Pacman is now 40 years old. I must confess, its not one that I was great at (that would be Battle Zone- the 3D wireframe tanks on the moon game- I could play that for an hour for just 10p),

This cross-platform implementation by German programmer Andre Weissflog (aka Flooh) looks spot on and plays it as well. Most of the game has been implemented in one C source file and its definitely worth reading the comments at the top of that file to see how things are done.

For instance “audio output works through an actual Namco WSG emulator which generates sound samples for 3 hardware voices from a 20-bit frequency counter,  4-bit volume and 3-bit wave type (for 8 wavetables made of 32 sample values each stored in a ROM dump)”.

The game uses the author’s Sokol libraries which are Simple STB-style cross-platform libraries for C and C++, written in C. It’s the first time I'[d come across these and they’re worthy of a separate look at. STB is a another library that I’ll also look at in the future. I’ve added a link to this Pacman to the C Code library. Happy Christmas!

PS. Also included in the GitHub download is The Pac-man dossier by Jamey Pittman, which is a 48 page PDF and tells you everything about playing Pacman. Stuff like the logic of gameplay, mechanics, easter eggs and the hardware specs of the original game boards not to mention the infamous level 255 overflow.

Interestingly the original hardware was a 3MHZ Z80A with 2 KB of RAM and 16KB of ROM on a 224 x 288 16 colour screen .


I added an if and switch tutorial

I added an if and switch tutorial

Teachjer and blackboard
Image by mohamed Hassan from Pixabay

The next tutorial is Simple control flow in C. This demonstrates how to do if, if else and then switch statements. These are pretty siomple concepts but they are fundamentals so you do need to know them.

One thing I didin’t show in the switch tutorial is that you can mix in labels for that real write-only code experience. It’s not a technique I recvommend which is why its NOT in the tutorial but if you promise never to use this except in the rarest of circumstances, here’s what I mean.

#include <stdio.h>

int main() {
	int a = 10;
	switch (a) {
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
	case 6:
		printf("a ==5 or a ==6\n");
	case 7:
	case 8:
	case 9:
		printf("a <9\n");
	case 10:
		goto fred;
		printf("a not in range 1-10\n");

The label is the line fred: and you can see in case 10 that after printing “a=10\n”, it jumps to fred which is just after the 5 case. That does nothing and falls through to the 6 case where it prints out “a ==5 or a ==6\n”. It’s not recommended but at least now if you ever see a jump to a label in a switch statement you can be sure that the programmer was not really at the top of his or her game.

I like web games

I like web games

IllyriadNot the Flash type games you get on sites like Kongregate but strategy games like Illyriad, (Pictured) Though you can waste an awful lot of time playing them. The web is an excellent platform for certain of multiplayer games. Heck you can even play games like Quake III which was a desktop game but redone using WebAssembly.

I used to play a strategy game Inselkampf (German for Island war) where you start off with one island and improve it then you can start building ships and invading other islands. I remember getting to the point where I was managing an empire of 80 islands and the only way I could do track all the details was with an Excel spreadsheet. But it was a big time hog, taking up over and hour and a half each day (just mad!) and I stopped playing.

It seems to have closed down a few years ago which is a shame as it was very popular in the mid 2000s. The UK website just has a start Game Over message on it! Searching about I even found a copy of the Inselkampf rules online.

Given my postal games background (I created three Play by Mail games back in the late 80s, two of which are still run today on ) I still have an inkling to create a big web or mobile playable game. I’m not saying I’ll make a £million like  I’m not a great fan of web development (well the JavaScript part of it) but I’m currently studying a Udemy course on Blazor which is Microsoft’s take on C# and WebAssembly.  This lets you create websites in C# running in the browser. And C# I am most definitely a fan of.