辅导CS253 HW7程序、讲解C++程序、data留学生编程辅导 辅导留学生Prolog|解析SPSS
- 首页 >> Matlab编程 CS253 HW7: Life in Jail
Changes
Updates to the assignment will be noted here. None yet!
Description
HW7 has all the same requirements & restrictions as HW6, 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:
nw n ne
w me e
sw s se
or
A B C
D E F
G H I
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().
Sample Run
Here is a sample run,
where % is my shell
prompt:
2020/11/29 CS253 | Main / HW7
https://cs.colostate.edu/~cs253/Fall20/HW7 2/3
% cmake .
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';
}
Tar le
The tar le
for this assignment must be called: hw7.tar
It must contain:
source les
(*.cc), including Board.cc & Rule.cc
header les
(*.h), including Board.h & Rule.h
CMakeLists.txt, which will create the library le
libhw7.a.
These commands must produce the library libhw7.a:
cmake . && make
Your CMakeLists.txt must use at least -Wall when compiling.
How to submit your work:
In Canvas, check in the le
hw7.tar to the assignment “HW7”.
How to receive negative points:
Turn in someone else’s work.
Changes
Updates to the assignment will be noted here. None yet!
Description
HW7 has all the same requirements & restrictions as HW6, 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:
nw n ne
w me e
sw s se
or
A B C
D E F
G H I
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().
Sample Run
Here is a sample run,
where % is my shell
prompt:
2020/11/29 CS253 | Main / HW7
https://cs.colostate.edu/~cs253/Fall20/HW7 2/3
% cmake .
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';
}
Tar le
The tar le
for this assignment must be called: hw7.tar
It must contain:
source les
(*.cc), including Board.cc & Rule.cc
header les
(*.h), including Board.h & Rule.h
CMakeLists.txt, which will create the library le
libhw7.a.
These commands must produce the library libhw7.a:
cmake . && make
Your CMakeLists.txt must use at least -Wall when compiling.
How to submit your work:
In Canvas, check in the le
hw7.tar to the assignment “HW7”.
How to receive negative points:
Turn in someone else’s work.