hw4-koch.c
hw4-madlib.c
hw4-life.c
.
These programs are to be electronically submitted by using the submit program on csa. The code you submit should conform with the program assignment guidelines.
Write a program to display Koch's snowflake using the turtle graphics routines to do the drawing. Since Roberts does not assume a turtle graphics library, some of what he says should be ignored, since you can do things more easily now that you have turtle graphics to work with. (Ignore Robert's comments about DrawPolarLine(), for example, since having the turtle "remember" its direction and able to draw forward makes this problem even easier.) For the turtle graphics, you may link your program against my turtle.o as in the Makefile. In any case, you only need to submit the main program. I will test it by linking it with my turtle package.
The function(s) which you write for drawing the snowflake must be recursive. In particular, there should be NO LOOPING CONSTRUCTS in your program. (No for, while, do, etc.)
You should write your program by filling the missing comments and code in the following shell:
/* program shell for hw4, fall 1997 */
/* edit this file, adding CODE and COMMENTS as necessary. */
#include <stdlib.h>
#include "turtle.h" /* a version of turtle.h has been placed where
* it can be read */
#include "simpio.h"
#include "genlib.h"
const int MAX_DEGREE = 5;
void GetParameters(double *lengthPtr, int *degreePtr);
void DrawKochSnowflake(double length, int degree);
int main(void)
{
double length = 1;
int degree = 1;
InitTurtle();
GetParameters(&length, & degree);
/*
* position and orient turtle to keep it roughly centered
* move up 2/3 of length, then turn right 150 degrees so turtle is
* ready to move down the right side of the "triangle" which forms basis
* for the snowflake.
*
* /\ <- start here and go down right side first
* / \
* / \
* /______\
*/
MoveTurtle(length * 2.0 / 3.0);
TurnRight(150);
DrawKochSnowflake(length,degree);
return(0);
}
void GetParameters(double *lengthPtr, int *degreePtr)
{
printf("Enter the length of one side: ");
*lengthPtr = GetReal();
printf("Enter degree of snowflake (max = %i): ",MAX_DEGREE);
*degreePtr = GetInteger();
if (*degreePtr > MAX_DEGREE) {
*degreePtr = MAX_DEGREE;
printf("Your degree is too large. Using %d instead.",*degreePtr);
}
if (*degreePtr < 0) {
*degreePtr = 0;
printf("Your degree is too small. Using %d instead.",*degreePtr);
}
}
Given the shell above, there are actually very few lines of code required
for this program. The difficult part is figuring out how to solve the problem
recursively. One hint: you will probably want to think about a Koch
snowflake as a triangle made of three Koch lines.
One cell of the board may be designated as the "home base". To make the home base visible, it is marked with O when alive and : when dead. Note that the home base, may not be at the center of the displayed portion of the board. Some notes on the transitions from one time to the next are given below the example. Note that I have only displayed as much of the board as necessary to see the live cells.
t=0 t=1 t=2 t=3 t=4
....... ....... ....... ....... .......
.ooOoo. ..ooo.. ...o... ...o... ..ooo..
....... ..oOo.. ..o.o.. ..ooo.. .o...o.
..ooo.. .o.:.o. .oo:oo. .o.:.o.
....... ..o.o.. ..ooo.. .o...o.
...o... ...o... ..ooo..
....... ....... .......
t=5 t=6 t=7 t=8
......... ......... ........... same as t=6
....o.... ...ooo... .....o.....
...ooo... ......... .....o.....
..o.o.o.. .o.....o. .....o.....
.ooo:ooo. .o..:..o. ...........
..o.o.o.. .o.....o. .ooo.:.ooo.
...ooo... ......... ...........
....o.... ...ooo... .....o.....
......... ......... .....o.....
.....o.....
...........
In this sequence:
Write a program which simulates Conway's Game of Life. Your program should take a file name as an argument, read from that file the initial configuration and simulation information and then simulate the game as specified in the file. Here is a shell which gets the file name from the argument list for you and also demonstrates a possible structure for the program. (Other structures are possible, some are actually better.)
#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#indlude "strlib.h"
int main(int argc, string argv[])
{
string fileName;
int roundsToSimulate, roundsToDisplay;
lifeBoardT board;
/* if syntax is wrong, exit with error message */
if (argc != 2) {Error("Syntax: $s ", argv[0]);}
fileName = CopyString(argv[1]);
ReadGameFile(fileName, &board, &roundsToSimulate, &roundsToDisplay);
SimulateGame(board, roundsToSimulate, roundsToDisplay);
return(0);
}
12 11 9 9 ........... ........... ........... ........... ........... ........... ...ooOoo... ........... ........... ........... ........... ...........
lifeBoardT (using struct) which
can hold all of the information you need about a board (size, contents)
and then write a series of functions like GetBoard(),
PrintBoard(), UpdateBoard(), etc which allow
you manipulate the board.
array[r * 5 + c] can be used
to access the thing in row r, column c of the conceptual 2-d array.
(Be sure you understand this arithmetic.)
This arithmetic could be hid inside a function so that you only have to
worry about it in one place.
Extra Credit.
As we have defined the game, the board is finite. One could consider a
version of the game where the board is infinite, but the amount of "life"
is always finite. Any squares "off the edges" of the displayed portion of
the board are considered dead.
In this version, life can spread beyond the original edges of the
board. To implement this, you need to dynamically allocate new space for
the board when this happens. (This is somewhat like
GetDynamicIntegerArray.)
The output would then look just like the example above, the board growing
as the life spreads beyond the edges.
You may program a version which allows for a "growing board" for extra credit. You might want to get the required version running first, though, just to be safe. If you do it wisely, it should not cost you any time to do the required version first and then extend it. Your boards do not have to shrink if the live cells near the edges start dying off.
Makefile.
Makefile.a4,
you will have to use the mv (short for move)
command to rename it by typing
mv Makefile.a4 Makefile
cp command to copy it by typing
cp Makefile.a4 Makefile
make -k. (The -k means "keep going", which instructs
make to build as much as it can, rather than quit after the first error.)
Do not allow your work to be used by others:
Warning: If someone cheats by using your work, you will also be penalized.