Tag: atoms

Tutorial on Atoms added

Tutorial on Atoms added

Atoms command lineThis is my alternative approach to learning C. Show code, explain what it does in depth rather than explaining the C language feature-by-feature. I’ve created a 90 line skeleton program for atoms. The full file is on GitHub, The file is called atoms.c.

All this does is setup the board and let you enter your move. I’ve tested it on Windows and it calls _getch() to read the keyboard so it’s fast to enter moves and you don’t have to press enter/return. It then validates the move and updates the board and draws it. No chain reactions yet or computer play but those will be in future tutorials.

In the screenshot you can see I added one atom to (3,3) and then (5,6).

The board uses three characters for each cell with atoms prefixed by P or C for player and computer.  Trying to have the computer play well will be an interesting bit of programming.


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 about.com. 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.