cd
mkdir cs313
chmod go-rwx cs313
Within this directory, create a directory "hw2" and go to it:
cd cs313
mkdir hw2
cd hw2
You are now ready to create the files for this assignment. All
Pascal files should end in ".p". To get started, look at the
example Pascal programs on benjerry in
~schar/cs313/pascal/. To compile a Pascal program,
say, "hello.p", simply use the command "fpc hello.p".
This will result in an executable "hello" which you can run by
typing "./hello"
(it will also create an object file "hello.o").
When you are done, hand in printouts of your code. On the first page, write your own name as well as the names of the students with whom you collaborated. Don't forget to answer question 4! Also submit your programs electronically by typing "submit313" in the directory containing your Pascal files.
will change into. . . . . . . # . . . . # . . . . # . . . . . . .
because the top and bottom cells die (1 neighbor), the middle cell survives (2 neighbors), and the two cells left and right of the middle are born (3 neighbors).. . . . . . . . . . . # # # . . . . . . . . . . .
The borders of the grid deserve special attention. There are two approaches: either, pretend border cells don't have neighbors beyond the border; or, pretend the grid "wraps around" both from the bottom to the top and from the left to the right, so every cell in the grid has exactly 8 neighbors. For full credit, you should implement the second option, which is also the way my sample program behaves. Hint: the "mod" function might come in handy here - but avoid taking the "mod" of negative numbers. By the way, a grid that "wraps around" in this way is topologically equivalent the surface of a torus (i.e., an object shaped like a doughnut).
Your initial population should be random, with a probability of 1/2 for each cell to be alive. You can use the function random(2), which will return either 0 or 1. To get different random numbers every time you run the program, call randomize at the beginning of your main program.
Use the following procedures to clear the screen or reposition the cursor (which make use of vt100 control sequences).
procedure clear;
{ move cursor to top left corner and clear screen }
begin
write(chr(27)); { ascii code for escape }
write('[2J') { control sequence to clear screen }
end;
procedure home;
{ move cursor to top left corner of screen }
begin
write(chr(27)); { ascii code for escape }
write('[f'); { control sequence to move cursor home }
end;
procedure backspace(n : integer);
{ move cursor to the left n spaces }
var i : integer;
begin
for i:=1 to n do
write(chr(8)); { ascii code for backspace }
end;