Exemplo n.º 1
0
void toggle_alive(board b, int h, int w) {
  if(is_alive(b, h, w)) {
    set_alive(b,h, w, false);
  } else {
    set_alive(b,h, w, true);
  }
}
Exemplo n.º 2
0
board init_board_from_chars(char* pic, int h, int w) {
  board b = init_board(h, w);
  int i,j;
  for(i=0; i<h; i++) {
    for(j=0; j<w; j++) {
      if(pic[i*w+j] == '.') {
        set_alive(b, i, j, false);
      } else if(pic[i*w+j] == 'X') {
        set_alive(b, i, j, true);
      } else {
        printf("FAIL %c\n", pic[i*w+j]);
      }
    }
  }
  return b;
}
Exemplo n.º 3
0
void conway_run(int8_t *start_pic)
{
	old_fb = start_pic;
	new_fb = led_matrix_set_fb(old_fb);
	uint8_t	changed = 1; // first time anyway

	led_matrix_set_pixel(8, 4, 1);
	led_matrix_set_pixel(8, 5, 1);
	led_matrix_set_pixel(8, 6, 1);

		
	led_matrix_set_pixel(7, 5, 1);
	led_matrix_set_pixel(7, 11, 1);

	led_matrix_set_pixel(8, 10, 1);
	led_matrix_set_pixel(8, 11, 1);
	led_matrix_set_pixel(8, 12, 1);

	app_reset_tick();
	app_start_tick();

	while(1) {
	if(old_fb == new_fb) {
		uart_puts("the same!!!\n");
		//malloc new fb
	} 

		for(uint8_t x = 0; x < SIZE_X; x ++) {
			for(uint8_t y = 0; y < SIZE_Y; y ++) {
				uint8_t neighbors = living_neighbors(x,y);
				if(neighbors == 3) {
					if(is_dead(x,y)) {
						changed = 1;
					}
					set_alive(x,y);
				}else if(neighbors == 2) {
					if(is_alive(x,y)) {
						set_alive(x,y);
					}else{
						set_dead(x,y);
					}
				}else {
					
					if(is_alive(x,y)) {
						changed = 1;
					}
					set_dead(x,y);
				}
			}
		}
		if(changed == 0) {
			reset_fb();
			app_stop_tick();
			app_reset_tick();
			return;	
		}
		changed = 0;
		/*	while(app_get_tick()) {
			uart_puts("waiting\n");
			_delay_ms(10);
			}*/
		for(int k = 0; k < 50; k++) {
			_delay_ms(10);
		}
		app_dec_tick();
		old_fb = new_fb;
		new_fb = led_matrix_set_fb(new_fb);
	}
}
Exemplo n.º 4
0
	void set_features( bool alive, bool positive, bool influential )
	{
		set_alive( alive );
		set_positive( positive );
		set_influential( influential );
	}
Exemplo n.º 5
0
int main()
{
	
	// initialise
	const unsigned int kGridSize = 25;

	const unsigned int kPopulationMax = 30;
	const unsigned int kFoodMax = 50;

	const unsigned int kReproduceEnergy = 120;

	const unsigned int kCellStartEnergy = 100;
	const unsigned int kMaxFoodEnergy = 100;

	Dish petri(kGridSize);

	unsigned int total_population = 15;
	unsigned int total_food = 30;

	std::vector<Cell> population;

	for (unsigned int i = 0; i < total_population; i++) {
		population.push_back(Cell(kGridSize, kCellStartEnergy));
	}

	std::list<Food> supply;

	for (unsigned int i = 0; i < total_food; i++) {
		supply.push_back(Food(kGridSize, kMaxFoodEnergy));
	}

	bool exit = false;
	unsigned int w = 1;

	while (w < 50) {

		/*
		// Process step
		*/
		for (auto cell = population.begin(); cell != population.end(); ++cell) {
			if (cell->alive() == true) {
				// move
				cell->move(kGridSize);

				for (auto food = supply.begin(); food != supply.end(); ++food) {
					if (cell->location() == food->location()) {
						total_food = supply.size();
						cell->consume_food(*food);
						food = supply.erase(food);
					}
				}

				for (auto target = population.begin(); target != population.end(); ++target) {
					switch (can_consume_cell(*cell, *target))
					{
					case false:
						continue;
					case true:
						target = population.erase(target);
						break;
					default:
						break;
					} 
				}
				

				// reproduce
				if ((total_population < kPopulationMax) && (cell->reproduce())) {
					population.push_back(Cell(kGridSize, cell->energy() / 2));
					cell->set_energy(cell->energy() / 2);

					total_population = population.size();
				}



				// death
				if (cell->energy() <= 0) {
					cell->set_alive(false);
				}
			}
		}

		if (total_food < kFoodMax) {
			supply.push_back(Food(kGridSize, kMaxFoodEnergy));

			total_food = supply.size();
		}

		/*
		// Input step
		*/

		// poll user input

		/*
		// Display step
		*/

		// display grid

		// TODO: Optimise this horrible loop
		for (unsigned int i = 0; i < kGridSize; i++) {
			for (unsigned int j = 0; j < kGridSize; j++) { 
				bool occupied = false;
				position display_location;
				display_location.x = i;
				display_location.y = j;
				for (Cell cell : population) {
					if (cell.location() == display_location) {
						if (cell.alive() == true) {
							std::cout << cell.energy();
						}
						else {
							std::cout << "-";
						}
						occupied = true;
					}
				}
				for (Food food : supply) {
					if (food.location() == display_location) {
						std::cout << ".";
						occupied = true;
					}
				}
				if (occupied == false) {
					std::cout << " ";
				}
				else {
					occupied = false;
				}

			}
			std::cout << "\n";
		}
		
		for (unsigned int i = 0; i < kGridSize; i++) {
			std::cout << "=";
		}
		std::cout << "\n" << total_population;

		std::cout.flush();
		Sleep(1500);

		w++;
	}

	population.clear();
	supply.clear();
	
	return 0;
}