<?php
class Person
{
public readonly string $lastName;
public readonly string $firstName;
private readonly int $strength;
private readonly int $agility;
private readonly int $intelligence;
private readonly int $charisma;
private readonly int $luck;
/**
* @param non-empty-string|null $lastName
*/
public function __construct(?string $lastName = null)
{
$this->lastName = $lastName ?? $this->randomName();
$this->firstName = $firstName ?? $this->randomName($this->lastName[0]);
$this->strength = $this->randomFeatureValue();
$this->agility = $this->randomFeatureValue();
$this->intelligence = $this->randomFeatureValue();
$this->charisma = $this->randomFeatureValue();
$this->luck = $this->randomFeatureValue();
}
public function __clone(): void
{
$this->firstName = $this->randomName($this->lastName[0]);
$this->strength = $this->evolveFeatureValue($this->strength);
$this->agility = $this->evolveFeatureValue($this->agility);
$this->intelligence = $this->evolveFeatureValue($this->intelligence);
$this->charisma = $this->evolveFeatureValue($this->charisma);
$this->luck = $this->evolveFeatureValue($this->luck);
}
public function getName(): string
{
return $this->lastName . ' ' . $this->firstName;
}
public function getValue(): int
{
return $this->strength + $this->agility + $this->intelligence + $this->charisma + $this->luck;
}
public function getDetails(): string
{
return $this->getName() . ' ' . $this->getValue() . '(' . $this->strength . '+' . $this->agility . '+' . $this->intelligence . '+' . $this->charisma . '+' . $this->luck . ')';
}
/**
* @return int
* @throws Exception
*/
private function randomFeatureValue(): int
{
return random_int(0, 9);
}
private function evolveFeatureValue(int $value): int
{
return min(max($value + random_int(-1, 1), 0), 9);
}
public function randomName(?string $notStartingWith = null): string
{
$possibleNames = ['Alice', 'Bob', 'Charlie', 'Dave', 'Eve', 'Frank', 'Grace', 'Heidi', 'Ivan', 'Judy', 'Kevin', 'Linda', 'Mallory', 'Niaj', 'Olivia', 'Peggy', 'Quentin', 'Rupert', 'Sybil', 'Trent', 'Ursula', 'Victor', 'Walter', 'Xavier', 'Yvonne', 'Zoe'];
do {
$randomName = $possibleNames[array_rand($possibleNames)];
} while ($notStartingWith !== null && $randomName[0] === $notStartingWith);
return $randomName;
}
}
class Life
{
/** @var Person[] */
private array $population = [];
public function run(int $iterations)
{
while ($iterations-- > 0) {
$this->runIteration();
}
}
public function runIteration()
{
$this->new();
$this->reproduce();
}
public function geStatisticsOnMostValuable()
{
$topMostValuablePersons = $this->getTopMostValuablePersons(10);
$statistics = array_map(
fn(Person $person) => $person->getDetails(),
$topMostValuablePersons,
);
return implode("\n", $statistics);
}
private function new(): void
{
foreach (range(1, 3) as $_) {
$this->addPerson(new Person());
}
}
private function reproduce(): void
{
$topMostValuablePerson = $this->getTopMostValuablePersons(5);
// usual evolution
foreach (range(1, 3) as $_) {
$randomValuablePerson = $topMostValuablePerson[array_rand($topMostValuablePerson)];
$this->addPerson(clone $randomValuablePerson);
}
// tracked evolution
$randomValuablePerson = $topMostValuablePerson[array_rand($topMostValuablePerson)];
$this->addPerson(clone $randomValuablePerson with (lastName: '0000'));
}
private function addPerson(Person $person)
{
/** add a person, eventually killing one that already has the same name */
$this->population[$person->getName()] = $person;
}
/**
* @param int $int
* @return Person[]
*/
private function getTopMostValuablePersons(int $int): array
{
$population = $this->population;
uasort($population, function (Person $person1, Person $person2) {
return $person2->getValue() <=> $person1->getValue();
});
return array_slice($population, 0, $int);
}
}
$life = new Life();
$life->run(100);
echo $life->geStatisticsOnMostValuable();
- Output for git.master, git.master_jit
- Parse error: syntax error, unexpected identifier "with", expecting ")" in /in/JIBoI on line 131
Process exited with code 255.
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches
Archived branches
Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:
142.61 ms | 1394 KiB | 9 Q