PhP Simulation Script Using Conditional Statements.
- Last Updated on: May 5, 2013
- By: scriptcrunch
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>
Other Interesting Blogs

CKA Exam Study Guide: Certified Kubernetes Administrator
Passing the cloud-native Certified Kubernetes Administrator (CKA) exam is not a cakewalk. To pass the CKA exam, you should have enough hands-on

Linux Foundation Coupon for 2023
Hi Techies, I wanted to let you know about a pretty sweet deal with the Linux Foundation Coupon that is running now.

[35% 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