/***************************************************************
 * Compute the next generation of cells
 ***************************************************************/
void Cellular_Automata :: compute_generation(){
    
    // Calculate the number of neighbours alive for each cell
    update_neighbours();
    
    // For each cell in the grid
    for(int y = diameter; y < height+(diameter*2); y += diameter)
        for (int x = diameter; x < width+(diameter*2); x += diameter) {
            int hash = get_coordinate_hash(x, y);
            
            // If the grid is not being seeded
            if(!seed){
                
                // If the current frame divides exactly
                // into the current sequences update speed
                if(ofGetFrameNum() % update_speed == 0){
                    
                    // Update the cells life state bases on the current sequences rule
                    int n_neighbours = (cell_grid->find(hash)->second).get_n_neighbours();
                    set_cell_state(x,y, n_neighbours, rule);
                }
            }
            // If in the cellular seuquence window draw the cells 
            if(visible)
                cell_grid->find(hash)->second.draw();
        }
    
}
예제 #2
0
파일: board.c 프로젝트: photz/conway
/**
 * Reads a board from standard input.
 * @returns returns 1, if the board could be read, 0 in the event of\
 * a failure.
 */
int read_board(board_t* board)
{
  assert(NULL != board);

  // clean the board
  memset(board, 0, sizeof(board_t));

  for (pos_t pos = {0, 0}; pos.y < ROWS; pos.y++)
    {
      // buffer for the chars of the currently processed row
      char rowbuf[COLUMNS+1] = {0};

      // FIXME replace the hard-coded 29
      const int chars_read = scanf("%29s[^\n]%*c", rowbuf);
      
      //assert(chars_read == COLUMNS);

      for (pos.x = 0; pos.x < COLUMNS; pos.x++)
	{
	  // FIXME this is so ugly
	  assert(rowbuf[pos.x] == CHAR_BLACK
		 || rowbuf[pos.x] == CHAR_WHITE
		 || rowbuf[pos.x] == CHAR_EMPTY);

	  // state of the cell referred to by _pos_
	  cell_t cell_state;

	  switch (rowbuf[pos.x])
	    {
	    case CHAR_BLACK: cell_state = BLACK; break;
	    case CHAR_WHITE: cell_state = WHITE; break;
	    case CHAR_EMPTY: cell_state = EMPTY; break;
	    }

	  set_cell_state(board, pos, cell_state);
	}
    }

  return 1;
}
예제 #3
0
파일: board.c 프로젝트: photz/conway
/**
 * Advances the state of the board (cellular automaton) by a single step.
 */
void step(board_t* board, const board_t* old_board)
{
  assert(NULL != board);

  assert(NULL != old_board);

  assert(is_valid_board(board));

  // create a copy of the old state
  // the board pointed to by _board_ will 
  //board_t old_board = *board;


  // iterate over the living cells
  // TODO decide whether a linked list is worth the added complexity
  for (pos_t pos = {0, 0}; pos.y < ROWS; pos.y++)
    {
      for (pos.x = 0; pos.x < COLUMNS; pos.x++)
	{
	  const cell_t old_state = get_cell(old_board, pos);

	  // determine the number of living cells bordering this one
	  const int black_neighbors = n_neighbors(old_board, pos, BLACK);
	  const int white_neighbors = n_neighbors(old_board, pos, WHITE);
	  
	  const cell_t new_state = successor_cell_state(old_state,
							white_neighbors,
							black_neighbors);

	  
	  //if (new_state != old_state)
	    set_cell_state(board, pos, new_state);

	  /* if (new_state != EMPTY) */
	  /*   printf("(%d,%d) b: %d, w: %d\n", */
	  /* 	   pos.x, pos.y, black_neighbors, white_neighbors); */
	}
    }
}