/********************************** -setting any alive cells to new represenation: 0x10 -call initialize_neighbour_counts **********************************/ char* load_board_values (FILE* input, const int nrows, const int ncols) { char* board = NULL; int ngotten = 0; int i = 0; /* Make a new board */ board = make_board (nrows, ncols); /* Fill in the board with values from the input file */ for (i = 0; i < nrows * ncols; i++) { ngotten = fscanf (input, "%c\n", &board[i]); if (ngotten < 1) { fprintf (stderr, "*** Ran out of input at item %d ***\n", i); fclose (input); exit (EXIT_FAILURE); } else /* ASCII '0' is not zero; do the conversion */ board[i] = board[i] - '0'; if (board[i] == 0x01) { board[i] = board[i] << 4; } } initialize_neighbour_counts(board, nrows, ncols); return board; }
static char *new_game_desc(game_params *params, random_state *rs, char **aux, int interactive) { const int w = params->w; const int h = params->h; const int sz = w * h; int *board = snewn(sz, int); int *randomize = snewn(sz, int); char *game_description = snewn(sz + 1, char); int i; for (i = 0; i < sz; ++i) { board[i] = EMPTY; randomize[i] = i; } make_board(board, w, h, rs); #ifdef ANDROID if (android_cancelled()) { sfree(randomize); sfree(board); return NULL; } #endif g_board = board; qsort(randomize, sz, sizeof (int), compare); minimize_clue_set(board, w, h, randomize); for (i = 0; i < sz; ++i) { assert(board[i] >= 0); assert(board[i] < 10); game_description[i] = board[i] + '0'; } game_description[sz] = '\0'; /* solver(board, w, h, aux); print_board(board, w, h); */ sfree(randomize); sfree(board); return game_description; }
int main (int argc, char **argv) { int r, c; begin_display(&r, &c); /* reserve one line for message row */ make_board(r-1, c); msg_row = r-1; prompt("Welcome to Life, Version %s, Copyright 1995, Jim Wise", VERSION_STR); clear_board(); while (1) { if (!edit()) break; run(); } end_display(); exit(0); }
int main (int argc, char* argv[]) { /* * Set verifyp to 1 if you want to turn on verification. */ const int verifyp = DO_VERIFY; const int argc_min = 3; const int argc_max = 4; int gens_max = 0; char* inboard = NULL; char* outboard = NULL; char* checkboard = NULL; char* final_board = NULL; int nrows = 0; int ncols = 0; FILE* input = NULL; FILE* output = NULL; int err = 0; /* Parse command-line arguments */ if (argc < argc_min || argc > argc_max) { fprintf (stderr, "*** Wrong number of command-line arguments; " "got %d, need at least %d and no more than %d ***\n", argc - 1, argc_min - 1, argc_max - 1); print_usage (argv[0]); exit (EXIT_FAILURE); } err = to_int (&gens_max, argv[1]); if (err != 0) { fprintf (stderr, "*** <num_generations> argument \'%s\' " "must be a nonnegative integer! ***\n", argv[1]); print_usage (argv[0]); exit (EXIT_FAILURE); } /* Open input and output files */ input = fopen (argv[2], "r"); if (input == NULL) { fprintf (stderr, "*** Failed to open input file \'%s\' for reading! ***\n", argv[2]); print_usage (argv[0]); exit (EXIT_FAILURE); } if (argc < argc_max || 0 == strcmp (argv[3], "-")) output = stdout; else { output = fopen (argv[3], "w"); if (output == NULL) { fprintf (stderr, "*** Failed to open output file \'%s\' for writing! ***\n", argv[3]); print_usage (argv[0]); exit (EXIT_FAILURE); } } /* Load the initial board state from the input file */ inboard = load_board (input, &nrows, &ncols); fclose (input); /* Create a second board for ping-ponging */ outboard = make_board (nrows, ncols); /* If we want to verify the result, then make a third board and copy the initial state into it */ if (verifyp) { checkboard = make_board (nrows, ncols); copy_board (checkboard, inboard, nrows, ncols); } /* * Evolve board gens_max ticks, and time the evolution. You will * parallelize the game_of_life() function for this assignment. */ copy_board (outboard, inboard, nrows, ncols); final_board = game_of_life (outboard, inboard, nrows, ncols, gens_max); /* Print (or save, depending on command-line argument <outfilename>) the final board */ save_board (output, final_board, nrows, ncols); if (output != stdout && output != stderr) fclose (output); if (verifyp) { /* Make sure that outboard has the final state, so we can verify it. Since we ping-pong between inboard and outboard, it could be either that inboard == final_board or that outboard == final_board */ copy_board (outboard, final_board, nrows, ncols); /* Ping-pong between checkboard (contains the initial state) and inboard */ final_board = sequential_game_of_life (inboard, checkboard, nrows, ncols, gens_max); if (boards_equalp (final_board, outboard, nrows, ncols)) printf ("Verification successful\n"); else { fprintf (stderr, "*** Verification failed! ***\n"); exit (EXIT_FAILURE); } } /* Clean up */ if (inboard != NULL) free (inboard); if (outboard != NULL) free (outboard); if (checkboard != NULL) free (checkboard); return 0; }