模拟红绿灯系统的 CLI 脚本
行为:
1) 06:00-23:00期间
绿灯30秒
绿黄灯亮5秒
红色 40 秒
回到绿色
2) 23:00-06:00期间
- 黄灯闪烁(2 秒熄灭/1 秒亮)
预期输出:
- 每秒当前灯光的文本表示
模拟红绿灯系统的 CLI 脚本
行为:
1) 06:00-23:00期间
绿灯30秒
绿黄灯亮5秒
红色 40 秒
回到绿色
2) 23:00-06:00期间
预期输出:
<?php
if (PHP_SAPI !== 'cli') exit("Not allowed here..");
$hour = date("H");
while (true) {
// 6am to 11pm
if($hour >= 6 && $hour < 23){
$j = 30;
for ($i=0; $i < $j; $i++) {
echo "green light [".($i+1)."]\n";
sleep(1);
}
$j = 5;
for ($i=0; $i < $j; $i++) {
echo $i%2 ? "green" : "yellow";
echo " light [".($i+1)."]\n";
sleep(1);
}
$j = 40;
for ($i=0; $i < $j; $i++) {
echo "red light [".($i+1)."]\n";
sleep(1);
}
}
if($hour < 6 || $hour >= 23){
$j = 2;
for ($i=0; $i < $j; $i++) {
echo "yellow light off [".($i+1)."]\n";
sleep(1);
}
$j = 1;
for ($i=0; $i < $j; $i++) {
echo "yellow light on [".($i+1)."]\n";
sleep(1);
}
}
}
:-D
添加了一个不同的版本来更新 cli 输出而不是打印新行。
<?php
if (PHP_SAPI !== 'cli') exit("Not allowed here..");
$hour = date("H");
echo "Light |State|Sec\n";
while(true){
// 6am to 11pm
if($hour >= 6 && $hour < 23){
$j = 30;
for ($i=0; $i < $j; $i++) {
output("green","on",$i+1);
}
$j = 5;
for ($i=0; $i < $j; $i++) {
$ltcol = $i%2 ? "green" : "yellow";
output($ltcol,"on",$i+1);
}
$j = 40;
for ($i=0; $i < $j; $i++) {
output("red","on",$i+1);
}
}
// 11pm to 6am
if($hour < 6 || $hour >= 23){
$j = 2;
for ($i=0; $i < $j; $i++) {
output("yellow","off",$i+1);
}
$j = 1;
for ($i=0; $i < $j; $i++) {
output("yellow","on",$i+1);
}
}
}
function output($light,$state,$sec){
$str = str_pad($light, 6, ' ', STR_PAD_RIGHT).' '.str_pad($state, 5, ' ', STR_PAD_RIGHT).' '.str_pad($sec, 2, ' ', STR_PAD_RIGHT);
echo "\033[16D";
echo $str;
sleep(1);
}
<?php
if (PHP_SAPI !== 'cli') exit("Not allowed here..");
$hour = date("H");
while (true) {
// 6am to 11pm
if($hour >= 6 && $hour < 23){
$j = 30;
for ($i=0; $i < $j; $i++) {
echo "green light [".($i+1)."]\n";
sleep(1);
}
$j = 5;
for ($i=0; $i < $j; $i++) {
echo $i%2 ? "green" : "yellow";
echo " light [".($i+1)."]\n";
sleep(1);
}
$j = 40;
for ($i=0; $i < $j; $i++) {
echo "red light [".($i+1)."]\n";
sleep(1);
}
}
if($hour < 6 || $hour >= 23){
$j = 2;
for ($i=0; $i < $j; $i++) {
echo "yellow light off [".($i+1)."]\n";
sleep(1);
}
$j = 1;
for ($i=0; $i < $j; $i++) {
echo "yellow light on [".($i+1)."]\n";
sleep(1);
}
}
}
正如在评论中,您同意通过 Javascript 实现此目的这是您有趣的输出。
var increment = 0;
setInterval(function(){
var time = new Date();
var hours = time.getHours();
var seconds = time.getSeconds();
var count = 75;//green+red
if(increment >= count){
increment =0;
}
if(hours >= 6 && hours<23 ){
//daySignal
document.querySelector('#shift').innerHTML = 'Day Signal';
if( increment >= 0 && increment < 35){
displayGreen(35-increment);
}
if( increment >= 30 && increment < 35){
displayYellow(35-increment);
}
if( increment >= 35){
displayRed(count-increment);
}
}else{
// knightSignal
document.querySelector('#shift').innerHTML = 'Knight Signal';
reset();
if( increment %3 != 0){
displayYellow(0);
}
}
increment +=1;
},1000);
function displayRed(timer='') {
reset();
document.querySelector('#timer').innerHTML = timer;
document.querySelector('#red_light').style.backgroundColor = "red";
}
function displayYellow(timer='') {
document.querySelector('#timer').innerHTML = timer;
document.querySelector('#yellow_light').style.backgroundColor = "yellow";
}
function displayGreen(timer='') {
reset();
document.querySelector('#timer').innerHTML = timer;
document.querySelector('#green_light').style.backgroundColor = "green";
}
function reset() {
document.querySelector('#red_light').style.backgroundColor = "black";
document.querySelector('#yellow_light').style.backgroundColor = "black";
document.querySelector('#green_light').style.backgroundColor = "black";
}
.signal {
height: 30px;
width: 30px;
background-color: #000;
border-radius: 50%;
margin: 15px auto;
}
.timer{
color: #000;
font-size:20px;
text-align: center;
}
<div id="shift"></div>
<div class="">
<div id="timer" class="timer"></div>
<div id="red_light" class="signal"></div>
<div id="yellow_light" class="signal"></div>
<div id="green_light" class="signal"></div>
</div>
var increment = 0;
setInterval(function(){
var time = new Date();
var hours = time.getHours();
var seconds = time.getSeconds();
var count = 75;//green+red
if(increment >= count){
increment =0;
}
if(false && hours >= 6 && hours<23 ){// false to get night signal
//daySignal
document.querySelector('#shift').innerHTML = 'Day Signal';
if( increment >= 0 && increment < 35){
displayGreen(35-increment);
}
if( increment >= 30 && increment < 35){
displayYellow(35-increment);
}
if( increment >= 35){
displayRed(count-increment);
}
}else{
// knightSignal
document.querySelector('#shift').innerHTML = 'Knight Signal';
reset();
if( increment %3 != 0){
displayYellow(increment%3);
}
}
increment +=1;
},1000);
function displayRed(timer='') {
reset();
document.querySelector('#timer').innerHTML = timer;
document.querySelector('#red_light').style.backgroundColor = "red";
}
function displayYellow(timer='') {
document.querySelector('#timer').innerHTML = timer;
document.querySelector('#yellow_light').style.backgroundColor = "yellow";
}
function displayGreen(timer='') {
reset();
document.querySelector('#timer').innerHTML = timer;
document.querySelector('#green_light').style.backgroundColor = "green";
}
function reset() {
document.querySelector('#red_light').style.backgroundColor = "black";
document.querySelector('#yellow_light').style.backgroundColor = "black";
document.querySelector('#green_light').style.backgroundColor = "black";
}
.signal {
height: 30px;
width: 30px;
background-color: #000;
border-radius: 50%;
margin: 25px auto;
}
.timer{
color: #000;
font-size:20px;
text-align: center;
}
<div id="shift"></div>
<div class="">
<div id="timer" class="timer"></div>
<div id="red_light" class="signal"></div>
<div id="yellow_light" class="signal"></div>
<div id="green_light" class="signal"></div>
</div>
注意:请不要在有红灯时过马路。