Wireworld
ncurses
4. Wireworld
4.1 Wireworld
Wireworld is a cellular automaton due to Brian Silverman, formed from a 2D grid of cells,
designed to simulate digital electronics. Each cell can be in one of four states, either ‘empty’,
‘electron head’, ‘electron tail’ or ‘copper’ (or ‘conductor’).
The next generation of the cells follows the rules, where n is the number of electron heads
found in the 8-surrounding cells:
• empty − > empty
• electron head − > electron tail
• electron tail − > copper
• copper − > electron head if n == 1 or n == 2
• copper − > copper otherwise
See also:
Exercise 4.1 Write a program which is run using the argc and argv parameters to main.
The usage is as follows :
$ wireworld wirefile . txt
where wirefile.txt is a file specifying the initial state of the board. This file codes empty
cells as ‘ ’, heads as ‘H’, tails as ‘t’ and copper as ‘c’. Display the board for 1000 generations
using plain text. You may assume that the grid is always 40 cells by 40
2 Chapter 4. Wireworld
Make sure all your code is fully ANSI compliant, and fully follows the house-style guidelines.
Show that your code has been developed using short, well-tested functions via the use of
assert() testing.
4.2 ncurses
C has no inherent functionality to allow printing in colour etc. Therefore, a programming library
know a ncurses was created in 1993 to allow terminals to interpret certain control-codes as
colours and other effects.
The library itself is somewhat complex, allowing keyboard and mouse events to be captured
and a whole range of simple graphics functionality. On the web page is my ‘wrapper’ for the
library, along with a program demonstrating its use. This will only work in unix-style terminals.
Note that after you begin ncurses mode (using Neill_NCURS_Init()) that you can’t print to
stdout or stderr, until you switch it off (using Neill_NCURS_Done()).
To compile the code you’ll have to use both my code neillncurses.c and also link in the
ncurses library. A typical compile might look like
gcc yourcode . c neillncurses . c - Wall - Wfloat - equal - Wextra - O2
- pedantic - ansi - lncurses -lm
If you’re running a virtual box you may also need to install the ncurses developer files,
including ncurses.h, using:
sudo apt install libncurses - dev
Some terminals do not support ncurses, so make sure you are using an ‘xterm’ or equaivalent.
Exercise 4.2 Adapt the wireworld code so that the output is displayed using this library, with
tails being red, heads being blue, copper being yellow and background being black. The main
loop will update the board, display it, and repeat until a quit event occurs (e.g. a mouse click
or the ESC key is pressed).
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。