用于模拟交通信号灯系统的CLI脚本
行为:
1)在06:00-23:00期间
绿灯亮30秒
绿色和黄色指示灯亮5秒钟
红色持续40秒
回到绿色
2)在23:00-06:00期间
预期输出:
答案 0 :(得分:0)
<?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);
}
答案 1 :(得分:0)
作为注释,您同意使用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>
注意:当有红色信号时,请不要过马路。
答案 2 :(得分:0)
<?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);
}
}
}