'Setting age in Parellelised game of life
I am a university student and my teacher is making us to create a prey/predator simulation with OMP for. I had covid during his lectures and he didn’t upload them, so sorry I am being stupid here
Onto the question,
I have implemented Age as the absolute value for the cell as he asked but I simply cannot think of a way to make each cell have their own individual age after breeding. Here is my code:
#pragma omp parallel num_threads(4)
{
#pragma omp for schedule(dynamic, 1)
for (int i = 1; i <= Y; i++) //for loop Y
{
for (int j = 1; j <= X; j++) //for loop X
{
int Left = i - 1;
int Right = i + 1;
int Bottom = j - 1;
int Top = j + 1;
int Fish = 0;
int Shark = 0;
int FishAge = 1;
int SharkAge = -1;
int NewFishAge = 1;
//COUNTING SHARK AND FISH
//SHARK OR FISH - TOP LEFT
if (oldOcean[Left][Top] > 0)
{
Fish++;
}
else if (oldOcean[Left][Top] < 0)
{
Shark--;
}
//SHARK OR FISH - TOP
if (oldOcean[i][Top] > 0)
{
Fish++;
}
else if (oldOcean[i][Top] < 0)
{
Shark--;
}
//SHARK OR FISH - TOP RIGHT
if (oldOcean[Right][Top] > 0)
{
Fish++;
}
else if (oldOcean[Right][Top] < 0)
{
Shark--;
}
//SHARK OR FISH - LEFT
if (oldOcean[Left][j] > 0)
{
Fish++;
}
else if (oldOcean[Left][j] < 0)
{
Shark--;
}
//SHARK OR FISH - LEFT
if (oldOcean[Right][j] > 0)
{
Fish++;
}
else if (oldOcean[Right][j] < 0)
{
Shark--;
}
//SHARK OR FISH - LEFT
if (oldOcean[Left][Bottom] > 0)
{
Fish++;
}
else if (oldOcean[Left][Bottom] < 0)
{
Shark--;
}
//SHARK OR FISH - LEFT
if (oldOcean[i][Bottom] > 0)
{
Fish++;
}
else if (oldOcean[i][Bottom] < 0)
{
Shark--;
}
//SHARK OR FISH - LEFT
if (oldOcean[Right][Bottom] > 0)
{
Fish++;
}
else if (oldOcean[Right][Bottom] < 0)
{
Shark--;
}
//AGE
if (oldOcean[i][j] > 0)
{
oldOcean[i][j]++;
FishAge = oldOcean[i][j] - 2;
}
else if (oldOcean[i][j] < 0)
{
oldOcean[i][j]--;
SharkAge = oldOcean[i][j] * -1;
//std::cout << "AGE" << SharkAge;
}
//EMPTY CELLS & BREEDING
if (oldOcean[i][j] == 0)
{
//newOcean[i][j] = 0;
if (Fish >= 4 && Shark > -4 && FishAge >= 2)
{
newOcean[i][j]=1;
}
else if (Shark <= -4 && Fish < 4 && SharkAge >= 3)
{
newOcean[i][j] = -1;
}
else
{
newOcean[i][j] = 0;
}
newOcean[i][j] = oldOcean[i][j];
}
//FISH CELLS
if (oldOcean[i][j] > 0)
{
if (FishAge < 10)
{
FishAge++;
}
//std::cout <<FishAge;
if (FishAge >= 10 || Shark <= -5)
{
oldOcean[i][j] = 0; //kill fish
FishAge = 0;
}
if (Fish >= 8)
{
newOcean[i][j] = 0; //overpopulation
}
newOcean[i][j] = oldOcean[i][j];
}
//SHARK CELLS
if (oldOcean[i][j] < 0)
{
if (SharkAge <= 20 )
{
SharkAge++;
}
if ((Shark <= -6 && Fish == 0) || SharkAge >= 20)
{
oldOcean[i][j] = 0; // dies of Starvation
SharkAge = 0;
}
if (ChanceofDeath <= rand() / ((int)RAND_MAX + 32))
{
newOcean[i][j] = 0; // Random Death ???
}
newOcean[i][j] = oldOcean[i][j];
}
}
}
}
I am a very amateur coder, so any improvement feel free to tell me too please, I love to learn
my code for the age is under the "//AGE" section
Sorry again, and thanks for the help.
Solution 1:[1]
Even without considering age, you must check every cell in every generation. And it should be easy to maintain an age per cell during these checks:
- If a dead cell has three neighbors, it gets born (
cell.age = 0
). - If an alive cell has two or three neighbors, it survives (
cell.age++
). - If an alive cell has less than two or more than three neighbors, it dies. (No
cell.age
any more.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Heiko Theißen |