CS253: Software Development with C++

Fall 2020

HW 7

CS253 HW7: Life in Jail

Changes

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 AI, 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:
nwnne
wmee
swsse
 or 
ABC
 D  E  F 
GHI
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

    cmake . && make

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.