![]() ![]() We'll also declare a sleepIteration const so we can easily change how long each iteration should be displayed (this will be useful when debugging. Before you start the game, you need to provide an initial state. A cell can either be dead or alive (alive cells are coloured blue in our demo). The rules are as follows: Each cell lives in a square in a rectangular grid. While we are defining constants, let's go ahead and declare a few more like the Unicode characters to represent dead vs living cells (I'll be using brown and green squares but feel free to use whatever you want), as well as an ANSI escape sequence to clear our screen each iteration (we want the appearance of animation). Conway's Game of Life is a game invented by mathematician John Conway in 1970. As simple as these rules may be, they can result in surprising complexity from relatively simple initial configurations. ![]() If an empty cell has exactly three neighbors, it becomes living otherwise, it remains dead. use X to clear/reset the grid and generate automatons at random positions. You could make these whatever you want, just be aware that it might look strange depending on the size of your terminal. If a living cell has two or three neighbors, it remains living otherwise it dies of loneliness or overcrowding. Let's import the packages we need and define some constants for the dimensions of the grid. I will also make use of some unicode characters to make the display of our game a little more visually appealing.įirst we need to make our grid. As far as packages go, we will need fmt to print our array, math/rand to generate our initial grid, and time so we can observe each iteration of the game. Because a cell can only be in one of two states (no quantum funny business here), we will use booleans as the type in our array of arrays.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |