Game of Life
Game of Life describes a game or better a rule. The rule works on a two-dimensional grid like a squared paper. Every cell can have one of two states: death or alive (black or white). To start the game, you can set some cells alive randomly. The next step is to apply the rules to get the next state. This goes on and on.
Cell is death:
- the cell get alive, if the surrounding amount of alive cells is three
- otherwise it stays death
Cell is alive
- cell dies, if the surrounding amount of alive cells is more then three
- cell stays alive, if the surrounding amount of alive cells is three or two
- cell dies, if there are less then two alive cells around
Every cell is controlled BEFORE some change get apply. You need to safe the next death/life status in additionally memory. To understand this, here an example:
Forth step with fixed border
I think you got it. There are two options to apply the rules on the border. My program will use fixed borders because it is easier to code.
- The pattern of the start should be saved in a text file
- The program reads this file in the same directory
- The program calculates every step and shows the result immediately
- With "p" you will pause the program
- Any other button will return the calculation
- With ESC you will quit the program
Litte bit more informations
- I will use "paging" to get smoothly transitions (1)
- The result for one complete calculation is saved in the page which is not used (1)
- The program runs in "text mode" with 80 columns and 25 rows (2)
(1) If you want to calculate the next picture, you must save the result of every cell. You need also a place with the size of 80x25 to save all informations. If the calculations are done, the program could go throw this field and draw pixel of pixel (or in my case char of char) on the screen. It depends on your computer effort, how fast the picture is loaded. If the pointing starts on the top left and ends on the bottom right, you will perhaps see this proceeding. To avoid this, graphics cards provide for some modes (here: 02h text, 4 screens) several screen pages. If the first screen is active, you can shape a graphic on the second screen without any effect on the active screen. With a command, you can change the second screen to active screen and the shaped graphic gets on the screen at once. The advantage in short form: You have memory space and a smooth transitions.
Read pattern from file
I decided to represent death cells as char "." and live cells with "O" (not the number). The content of the text file must be a matrix of chars with 80 times 25.
Note, that the line break must be Windows like "CR + LF" (13, 10) and not *nix "LF" (10). The first thing that the program must do, is also to read the text file an save it on the screen (with paging). I use the BIOS and DOS functions to do this.
The program described in few steps
- Jump over determined variables/data
- Open file "field.txt" and load the pattern to active screen
- Copy the boundary of the active screen to passiv screen
- Start the algoritm
- Check if a keyboard key is pressed
- Pause or quit the program if "p" or "q" is pressed
- Otherwise return calculation
More detailed informations
- Some important informations are the grid size (25x80), the filename who gets loaded and the chars who are used. We need also a few counter variables, a small buffer to read the field and some for the screen change
- The content of the file "field.txt" will be opened and readed with a DOS function (3D and 3F). The content were read from left to right and from top to bottom. It read each character and check its status and set the equivalent char to the screen. To skip the "line break" I move the pointer to two bytes right with the DOS function 42. The transmission is completed if the bottom right corner is reached
- The algorithm checks every cell inside the boundary (because the boundary doesn't change). So the cursor moves again from left to right and from top to bottom. On every position, a function counts the number of living cells around. Depending on this result, the cell on the current position at the passiv screen is set or not. At the end, the screen changes (passiv to activ) and you see the calculated next generation
- If you press a key on the running program, the number of the key will be saved in a buffer. Always on the end of a whole generation calculation one key will be checked (the top one of the stack). If you press two keys in this time, the second key is taken into account only in the next run
You need both of them in the same directory. The source code is commented.
compile with NASM assembler
nasm -fbin lifegame.asm -o lifegame.com
If you have some questions please feel free and write me an email.