CPSC 225, Spring 2002
Programming Assignment 4
Using Files in the "Game of Life"
FOR THE PREVIOUS PROGRAMMING ASSIGNMENT, you implemented John H. Conway's Game of Life, using the ncurses library. However, two of the menu commands, "Save To File" and "Load From File" were left unimplemented. The fourth programming assignment is to implement these commands.
On Thursday afternoon (February 14), I will post source code for the third programming assignment in the directory /home/cs225/life. If you have not successfully completed the third assignment, you might want to use my source code as a basis for your work on the fourth assignment. The compiled programs, life and enhanced_life in the directory /home/cs225/life, already implement the commands for working with files.
This assignment is due next Wednesday, February 20.
The "Save To File" command should save the current contents of the playing board to a file. The "Load From File" command should be able to read a file that was created by the "Save To File" command, and restore the state of the board. It should also be able to read my sample input files, such as /home/cs225/life/glidergun.life. Both commands should prompt the user for the name of the file. You can't use cin and cout in curses mode. However, to read the file name, you can temporarily leave curses mode, by using the endCurses() function, in order to get the file name from the user. Then you can use startCurses() to re-enter curses mode. (Alternatively, you could write a routine for getting a string from the user in curses mode. To do this, you would have to track each of the user's key presses. This is far more difficult than just leaving curses mode to get the user's input, but it would be nice to have a general curses string-input function.)
Your program must be able to read files in the following format (which I use for my own files): The first line of the file contains two integers, separated by a space, giving the number of rows and the number of columns on the board. This is followed by the contents of the board, with a period (".") standing for an dead cell and an asterisk ("*") standing for a living cell. The data for each row is followed by and end-of-line character. Remember that when you use >>, it always skips spaces and ends-of-line before reading any data, so if you read the file with >>, the ends-of-line are not significant.
You should not assume that the number of rows and columns in the data is the same as the number of rows and columns on your board. You can discard any extra data in the file, or add extra empty cells, as necessary.
An ideal solution will: Not overwrite an existing file without asking first; Give an error message if an input file doesn't exist; Give an error message if the input file does not contain legal data.
David Eck, 13 February 2002