Beispiel #1
0
int main()
{
  int select;
  int err=1;
  /* array of grid members */
  GridMember** memberList;
  memberList = createGridList();
  /* setting random generator */
  srand(time(NULL));
  /* create game grid members */
  while(err) {
    /* clear screen and always show title */
    displayTitle();
    /* config game options */
    configGame(&select);
    err = createGridMembers(memberList, select);
  }
  /* enter game loop */
  while(1) {
    /* clear screen and always show title */
    displayTitle();
    /* show game grid */
    showGrid(memberList);
    /* count neighbours */
    countNeighbours(memberList);
    /* update status */
    updateGrid(memberList);
    /* time tick */
    timeTick();
  }
  displayEnd();
  freeGridList(memberList);
  return 0;
}
void GameOfLife::iterateOnce() {
	++cur_iteration;
	int alive = 0;

	#pragma omp parallel for reduction(+:alive) if(OPENMP_ENABLED)
	for (int i = 0; i < row_size; ++i) {
		for (int j = 0; j < col_size; ++j) {
			int count = countNeighbours(i, j);
			if (count <= 1 || count >= 4) {
				copy_board[i][j] = DEAD;
			}
			else {
				copy_board[i][j] = ALIVE;
				alive += 1;
			}
		}
	}

	// copy back the board
	#pragma omp parallel for if(OPENMP_ENABLED)
	for (int i = 0; i < row_size; ++i) {
		for (int j = 0; j < col_size; ++j) {
			game_board[i][j] = copy_board[i][j];
		}
	}

	if (DEBUG) printf("current alive:%2d\n", alive);
	cur_alive = alive;
}
void Object_LifeExplosion::step()
{
	BigPixelArray* bpa = getComponent<BigPixelArray>();
	std::vector<std::pair<int, int>> toAdd;
	std::vector<std::pair<int, int>> toRemove;
	for (int cx = 0; cx < bpa->getWidth(); cx++)
		for (int cy = 0; cy < bpa->getHeight(); cy++)
		{
			int neighbours = countNeighbours(cx, cy);
			if (bpa->getAlive(cx, cy))
			{
				if (neighbours != 2 && neighbours != 3)
					toRemove.push_back(std::make_pair(cx, cy));
			}
			else
			{
				if (neighbours == 3)
					toAdd.push_back(std::make_pair(cx, cy));
			}
		}
	for (auto add : toAdd)
	{
		bpa->addPixel(add.first, add.second, sf::Color(250, 10, 0), 20);
	}
	for (auto rm : toRemove)
	{
		bpa->removePixel(rm.first, rm.second);
	}
}
Beispiel #4
0
void nextStep(world_t *world) {
  int neighbours=0;
  int i, j, tmp;

  for(i = 0; i<height; i++) {
    for(j = 0; j<width; j++) {
      world->cells[i][j].alive_next_round = world->cells[i][j].alive;
    }
  }

  for(i = 0; i<height; i++) {
    for(j = 0; j<width; j++) {
      // rule #1
      if(countNeighbours(i, j, world) < 2)
        world->cells[i][j].alive_next_round = false;
      // rule #2
      if((tmp == 2) || (tmp == 3)) {
        if(world->cells[i][j].alive)
          world->cells[i][j].alive_next_round = true;
      };
      // rule #3
      if(countNeighbours(i, j, world) > 3) {
        world->cells[i][j].alive_next_round = false;
      };
      // rule #4
      if(countNeighbours(i, j, world) == 3) {
        if(!world->cells[i][j].alive)
          world->cells[i][j].alive_next_round = true;
      }
    }
  }

  for(i=0; i<height; i++) {
    for (j=0; j<width; j++) {
      world->cells[i][j].alive = (world->cells[i][j].alive_next_round) ? true : false;
    }
  }
}
Beispiel #5
0
void LifeModel::simulate()
{
	for (int i = 0; i != n; ++i)
	{
		for (int j = 0; j != m; ++j)
		{
			int neighboursCount = countNeighbours(i, j);
			int cellState = getCell(i, j);

			if ((cellState == 0 && neighboursCount == 3)
					|| (cellState > 0 && (neighboursCount == 2 || neighboursCount == 3)))
			{
				backField[i][j] = 1;
			}
			else
			{
				backField[i][j] = 0;
			}
		}
	}
	swapFields();
}
Beispiel #6
0
void iterateGrid(int grid[GRID_SIZE][GRID_SIZE]) {
	static int newGrid[GRID_SIZE][GRID_SIZE];

	int i, j;
	for (i = 0; i < GRID_SIZE; ++i) {
		for (j = 0; j < GRID_SIZE; ++j) {
			int liveNeighbours = countNeighbours(grid, i, j);
			if (liveNeighbours < 2 || liveNeighbours > 3) {
				newGrid[i][j] = 0;
			} else if (liveNeighbours == 2 && grid[i][j] == 1) {
				newGrid[i][j] = 1;
			} else if (liveNeighbours == 3) {
				newGrid[i][j] = 1;
			}
		}
	}
	
	for (i = 0; i < GRID_SIZE; ++i) {
		for (j = 0; j < GRID_SIZE; ++j) {
			grid[i][j] = newGrid[i][j];
		}
	}
}