
/* Program plays John Conway's "Game of Life" */
#include "life.h"

int main(int argc, char **argv)
{
    char string [MAXLEN];  /* Holds input from the user */
    char board [BOARDX][BOARDY];  /*  Array contains the status
                                          of the life board */
    if (argc != 2){        /* Check that we have two arguments 
                           life filename */
        print_usage();
        return -1;
    } 
    
    /* Read the board from disk - exit the program if there is an error */
    if (read_board (argv[1],board) == ERROR) 
        return -1;
   
    while (1) {    /* Loop around until the user types quit */
        print_board(board);
        printf (
          "\nType quit to exit, save to save or anything else to continue\n");
        fgets (string, MAXLEN, stdin);
        if (strings_match (string, "quit\n"))
            return 0;
        update_board(board);
    }
}

int read_board (char *filename, char gameboard [BOARDX][BOARDY])
/* Tries to read in a "life" board from a file - if this is done
successfully, return NOERROR - if there is a problem opening the 
file return ERROR.
Note: We want our board to contain only spaces or #s and to fill
all the sections of the array - even if the lines read from the file
are too short pad the rest of the array with spaces*/
{
    FILE *fptr;               /* File pointer for the board file being read */
    char readline[MAXLEN];    /* Holds an input line from a file */
    int x, y;                 /* The x,y location we are currently reading */
    
    fptr= fopen (filename, "r");
    if (fptr == NULL) {
        printf ("Unable to open file \"%s\"\n",filename);
        return ERROR;
    }
    y= 0;
    while (fgets (readline, MAXLEN, fptr) != NULL) {
        x= 0;
        while (x < strlen(readline) && x < BOARDX) {
           if (readline[x] == '#') {
               gameboard[x][y]= '#';
           } else {
               gameboard[x][y]= ' ';
           }
           x++;
        }
        while (x < BOARDX) {
            gameboard [x][y]=' ';
            x++;
        }
        y++;
        if (y == BOARDY)
            break;
    }
    while (y < BOARDY) {
        for (x= 0; x < BOARDX; x++)
            gameboard [x][y]= ' ';
        y++;
    }
    fclose (fptr);
    return NOERROR;
}

void print_board (char gameboard[BOARDX][BOARDY])
/* Prints the status of the board to screen.  */
{
 

}

void update_board (char gameboard[BOARDX][BOARDY])
/* Updates the game board according to the life rules.  Note that we
must take a complete copy of the old board first  */
{
    
}

int calc_neighbours (int x, int y, char gameboard[BOARDX][BOARDY])
/* Returns the number of neighbours for a particular x, y location on
the board including checking if we are at the edge of the board */
{
    return 0;
}

int write_board (char gameboard[BOARDX][BOARDY])
/* Tries to write the state of the gameboard to a file - first
request the user to input a file name.  If the file fails to open
return ERROR otherwise return NOERROR */
{
    return NOERROR;
}

int strings_match (char *string1, char *string2)
/* Checks two strings to see if they are the same.  Returns 1 if the strings 
are the same or 0 if they are not the same.  Remember that we should only
check up to the '\0' character (and if the strings match they should both
have the '\0' in the same place) */
{
    return 1;
}

void copy_n_chars (char *string1, char *string2, int n)
/* Copies at most n-1 characters from string 1 to string 2 - that is, if
string 1 is "Hello" string 2 will become "Hello" if n is over 6.  In the
case of life, remember that we're dealing with character arrays which
aren't strings (not null terminated) so be sure to stop */
{

}

void print_usage (void)
/* Print instructions on how to use the program */
{
    fprintf (stderr, "life [filename]:\n");
    fprintf (stderr, "    Play Conways \"Game of Life\" on a given file.\n");
}

