Esempio n. 1
0
void ToRun::runIteration(){
    for (int i = 0; i < 100; i++){
        for (int j = 0; j < 100; j++){
            if (!((i == 0 && j == 0) || (i == 0 && j == 99) || (i == 99 && j == 0) || (i == 99 && j == 99))){
                if (currentData[i][j]){
                    if (countNeighbors(i, j) == 2 || countNeighbors(i, j) == 3){
                        newData[i][j] = true;
                    }
                    else{
                        newData[i][j] = false;
                    }
                }
                else{
                    if (countNeighbors(i, j) == 3){
                        newData[i][j] = true;
                    }
                    else{
                        newData[i][j] = false;
                    }
                }
            }

        }
    }
    for (int i = 0; i < 100; i++){
        for (int j = 0; j < 100; j++){
            currentData[i][j] = newData[i][j];
        }
    }
}
Esempio n. 2
0
bool SimpleLife::evolve() {
    // qDebug() << "Evolving";

    int h = height;
    int w = width;

    std::vector< std::vector<bool> > rval;
    
    for (int i=0; i<height; ++i) {
        rval.push_back(std::vector<bool>());
        rval[i].resize(width, false);
    }

    for (int i=0; i<h; ++i) {
        for (int j=0; j<w; ++j) {
            int num = countNeighbors(i,j);

            if (array[i][j]) {
                if ((num < 2) || (num > 3)) {
                    rval[i][j] = false;
                } else {
                    rval[i][j] = true;
                }
            } else {
                if (num == 3) {
                    rval[i][j] = true;
                } else {
                    rval[i][j] = false;
                }
            }
        }
    }
    array = rval;
    return false;
}
Esempio n. 3
0
/**
 * Ages the grid by 'count' cycles. Loops though every cell, countsNeighbors, and updates
 */
