Hello again! In this video, we are going to have a practical on Conway's Game of Life. Before we get started,
there are a couple of things we need to think about.
The first one is that C++ does not, in fact, have a standard graphics library.
There was talk of adding one in C++20, but unfortunately that did not happen.
We could use a third-party graphics library, but that is a bit of a support headache! So I would prefer to
save that for later in the course.
So we're going to implement this solution in text mode.
The other issue is that a cellular automaton requires an infinite grid, and obviously our computer
only has a finite amount of memory.
So we need somehow to restrict the size of the grid.
What we are going to do is, we are going put a border of cells around the actual simulation, and the cells
in that border are going to be permanently unpopulated.
So even if the rules of Life say that one of these border cells should become populated, we are going
to ignore that, and leave it empty.
So this means that the cells are going to be restrained within that border, and they cannot move outside
the border of our simulation.
We are going to use a two dimensional array to represent the grid. Hence the last video!
The elements
of this array are going to be a class which represents a cell on the grid.
The game is going to be played inside a standard 80x24 terminal, in text mode, and we are going to use
so-called "ANSI escape codes" for displaying these cells on the terminal.
In the days before everyone had a computer on their desk at work,
people used to have so-called "dumb terminals".
This was something that looked like a keyboard, and something that looked like a television screen.
So you type something in on the keyboard.
It would go to a central computer and the central computer would send something back, which was displayed
on the terminal.
And there are various commands for changing the way that the output appears.
There is actually a standard, made by ANSI: the American National Standards Institute, I think.
And these can be used by programs, to change the way that output is displayed.
So you can use different coloured text, you can make the text bold or italic, and so on.
And what we are interested in here is, you can make text appear at certain positions on the screen.
These ANSI codes all begin with the escape character. So that that corresponds to pressing the escape key on
your keyboard.
You do not actually enter these codes yourself.
They are something that the program sends to the terminal.
So we have the escape character, followed by a square bracket. And 1b is the hexadecimal code for
"escape",
in the ASCII character set. The commands we are going to use are to move the cursor to a given row, and
a given column within that row, and then display a character.
To do that, you have the escape character, then the square bracket, then the row number, semi-colon, column
number and capital H. And it has to be a capital, not lower-case.
The other one is to clear the screen and return the cursor to the "home" position.
So that means the top left of the screen.
And that is the escape character, square bracket, two, capital J.
Nowadays we have computers with graphical windowing environments, and they have consoles which can emulate
one of these "ANSI-compatible" terminals.
When I first thought up this exercise, I thought it would be nice and simple, but it is actually not quite
as simple as I hoped for.
If you have something which is descended from Unix, so that includes OSX and Linux, then the terminal
should normally support this automatically.
If you're perhaps running in full-screen text mode, or using single-user mode, if your terminal does
not support these, then there is an environment variable "TERM".
And you can set that to
some value, which represents terminal which does support ANSI commands.
For Windows,
it is a bit more complicated. If you are using MinGW or Cygwin for your C++ compiler environment.
These emulate a UNIX environment, so they support ANSI commands.
If you're using PowerShell 5.1 or later to run your program, or Windows Terminal. Which is not the default
command line, that's a separate program.
If you have one of those, then that will support the command codes. There are also some third-party
terminal programs you can install. If you are using the default command line in Windows, which is the
one that pops up when you run Visual Studio.
This does support ANSI commands, if you are using Windows 10, and if you put in some extra code to
enable it.
So - I will give you the code
to do that.
So the actual logic of the program itself. We are going to have two grids: one which will represent
the current generation, and one which will represent the next generation.
And then we are going to calculate if the cells in the current generation will survive or be populated
in the next generation.
So we start off by creating a grid object, to represent the initial generation.
We populate the cells in that initial grid at random. Then we display it.
Then we create a second grid object, which will represent the next generation.
And then we go through, and calculate which cells will survive from this generation to the next generation,
and which ones are going to be newly populated.
And then we populate the next generation grid.
Then we copy this grid over the current generation grid.
So, what was the next generation now becomes the current generation, and the first generation is superseded.
And then we display the new values of the current generation grid.
And then we go back.
We create another grid for the next generation.
We calculate what cells will be in that next generation.
Then we copy over the current generation, display the current generation, and so on.
So it is really all one big loop.