WEBVTT

00:00.090 --> 00:07.050
Hello again! In this video, we are going to have a practical on Conway's Game of Life. Before we get started,

00:07.050 --> 00:09.630
there are a couple of things we need to think about.

00:09.960 --> 00:14.340
The first one is that C++ does not, in fact, have a standard graphics library.

00:15.090 --> 00:19.830
There was talk of adding one in C++20, but unfortunately that did not happen.

00:20.880 --> 00:28.560
We could use a third-party graphics library, but that is a bit of a support headache! So I would prefer to

00:28.560 --> 00:30.120
save that for later in the course.

00:30.780 --> 00:33.840
So we're going to implement this solution in text mode.

00:35.070 --> 00:40.920
The other issue is that a cellular automaton requires an infinite grid, and obviously our computer

00:40.920 --> 00:42.960
only has a finite amount of memory.

00:43.470 --> 00:46.170
So we need somehow to restrict the size of the grid.

00:46.920 --> 00:52.800
What we are going to do is, we are going put a border of cells around the actual simulation, and the cells

00:52.800 --> 00:55.920
in that border are going to be permanently unpopulated.

00:56.370 --> 01:02.040
So even if the rules of Life say that one of these border cells should become populated, we are going

01:02.040 --> 01:04.080
to ignore that, and leave it empty.

01:04.650 --> 01:10.530
So this means that the cells are going to be restrained within that border, and they cannot move outside

01:10.530 --> 01:12.170
the border of our simulation.

01:15.070 --> 01:19.990
We are going to use a two dimensional array to represent the grid. Hence the last video!

01:20.770 --> 01:21.460
The elements

01:21.460 --> 01:25.210
of this array are going to be a class which represents a cell on the grid.

01:26.320 --> 01:34.390
The game is going to be played inside a standard 80x24 terminal, in text mode, and we are going to use

01:34.390 --> 01:39.370
so-called "ANSI escape codes" for displaying these cells on the terminal.

01:42.440 --> 01:46.550
In the days before everyone had a computer on their desk at work,

01:46.820 --> 01:49.190
people used to have so-called "dumb terminals".

01:49.640 --> 01:53.330
This was something that looked like a keyboard, and something that looked like a television screen.

01:53.810 --> 01:55.520
So you type something in on the keyboard.

01:55.850 --> 02:00.260
It would go to a central computer and the central computer would send something back, which was displayed

02:00.260 --> 02:00.920
on the terminal.

02:02.120 --> 02:05.930
And there are various commands for changing the way that the output appears.

02:06.350 --> 02:11.810
There is actually a standard, made by ANSI: the American National Standards Institute, I think.

02:12.290 --> 02:16.010
And these can be used by programs, to change the way that output is displayed.

02:16.550 --> 02:20.870
So you can use different coloured text, you can make the text bold or italic, and so on.

02:21.620 --> 02:26.780
And what we are interested in here is, you can make text appear at certain positions on the screen.

02:29.300 --> 02:34.490
These ANSI codes all begin with the escape character. So that that corresponds to pressing the escape key on

02:34.490 --> 02:35.090
your keyboard.

02:35.600 --> 02:37.460
You do not actually enter these codes yourself.

02:37.460 --> 02:39.860
They are something that the program sends to the terminal.

02:40.700 --> 02:46.700
So we have the escape character, followed by a square bracket. And 1b is the hexadecimal code for

02:46.700 --> 02:47.210
"escape",

02:47.720 --> 02:55.460
in the ASCII character set. The commands we are going to use are to move the cursor to a given row, and

02:55.460 --> 02:58.790
a given column within that row, and then display a character.

02:59.480 --> 03:05.210
To do that, you have the escape character, then the square bracket, then the row number, semi-colon, column

03:05.270 --> 03:09.470
number and capital H. And it has to be a capital, not lower-case.

03:10.370 --> 03:13.730
The other one is to clear the screen and return the cursor to the "home" position.

03:14.030 --> 03:16.070
So that means the top left of the screen.

03:16.730 --> 03:20.210
And that is the escape character, square bracket, two, capital J.

03:24.670 --> 03:31.030
Nowadays we have computers with graphical windowing environments, and they have consoles which can emulate

03:31.120 --> 03:33.670
one of these "ANSI-compatible" terminals.

03:34.810 --> 03:39.400
When I first thought up this exercise, I thought it would be nice and simple, but it is actually not quite

03:39.400 --> 03:40.720
as simple as I hoped for.

03:41.440 --> 03:47.410
If you have something which is descended from Unix, so that includes OSX and Linux, then the terminal

03:47.410 --> 03:49.720
should normally support this automatically.

03:50.650 --> 03:56.170
If you're perhaps running in full-screen text mode, or using single-user mode, if your terminal does

03:56.170 --> 03:59.410
not support these, then there is an environment variable "TERM".

03:59.650 --> 04:00.880
And you can set that to

04:00.880 --> 04:05.140
some value, which represents terminal which does support ANSI commands.

04:07.990 --> 04:08.920
For Windows,

04:08.920 --> 04:15.220
it is a bit more complicated. If you are using MinGW or Cygwin for your C++ compiler environment.

04:15.850 --> 04:19.390
These emulate a UNIX environment, so they support ANSI commands.

04:21.570 --> 04:28.350
If you're using PowerShell 5.1 or later to run your program, or Windows Terminal. Which is not the default

04:28.350 --> 04:30.270
command line, that's a separate program.

04:31.050 --> 04:36.300
If you have one of those, then that will support the command codes. There are also some third-party

04:36.300 --> 04:42.450
terminal programs you can install. If you are using the default command line in Windows, which is the

04:42.450 --> 04:44.490
one that pops up when you run Visual Studio.

04:45.180 --> 04:51.370
This does support ANSI commands, if you are using Windows 10, and if you put in some extra code to

04:51.370 --> 04:51.900
enable it.

04:53.070 --> 04:54.780
So - I will give you the code

04:54.780 --> 04:55.200
to do that.

04:58.440 --> 05:03.660
So the actual logic of the program itself. We are going to have two grids: one which will represent

05:03.660 --> 05:08.280
the current generation, and one which will represent the next generation.

05:08.910 --> 05:14.970
And then we are going to calculate if the cells in the current generation will survive or be populated

05:14.970 --> 05:15.960
in the next generation.

05:17.100 --> 05:21.390
So we start off by creating a grid object, to represent the initial generation.

05:22.290 --> 05:26.250
We populate the cells in that initial grid at random. Then we display it.

05:27.840 --> 05:32.190
Then we create a second grid object, which will represent the next generation.

05:33.630 --> 05:38.340
And then we go through, and calculate which cells will survive from this generation to the next generation,

05:38.700 --> 05:40.830
and which ones are going to be newly populated.

05:41.400 --> 05:43.620
And then we populate the next generation grid.

05:44.520 --> 05:47.280
Then we copy this grid over the current generation grid.

05:47.880 --> 05:55.110
So, what was the next generation now becomes the current generation, and the first generation is superseded.

05:55.860 --> 05:59.510
And then we display the new values of the current generation grid.

06:00.060 --> 06:00.720
And then we go back.

06:00.720 --> 06:02.550
We create another grid for the next generation.

06:02.580 --> 06:05.160
We calculate what cells will be in that next generation.

06:05.700 --> 06:09.240
Then we copy over the current generation, display the current generation, and so on.

06:09.510 --> 06:10.920
So it is really all one big loop.