PhP Simulation Script Using Conditional Statements.
- Last Updated On: May 5, 2013
- By: Scriptcrunch Editorial
This script simulates a dog moving to its kennel. It consists of all conditional statements except switch. Initially we set to random positions for the dog and the kennel using the php function rand(). We set up the position using the x, y cordinates. Here i have used a 10X10 square. you can check out the script given below. You can see the demo here. Simulation Demo
<html>
<title>Dog</title>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
<style type="text/css">
div.map
{
float: left;
margin: 10px;
padding:1em;
background: white;
border: 1px solid;
border-color: #e5e5e5 #dbdbdb #d2d2d2;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
-webkit-box-shadow: rgba(0, 0, 0, 0.3) 0 1px 3px;
-moz-box-shadow: rgba(0,0,0,0.3) 0 1px 3px;
box-shadow: rgba(0, 0, 0, 0.3) 0 1px 3px;
}
.header{
position: fixed;
height: 50px;
width: 100%;
top: 0;
left: 0;
border-top: 4px solid #a1cb2f;
background: #fff;
-moz-box-shadow: 0 2px 4px 0px rgba(0, 0, 0, 0.16);
-webkit-box-shadow: 0 2px 4px 0px rgba(0, 0, 0, 0.16);
box-shadow: 0 2px 4px 0px rgba(0, 0, 0, 0.16);
z-index: 999999;
}
#banner h1 {
line-height: 60px;
}
span.kennel ,span.dog
{
font-weignt:bold;
}
span.empty
{
color: #666;
}
</style>
</head>
<body>
<div class="header">
<div> <input type="button" align="center" onclick="javascript:history.go(0)" class="blu-btn" value="Click Here To View New Random Results"/> </div>
</div>
<?php
$squaresize =10; //sets the square size to create a 10X10 box
/*the following do while loop executes till there is a difference of 5 units between the dog and the kennel, either in x or y direction */ do{
$kennelX = rand(1,$squaresize-1);
$kennelY = rand(1,$squaresize-1);
$dogX = rand(1 , $squaresize-1);
$dogY = rand(1 , $squaresize-1);
}while((abs($kennelX-$dogX)<$squaresize/2) && (abs($kennelY-$dogY)<$squaresize/2));
do
{
if($dogX<$kennelX)
$dogX++;
elseif($dogX>$kennelX)
$dogX--;
if($dogY<$kennelY)
$dogY++;
elseif($dogY>$kennelY)
$dogY--;
echo '<div class="map" style="width:auto"><pre>';
for($y=0;$y<$squaresize;$y++){
for($x=0;$x<$squaresize;$x++) {
if(($y==$kennelY)&&($x==$kennelX))
{
echo '<span class="kennel"><img src="home.png"></span>'; //kennel is set
}
elseif (($y==$dogY)&&($x==$dogX))
{
echo '<span class="kennel"><img src="dog.png"></span>'; //dog is set
}
else
{
echo '<span class="empty">.</span>'; //prints empty square
}
echo ($x!= $squaresize-1)?" ":"";
}
echo "<br/>n";
}
echo "</pre></div>n";
}while ($dogX!=$kennelX || $dogY!=$kennelY);
?>
</body>
</html>
Scriptcrunch Editorial
ScriptCrunch is your go-to website for all things DevOps. We specialize in helping engineers master Kubernetes and other essential DevOps tools. Our main goal is to provide easy-to-follow tutorials and valuable resources, with a special focus on the Cloud Native Computing Foundation (CNCF). Additionally, we offer insights into Linux Foundation Training and its various initiatives.
Other Interesting Blogs
[50% OFF] Kubernetes Certification Coupons (CKA, CKAD, CKS, KCNA and KCSA)
Looking for CKA coupon and other Kubernetes certification coupon codes? You have come to the right place. Following are the list of
[80% OFF] Linux Foundation Coupon for December 2024
Looking for CKA Coupon and other Linux Foundation certifications coupons? We got you covered. With the latest Linux Foundation Coupon, you can
[65% OFF] Linux Foundation LFCA, LFCS & LFCT Exam Voucher Codes
Linux Foundation has announced up to a $284 discount on its Linux certification programs Linux Foundation Certified IT Associate (LFCA) and Linux