Fastnat och fr inte vrden att uppdatera.
Kod:
#include <stdio.h> #include <stdlib.h> #include <time.h> /* Constants, representation of states */ #define ALIVE 'X' #define DEAD '.' /* Declaration of data structure */ typedef struct{ char current; char next; } cell; /* Declaration of functions */ void initField(const int rows, const int cols, cell field[rows][cols]); void clearField(const int rows, const int cols, cell field[rows][cols]); void loadGlider(const int rows, const int cols, cell field[rows][cols]); void loadSemaphore(const int rows, const int cols, cell field[rows][cols]); void loadRandom(const int rows, const int cols, cell field[rows][cols]); void loadCustom(const int rows, const int cols, cell field[rows][cols]); void printField(const int rows, const int cols, cell field[rows][cols]); void aliveOrDead(const int rows, const int cols, int array[rows][cols], cell field[rows][cols]); void countNeighborhood(const int rows, const int cols, int array[rows][cols], cell field[rows][cols]); int countNeighbors(const int rows, const int cols, cell field[rows][cols], int row, int col); int getState(int rows, int cols, cell field[rows][cols], int row, int col); void updateWorld(const int rows, const int cols, cell field [rows][cols]); char getStartStateChoice(void); /* Function: main * Description: Start and run simulations, interact with the user. * Lets the user choose initial structure and whether to step * or exit. Writes information to the user, and the game field * in each step. */ int main(void) { const int rows = 20; const int cols = 20; cell field[rows][cols]; initField(rows, cols, field); printField(rows, cols, field); return 0; } /* Function: initField * Description: Loads a structure that the user selects * Input: rows - the number of rows in the field * cols - the number of columns in the field * field - the field array * Output: The field array is updated. */ void initField(const int rows, const int cols, cell field[rows][cols]) { char choice; clearField(rows,cols,field); choice=getStartStateChoice(); switch (choice) { case 'g': case 'G': loadGlider(rows, cols, field); break; case 's': case 'S': loadSemaphore(rows, cols, field); break; case 'r': case 'R': loadRandom(rows, cols, field); break; case 'c': case 'C': default: loadCustom(rows, cols, field); break; } } /* Function: getStartStateChoice * Description: Lets the user choose starting state * Input: * Output: The users choice. Should be one of the letters G,S,R or C. */ char getStartStateChoice(void) { printf("Select field spec to load ([G]lider, [S]emaphore, [R]andom "); printf("or [C]ustom): "); int ch = getchar(); /* Ignore following newline */ if (ch != '\n') { getchar(); } return ch; } /* Function: clearField * Description: Initialize all the cells in the field to dead * Input: rows - the number of rows in the field * cols - the number of columns in the field * field - the field array * Output: The field array is updated. */ void clearField(const int rows, const int cols, cell field[rows][cols]) { for (int r = 0 ; r < rows ; r++) { for (int c = 0 ; c < cols ; c++) { field[r][c].current = DEAD; } } } /* Function: loadGlider * Description: Inserts a glider into the field. * Input: rows - the number of rows in the field * cols - the number of columns in the field * field - the field array * Output: The field array is updated. */ void loadGlider(const int rows, const int cols, cell field[rows][cols]) { field[0][1].current = ALIVE; field[1][2].current = ALIVE; field[2][0].current = ALIVE; field[2][1].current = ALIVE; field[2][2].current = ALIVE; } /* Function: loadSemaphore * Description: Inserts a semaphore into the field. * Input: rows - the number of rows in the field * cols - the number of columns in the field * field - the field array * Output: The field array is updated. */ void loadSemaphore(const int rows, const int cols, cell field[rows][cols]) { field[8][1].current = ALIVE; field[8][2].current = ALIVE; field[8][3].current = ALIVE; } /* Function: loadRandom * Description: Inserts a random structure into the field. * Input: rows - the number of rows in the field * cols - the number of columns in the field * field - the field array * Output: The field array is updated. There is a 50 % chance that a cell * is alive. */ void loadRandom(const int rows, const int cols, cell field[rows][cols]) { srand(time(NULL)); for (int r = 0;r < rows ; r++){ for (int c = 0;c < cols ; c++){ int temp = rand() % 2; if(temp == 0){ field[r][c].current = DEAD; } else{ field[r][c].current = ALIVE; } } } } /* Function: loadCustom * Description: Lets the user specify a structure that then is inserted into * the field. * Input: rows - the number of rows in the field * cols - the number of columns in the field * field - the field array * Output: The field array is updated. */ void loadCustom(const int rows, const int cols, cell field[rows][cols]) { printf("Give custom format string: "); do { int r, c; scanf("%d,%d", &r, &c); field[r][c].current = ALIVE; } while (getchar() != '\n'); } void printField(const int rows, const int cols, cell field[rows][cols]){ do{ for(int r = 0; r < rows; r++){ if(r != 0){ printf("\n"); } for (int c = 0; c < cols; c++) { if (field[r][c].current==ALIVE) { printf(" X "); } else{ printf(" . "); } } } printf("\n"); printf("Select one of the following options:\n (enter) Step\n"); printf(" (any) Exit \n"); //Kolla } while (getchar() == '\n'); } /* Function: countNeighborhood * Description: Counts the number of live cells one cell is surrounded by. * Input: The field array and its size. * Output: The neighborCount array. */ void countNeighborhood(const int rows, const int cols, int array[rows][cols], cell field[rows][cols]){ for (int r = 0; r <= rows; r++){ for (int c = 0; c <= cols; c++) { array[r][c] = countNeighbors(rows, cols, field, r, c); } } } int countNeighbors(const int rows, const int cols, cell field[rows][cols], int row, int col){ int count = 0; for (int r = row - 1; r <= row + 1; r++) { for (int c = col - 1; c <= col + 1; c++) { if ((row != r || col != c) && getState (rows, cols, field, r, c) == ALIVE) { count++; } } } return count; } int getState(const int rows, const int cols, cell field[rows][cols], int row, int col) { if (row >= 0 && row <rows && col >= 0 && col < cols){ return field[row][col].current; } else{ return DEAD; } } void aliveOrDead(const int rows, const int cols, int array[rows][cols], cell field[rows][cols]){ countNeighborhood(rows, cols, array, field); for (int r = 0; r < rows; r++){ for (int c = 0; c < cols; c++){ if (field[r][c].current == ALIVE && array[r][c] == 2){ field[r][c].next = ALIVE; } else if (array[r][c] == 3) { field[r][c].next = ALIVE; } else{ field[r][c].next = DEAD; } } } for (int i = 0; i < rows; i++){ for (int j = 0; j < cols; j++){ printf("%d ", array[i][j]); } } updateWorld (rows, cols, field); } void updateWorld(const int rows, const int cols, cell field [rows][cols]){ for (int r = 0; r < rows; r++){ for (int c = 0; c < cols; c++){ field[r][c].current = field[r][c].next; } } }