/* * Method used for moving any creature, it's checking * if the creature is old enough for spawn and is it * old enough to die * */ private void MoveCreature(Creature creature, int x, int y) { if (creature != null) { if (!IsAlive(creature, x, y)) { return; } if (CheckForSpawn(creature)) { return; } if (creature is Shark) { MoveShark((Shark)creature, x, y); } else { MoveSepia((Sepia)creature, x, y); } } }
/* * Checking of the creature - creature can * change it's location to position (x, y) * */ private bool IsLegal(Creature creature, int x, int y) { if (x < 0 || y < 0) { return false; } if (x >= _simulationField.GetLength(0) || y >= _simulationField.GetLength(1)) { return false; } if (creature is Sepia && _simulationField[x, y] is Sepia) { return false; } if (creature is Shark && _simulationField[x, y] is Shark) { return false; } return true; }
/* * Finding the next position for any creature with current coordinates (x, y) * */ private Library.Pair FindPlaceForSepia(Creature creature, int x, int y) { bool positionFound = false; Random rand = new Random(); Library.Pair newPosition = new Library.Pair(0, 0); MixPositions(); for (int i = 0; i < _positionsCatalog.Count; i++) { if (IsLegal(creature, _positionsCatalog[i].X + x, _positionsCatalog[i].Y + y)) { newPosition.X = _positionsCatalog[i].X + x; newPosition.Y = _positionsCatalog[i].Y + y; positionFound = true; } } if (!positionFound) { newPosition.X = x; newPosition.Y = y; } return newPosition; }
/* * This method is checking if any creature is old enough to be removed * from the simulation ground. If the creature is shark * this method is also checking if it's hunger. * */ private bool IsAlive(Creature creature, int x, int y) { if (creature.Age <= 0) { KillCreature(x, y); return false; } else { creature.Age--; } if (creature is Shark) { if (((Shark)creature).Hunger >= ((Shark)creature).MaxHunger) { KillCreature(x, y); return false; } else { ((Shark)creature).Hunger++; } } return true; }
/* * Increacing the population if any creature is older than 1/3 of it's life * */ private bool CheckForSpawn(Creature creature) { if (creature is Shark) { return CheckForSpawnShark((Shark)creature); } else { return CheckForSpawnSepia((Sepia)creature); } }