void age()
{	
	int i,j,k;
	int neighbors = 0;
	// Loop through every cell and put new values in temp grid.
	for (i = 0; i < LENGTH; i++)
		for (j = 0; j < WIDTH; j++)
			for (k = 0; k < HEIGHT; k++) 
			{
				neighbors = countNeighbors(i, j, k);
				if (neighbors < minNeighbors || neighbors > maxNeighbors)
				{
					temp[i][j][k] = 0; // dies
				}
				else if (neighbors == alive && grid[i][j][k] == 0) 
				{
					// its its dead and has right number of neighbors, cell is born
					temp[i][j][k] = 1;
				}
				else 
				{
					//if it doesn't die it gets older
					temp[i][j][k] = grid[i][j][k] + 15;
				}
			}
	
	// then copy temp to grid... inefficient, should have used pointers
	for (i = 0; i < LENGTH; i++)
		for (j = 0; j < WIDTH; j++)
			for (k = 0; k < HEIGHT; k++) 
				grid[i][j][k] = temp[i][j][k];
}
Esempio n. 4
0
//Function that determines save, kill or born action based on neighbors for each generation
void processGeneration(int rows, int cols, int g[rows][cols])
{
	int i, j, neighbors;
	for (i = 0; i < rows; i++)  //Loop to determine death of existing person or birth of new person.
	{
		for(j = 0; j < cols; j++)
		{
			if(g[i][j] == -1) continue; //Checks if border cell, if so, stop and go on to the next iteration (moves to next cell in loop).
			neighbors = countNeighbors(rows, cols, g, i, j);  //Finds # of neighbors for the cell we're on

			if(g[i][j] == 1 && (neighbors < 2 || neighbors > 3)) //If person is alive and has <2 or >3 neighbors, 
				tempGrid[i][j] = 0;  //person dies.
			else if((g[i][j] == 0) && (neighbors == 3))  //If cell is empty, and has exactly 3 neighbors,
				tempGrid[i][j] = 1; // a person is born.
		}
	}

	population = 0; //Reset population temporarily
	for (i = 0; i < rows; i++) //Loop to fill real grid from temp grid and set new population
	{
		for(j = 0; j < cols; j++)
		{
			if(g[i][j] == -1) continue; //Checks if border cell, if so, stop and go on to the next iteration (moves to next cell in loop).
			if(tempGrid[i][j] == 1) population++; //If person is alive, add to population count.
			g[i][j] = tempGrid[i][j]; //Cell in real grid will get overwritten with cell in temp grid
		}
	}
	//Keep track of popMax and popMin to see our lowest and highest populations
	if(population > populationMax)	//If pop reaches highest count, store into new popMax
		populationMax = population; 
	if(population < populationMin || populationMin == 0) //If pop reaches lowest count, store into new popMin
		populationMin = population;
}
Esempio n. 5
0
// ***************************************
void processGeneration(int rows, int cols, int g[rows][cols])
{
	int i, j, neighbors;
	for(i = 0; i < rows; i++)
	{
		for(j = 0; j < cols; j++)
		{
			if(g[i][j] == -1) continue;
			neighbors = countNeighbors(rows, cols, g, i, j);
			
			if(g[i][j] == 1 && (neighbors < 2 || neighbors > 3))
				tempGrid[i][j] = 0;
			else if((g[i][j] == 0) && (neighbors == 3))
				tempGrid[i][j] = 1;
		}
	}
	
	population = 0;
	for(i = 0; i < rows; i++)
		for(j = 0; j < cols; j++)
		{
			if(g[i][j] == -1) continue;
			if(tempGrid[i][j] == 1) population++;
			g[i][j] = tempGrid[i][j];
		}
	if(population > populationMax)
		populationMax = population;
	if(population < populationMin || populationMin == 0)
		populationMin = population;
}
Esempio n. 6
0
/*
  core function: recursively traces through every stone that is
  orthogonally connected starting at stone x,y. as it traverses it counts
  and updates the attributes Size, Liberties, and Owner, and returns them
  wrapped in a Group structure

  param x,y: coordinates of a stone within a group
  param currGroup: a copy of a Group object that holds all attributes
  
  returns currGroup after traversing all stones in the group
*/
Group GoBoard::getGroup( int x, int y, Group currGroup )
{
  //coordinates of all neighbor stones
  int xnew[] = { x-1, x, x+1, x };
  int ynew[] = { y, y-1, y, y+1 };

  //set as counted to prevent double count
  grid[x][y]->setCounted();

  //iterate through every neighbor
  for( int i = 0; i < 4; i++ )
    {
      //verify we're on the board
      if( isOnBoard( xnew[i], ynew[i] ))
	{
	  //verify we have the same player
	  if( grid[xnew[i]][ynew[i]]->isPlayer(currGroup.player))
	    {
	      //check if already counted
	      if( !grid[xnew[i]][ynew[i]]->isCounted())
		{
		  currGroup = getGroup( xnew[i], ynew[i], currGroup );
		}
	    }
	  //if it is not the same player then we have to determine if ownership
	  //of the group has changed for empty territories
	  else if( currGroup.owner == EMPTY || 
		   grid[xnew[i]][ynew[i]]->isPlayer(currGroup.owner))
	    {
	      //if we have run into the same owner, or the owner hasn't been set
	      //then the piece we've run into is the owner
	      currGroup.owner = (Player)grid[xnew[i]][ynew[i]]->getPlayer();
	    }
	  else
	    {
	      //if we've run into something different than the owner
	      //then the territory is contested, once it's marked as DOMI
	      //it can't be scored for either player
	      currGroup.owner = (Player)DOMI;
	    }
	}
    }

  //increment the group size and find any liberties next to the current stone
  currGroup.size += 1;
  currGroup.liberties += countNeighbors( EMPTY, x, y );

  //debug for the attributes of each stone as it's traversed
  //std::cout<<"x: "<< x <<" y: "<< y << " lib: "<<countNeighbors( EMPTY, x, y )<<std::endl;
  
  
  return currGroup;
}
Esempio n. 7
0
/**
 * Update the state depending on time spent in the current state,
 * the number of neighbors and the presence of obstacles.
 */
