Is there a way to get such moving result in one line without getting 7 lines?
For example made it like this:
$trackLength = 18;
$playerSymbol = "_";
$emptyTrack = array_fill(0, $trackLength, $playerSymbol);
$playerList = ["#", "X", 1, "$", "Y", 2, "!", "Z", 3, "@", "C", 4];
$ChosenPlayer = [];
$makePlayerTrack = [];
$randomMin = 1;
$randomMax = 2;
$oneStep = $randomMax - 1;
$runningInProgress = true;
$reachedFinish = [];
$playerCount = (int) readline("Please enter amount of participants: ");
if (!is_numeric($playerCount) || !(0 <= $playerCount && $playerCount < count($playerList))){
echo "Invalid input, should be from ";
exit;
}
for ($i = 0; $i < $playerCount; $i++){
$makePlayerTrack[$i] = $emptyTrack;
$ChosenPlayer[] = $playerList[$i];
$makePlayerTrack[$i][0] = $ChosenPlayer[$i];
}
while ($runningInProgress) {
usleep(50000);
for ($i = 0; $i < $playerCount; $i++){
echo implode(" ", $makePlayerTrack[$i]) . PHP_EOL;
$position = array_search($ChosenPlayer[$i], $makePlayerTrack[$i]);
$randomSpeed = rand($randomMin, $randomMax);
if ($position < $trackLength - 1){
$makePlayerTrack[$i][$position] = $playerSymbol;
if ($position === $trackLength - 2){
$makePlayerTrack[$i][$position + $oneStep] = $playerList[$i];
} else{
$makePlayerTrack[$i][$position + $randomSpeed] = $playerList[$i];
}
//echo $randomSpeed . PHP_EOL;
} else{
$reachedFinish[] = $ChosenPlayer[$i];
$reachedFinish = array_unique($reachedFinish);
}
if (count(array_unique($reachedFinish)) === $playerCount){
echo PHP_EOL;
echo "Winner board: " . PHP_EOL;
for ($i = 1; $i < $playerCount + 1; $i++){
echo "{$i}. " . $reachedFinish[$i - 1] . PHP_EOL;
}
$runningInProgress = false;
}
}
}
Update:
To sum up:
Expected input could be string: $character = "#"; or string in array $character = ["#"];
This string is moving in predefined string field with certain length: "_"
Movement happens with heartbeat like steps using function: sleep(1);
Movement step amount could be changed for example with: rand(1,3)
, meaning after 1s passed and echo
refreshes, the "#"
could be from:
#______
to ___#___
, by jumping 3 steps if that is the result of rand(1,3)
;
Expected output in echo with step 1:
#______
_#_____
__#____
___#___
____#__
_____#_
______#
Is there a way to get such moving result in one line without getting 7 lines?