1 / 18

Game of Life - Programming Assignment #2

Implement the Game of Life using command line arguments, dynamic memory allocation, and file reading.

rstokes
Download Presentation

Game of Life - Programming Assignment #2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Assignment #2 Professor Hugh C. LauerCS-2303 — System Programming Concepts Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie,Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by BjarneStroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel Assignment #2 -- Game of Life

  2. Game of Life Occupied cell:– Empty cell:– x 0 or 1 neighbors organism diesof loneliness 3 neighbors birth of neworganism x 4 – 8 neighbors organism diesof overcrowding x x x 2 – 3 neighbors organism survives Published by John Conway,Scientific American, April 1970 Assignment #2 -- Game of Life

  3. Game of Life Occupied cell:– Empty cell:– x 0 or 1 neighbors organism diesof loneliness 3 neighbors birth of neworganism x x 4 – 8 neighbors organism diesof overcrowding x x x 2 – 3 neighbors organism survives Assignment #2 -- Game of Life

  4. Game of Life Occupied cell:– Empty cell:– x 0 or 1 neighbors organism diesof loneliness 3 neighbors birth of neworganism x x x 4 – 8 neighbors organism diesof overcrowding x x 2 – 3 neighbors organism survives Assignment #2 -- Game of Life

  5. Game of Life Occupied cell:– Empty cell:– x 0 or 1 neighbors organism diesof loneliness 3 neighbors birth of neworganism x x x 4 – 8 neighbors organism diesof overcrowding x 2 – 3 neighbors organism survives Assignment #2 -- Game of Life

  6. Programming Assignment #2 • Implement the Game of Life ./life X Y gens input print pause • x and y are size of grid • gens is # of generations to play • Game may halt earlier • input is name of file containing initial configuration • print is 'y' or 'n' indicating whether to print each generation • pause is 'y'or 'n'indicating whether to pause and wait for a keystroke between generations Assignment #2 -- Game of Life

  7. Things you need to know • Command line arguments • K & R §5.10 • Allocating and freeing memory • malloc()and free()— K & R §B.5 • Reading from a file • K & R §7.5 • Two-dimensional arrays • K & R §5.7 Assignment #2 -- Game of Life

  8. Game of Life • Read command line to determine size of board, number of generations, etc. • Dynamically allocate memory for three boards of the appropriate size • Using malloc() • Read input file an initialize one board • Play up to n generations • After each generation, test for termination — I.e., same as previous or 2nd previous generation • Print final generation • Optional pause and/or print after each generation for debugging purposes • Free dynamically allocated memory! Assignment #2 -- Game of Life

  9. Program Organization • From this point on, every programmingassignment at WPI must be accompaniedby makefileor equivalent • Unless otherwise specified You may choose your own partition of the problem • See Lab1, Part3 • Must have • At least one .h file (your interface) • At least two .c files (together your program) • One makefile • Suggested partition • Life.c — main program, read input & initialize • Game.c — playing game, testing termination • Board.c— utility functions for printing boards, comparison, etc. • Life.h— interface with function headers, extern variable for any globals, etc. Assignment #2 -- Game of Life

  10. Command line arguments int main(intargc, char *argv[]); • argc is number of arguments • argv[i] points to a null-terminated string representing the ith argument • intatoi(char*s)converts string to int • FILE*fopen(char*f,mode) opens a file named by character string Assignment #2 -- Game of Life

  11. Allocating and freeing memory • May allocate arrays as automatic arrays (i.e., on The Stack) however • A better way is to request memory from The Heap • void*malloc(x*y*sizeof(char)) • Allocates this many bytes of storage, returns a pointer to it • voidfree(void *mallocedMemory) • Returns previously allocated memory to The Heap Assignment #2 -- Game of Life

  12. Reading from a file • FILE *fopen(char *filename, char *mode); • Opens the file named for the string at filename • mode = "r"  read-only • Don’t forget to close using fclose() • FILE *f; • intfgets(f) reads one character from file f Assignment #2 -- Game of Life

  13. Setting up two-dimensional array • Impossible to have dynamic 2D arrays in C • Instead • Allocate one array A of x*y elements • i.e., x rows of y elements each • All contiguously allocated in memory • Allocate a second array B of x pointers • Initialize ith pointer to &A[i*y*sizeof(element)); • To access element in ith row, jth column • B[i][j] Assignment #2 -- Game of Life

  14. Two-dimensional array (continued) • char *B[x] • char A[x*y] ... ... ... ... ... ... ... A[0] A[1] A[2] A[3] A[4] A[y-1] Assignment #2 -- Game of Life

  15. Two-dimensional array (continued) • char *B[x] • char A[x*y] j ... ... ... ... ... ... ... i Element B[i][j] Assignment #2 -- Game of Life

  16. Program Organization • See Lab1, Part3 • Must have • At least one .h file (your interface) • At least two .c files (together your program) • One makefile • Suggested partition • Life.c — main program, read input & initialize • Game.c — playing game, testing termination • Board.c— utility functions for printing boards, comparison, etc. • Life.h— interface with function headers, extern variable for any globals, etc. Assignment #2 -- Game of Life

  17. Game.c void PlayOne(int width, int height, char Old[][], char New[][]); • Loop thru Old array in two nested loops (rows and columns) • Corresponding value in New array depends upon nearest neighbors in Old • Suggested supporting function:– • char countNeighbors(Old[][], x, y, width, height); • Counts number of adjacent occupied cells, so that PlayOne() can determine cell value in New array • Two nested loops • Adjust for boundary conditions Assignment #2 -- Game of Life

  18. Questions? Assignment #2 -- Game of Life

More Related