void alphaAlgorithm() {
  nNeighbors = countNeighbors();

  if(currentState == FORWARD || currentState == FORWARD_AVOIDANCE) {
    // Lost the swarm --> jump to coherence state
    if(nNeighbors < ALPHA) {
      // Lost the swarm: make a 180° turn
      initiateTurn(180);

      setState(COHERENCE);
      // printf("Robot %s is turning back (%d neighbors).\n", robotName, nNeighbors);
    }
    // Obstacle avoidance timed out --> go back to forward state
    else if(currentState == FORWARD_AVOIDANCE) {
      avoidanceTime--;
      if(avoidanceTime <= 0) {
        setState(FORWARD);
      }
    }
    // Encountered an obstacle --> jump to obstacle avoidance
    else if(hasObstacle()) {
      setState(FORWARD_AVOIDANCE);
    }
    // Otherwise, persist in forward state
  }
  else if(currentState == COHERENCE || currentState == COHERENCE_AVOIDANCE) {
    // Successful coherence --> pick a random orientation and jump to state forward
    if(nNeighbors >= ALPHA) {
      // Pick a new random orientation
      initiateTurn(rand() % MAX_RANDOM_TURN);
      setState(FORWARD);
      // printf("Robot %s found back %d neighbors :)\n", robotName, nNeighbors);
    }
    else {
      if(currentState == FORWARD_AVOIDANCE) {
        avoidanceTime--;
        // Obstacle avoidance timed out --> go back to forward state
        if(avoidanceTime <= 0) {
          setState(FORWARD);
        }
      }

      // Regardless of avoidance time, the coherence time counter keeps running
      coherenceTime--;
      // Failed coherence --> go back to state FORWARD
      if(coherenceTime <= 0) {
        setState(FORWARD);
        // printf("Robot %s failed coherence :(\n", robotName);
      }
    }
  }
}
Esempio n. 8
0
void destroyBlock(Block *block)
{
  FaceBlock neighbors[NUM_PORTS+1];
  tellNeighborsDestroyed(block, neighbors);
  Q_REMOVE(&blockList, block, blockLink);
  block->destroyed = 1;
  allBlocks[block->id] = NULL;
  free(block);
  // now set neighbor count
  int i;
  for (i=0; neighbors[i].block != 0; i++) {
    msg2vm(neighbors[i].block, CMD_ADD_NCOUNT, countNeighbors(neighbors[i].block));
    msg2vm(neighbors[i].block, CMD_ADD_VACANT, neighbors[i].face);
  }
}
Esempio n. 9
0
//steps to the next time
void update(std::vector< std::vector<bool> > & current, std::vector< std::vector<bool> > & step){
    int neighbors = 0;
//check all relevant cells
    for (unsigned int v = 1; v <= current.size()-2; v++){
        for (unsigned int h = 1; h <= current.size()-2; h++){
//counts the neighbors
            neighbors = countNeighbors(current, v, h);
//determines if the cell lives or dies
            if ((current[v][h])&&((neighbors == 2)||(neighbors == 3)))
                step[v][h] = true;
            else if (!(current[v][h])&&(neighbors == 3))
                step[v][h] = true;
            else
                step[v][h] = false;
        }
    }

//updates the universe
    current = step;
}
Esempio n. 10
0
// b: block to check
// other: if set, then we only tell b about other's face
void
checkForNeighbors(Block* b, Block* other)
{
  int i;
  for(i = 0; i < NUM_PORTS; ++i) {
    // see if we have a neighbor at port i
    Block* d = seeIfNeighborAt(b, i);
    if ((d != 0)&&((other==NULL)||(other==d))) {
      if (d->neighborhood[i] == NULL) {
        // we have a neighbor. tell them
        msg2vm(d, CMD_DEL_VACANT, b->localTime, i);
        msg2vm(d, CMD_ADD_NBR, b->localTime, b->id, i);
        if (other == NULL) checkForNeighbors(d, b);
        msg2vm(d, CMD_ADD_NCOUNT, b->localTime, countNeighbors(d));
        d->neighborhood[i] = b;
      }
      assert(d->neighborhood[i] == b);
    }
  }
}
Esempio n. 11
0
bool ThreeDimLife::evolve() {
    // qDebug() << "Evolving";

    int h = height;
    int w = width;
    int d = depth;

    vector_3d rval;

    rval.resize(height);
    for (int i=0; i<height; ++i) {
        rval[i].resize(width);
        for (int j=0;j<width; ++j) {
            rval[i][j].resize(depth, false);
        }
    }

    for (int i=0; i<h; ++i) {
        for (int j=0; j<w; ++j) {
            for (int k=0; k<d; ++k) {
                int num = countNeighbors(i,j, k);

                if (array[i][j][k]) {
                    if ((num < 2) || (num > 3)) {
                        rval[i][j][k] = false;
                    } else {
                        rval[i][j][k] = true;
                    }
                } else {
                    if (num == 3) {
                        rval[i][j][k] = true;
                    } else {
                        rval[i][j][k] = false;
                    }
                }
            }
        }
    }
    array = rval;
    return false;
}
Esempio n. 12
0
//
// mEvolve
//
// mEvolve looks at a section of the grid G (determined by the thread), and
// proceeds to count the neighbors for each entry. Based on the neighbor
// count, the function passes a value indicating cell death (0) to T, or
// cell birth (1) to T. 
//
void *mEvolve(grid *G, grid *T, int height, int part) {
  
  int i, j, neighbors;

  // Examine a specific part of G
  //
  for (i=part; i<(part+height); i++) {
    for (j=0; j<G->cols; j++) {

      neighbors = countNeighbors(G, i, j);
      
      // Determine which cells are born and which die.
      //
      if (G->val[i][j] == 1 && (neighbors < 2 || neighbors > 3)) {
        T->val[i][j] = 0;
      } else if (G->val[i][j] == 0 && neighbors == 3) {
        T->val[i][j] = 1;
      }
    }
  }   
}
Esempio n. 13
0
void update() {
	int numNeighbors;
	for (int i = 0; i < NUM_COLS; i++) {
		for (int j = 0; j < NUM_ROWS; j++) {
			numNeighbors = countNeighbors(i, j);

			//Starvation
			if (numNeighbors < 2) {
				temp[i][j] = false;
			}
			//Reproduction
			if (numNeighbors == 3) {
				temp[i][j] = true;
			}
			//Overcrowding
			else if (numNeighbors > 3) {
				temp[i][j] = false;
			}
		}
	}
	swapGrids();
}