CS253 HW7: Life in Jail
Changes
- The sample output was missing a trailing blank line; it’s now there.
- The return value of
.golly()
after a call to .jail(string)
is undefined. Return what you like, throw an error, whatever works
for you.
This only applies if .jail(string)
was the most recent call of
the .conway()
/.golly(string)
/.jail(string)
family. If
.conway()
or .golly(string)
is called again after a call
to .jail(string)
, then the return value from .golly()
is
defined again.
- You will create the library
libhw7.a
.
Description
HW7 has all the same requirements & restrictions as HW6
(except that you’re creating libhw7.a
. not libhw6.a
)
with the addition of a single method.
                
Methods
Rule
must have the following additional public method:
-
.jail(string
jail-program)
-
If this method is called, then instead of using Conway’s rule from
HW2, or a Golly rule from
HW4, use the jail-program to evaluate whether or not a cell
lives or dies when
.eval()
is called.
The Jail program will expect nine arguments, which will be placed in
the variables A
…I
, that indicate the immediate neighboorhood
of the cell in question, including the cell itself. Each argument
will be 0
if the cell is dead, and 1
if the cell is alive.
The program must return 0
if the cell should be dead in the next
generation, and 1
if it should be alive.
The nine arguments represent the adjacent cells and the cell itself,
and correspond to the nine arguments to Rule::eval()
, like this:
or
That is, E
is the cell in question. D
is its neighbor to the
left, H
is its neighbor below, and so on.
If there are any bad tokens in the Jail program, throw a
runtime_error when Rule::jail()
is called. As in HW5, other
errors (mismatched if
/fi
, if a b <
, etc.) must result in a
thrown runtime_error, either when Rule::jail()
is called, or
when the program is actually executed via Rule::eval()
.
The Jail program is not executed when Rule::jail()
is called.
Rule::jail()
performs lexical analysis on the Jail program, and
remembers it. Later, when Rule::eval()
is called, the Jail
program is executed (interpreted). Of course, for any Board
,
computing the next generation will require calling Rule::eval()
many times, once for each cell.
It is valid to call Rule::conway()
, Rule::golly()
, or
Rule::jail()
in any combination, several times. The last call
determines the future behavior of Rule::eval()
.
Debugging
If you encounter “STACK FRAME LINK OVERFLOW”, then try this:
export STACK_FRAME_LINK_OVERRIDE=ffff-ad921d60486366258809553a3db49a4a
Sample Run
Here is a sample run, where %
is my shell prompt:
                
% cmake .
… cmake output appears here …
% make
… make output appears here …
% cat blinker
....
.O..
.O..
.O..
....
% cat block3
''''
'**'
'*''
''''
% cat test.cc
#include "Board.h"
#include "Rule.h"
#include <iostream>
using namespace std;
int main() {
Rule r;
r.jail(R"(# Conway’s rule in JAIL
# A B C
# D E F
# G H I
# Accumulate count in C:
c += a c += b c += d c += f
c += g c += h c += i
if C = 2 return E fi # Two neighbors means survive
if C = 3 return 1 fi # Three neighbors mean born or survive
return 0 # Death otherwise
)");
Board b("blinker", r);
cout << b << '\n';
for (int i=0; i<3; i++)
cout << ++b << '\n';
r.jail("return a # move down to the right");
Board b3("block3", '*', '\'', r);
cout << b3 << '\n';
for (int i=0; i<3; i++)
cout << ++b3 << '\n';
}
% ./test
....
.O..
.O..
.O..
....
....
....
OOO.
....
....
....
.O..
.O..
.O..
....
....
....
OOO.
....
....
''''
'**'
'*''
''''
''''
''''
''**
''*'
'''*
''''
''''
*''*
**''
*'''
''''
''''
Tar file
- The tar file for this assignment must be called:
hw7.tar
- It must contain:
- source files (
*.cc
), including Board.cc
& Rule.cc
- header files (
*.h
), including Board.h
& Rule.h
CMakeLists.txt
, which will create the library file
libhw7.a
.
- These commands must produce the library lib
hw7.a
:
cmake . && make
- Your
CMakeLists.txt
must use at least -Wall
when compiling.
How to submit your work:
In Canvas, check in the
file
hw7.tar
to the assignment
“HW7”.
                
How to receive negative points:
Turn in someone else’s work.