473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Function to check plan

KeredDrahcir
426 256MB
I'm creating an application that allow the user to draw out a floor plan. It gives them a 12 x 8 grid and lets them click up to 50 sqaures. It stops them when they get to 50 but they can click on an already selected sqaure to turn it blank and then choose another.

What I need to be able to do is check the plan. They can't have any gaps in it. All squares have the accesable from all other square on only in the four main directions (no diagonals).

Is there some kind of function that could image a man standing in square one and making sure he can visit all the other squares.

I'm willing to use php or JavaScript if required. Is there anything already around that will do that, or would somebody be able to assist me.

There code for creating the floor plan is below.
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2.  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  5.  
  6.   <head>
  7.     <title>Plan</title>
  8.     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
  9.     <script type="text/javascript">
  10.     <!--
  11.       var count=0;
  12.  
  13.       function plan(id)
  14.       {
  15.         var obj = document.getElementById(id);
  16.  
  17.         if(obj.style.backgroundColor == "rgb(0, 0, 0)")
  18.         {
  19.           if(count <= 49)
  20.           {
  21.             count++;
  22.           }
  23.           else
  24.           {
  25.              alert('You can have a maximum of 50');
  26.             count++;
  27.           }
  28.         }
  29.         else if(obj.style.backgroundColor == "rgb(255, 0, 0)")
  30.         {
  31.           count--;
  32.         }
  33.         if(count <= 50)
  34.         {
  35.           obj.style.backgroundColor = (obj.style.backgroundColor == "rgb(0, 0, 0)") ? "#ff0000" : "#000000";
  36.           obj.style.color = (obj.style.backgroundColor == "rgb(0, 0, 0)") ? "#000000" : "#ffffff";
  37.         }
  38.         if (count>50)
  39.         {
  40.             count--;
  41.         }
  42.       }
  43.  
  44.       function number()
  45.       {
  46.         var room_number=0;
  47.         col=0;
  48.         row="a";
  49.         for (var i=1; i<97; i++)
  50.         {
  51.           col++;
  52.           if (i<97)
  53.           {
  54.             row="h";
  55.           }
  56.           if (i<85)
  57.           {
  58.             row="g";
  59.           }
  60.           if (i<73)
  61.           {
  62.             row="f";
  63.           }
  64.           if (i<61)
  65.           {
  66.             row="e";
  67.           }
  68.           if (i<49)
  69.           {
  70.             row="d";
  71.           }
  72.           if (i<37)
  73.           {
  74.             row="c";
  75.           }
  76.           if (i<25)
  77.           {
  78.             row="b";
  79.           }
  80.           if (i<13)
  81.           {
  82.             row="a";
  83.           }
  84.  
  85.           if (col>12)
  86.           {
  87.             col=1;
  88.           }
  89.           var room = document.getElementById(row+col);
  90.           if (room.style.backgroundColor == "rgb(255, 0, 0)")
  91.           {
  92.             room_number++;
  93.             room.textContent=room_number;
  94.           }
  95.           else
  96.           {
  97.             room.textContent="";
  98.           }
  99.         }
  100.       }
  101.     //-->
  102.     </script>
  103.   </head>
  104.  
  105.   <body style="background-color: #000000; width: 386px; margin: 10px auto 0;">
  106. <?php
  107.     $l=0;
  108.     $j=0;
  109.  
  110.     for ($i=0; $i<96; $i++)
  111.     {
  112.       $l++;
  113.       $j++;
  114.       if ($j<97)
  115.       {
  116.         $letter=h;
  117.       }
  118.       if ($j<85)
  119.       {
  120.         $letter=g;
  121.       }
  122.       if ($j<73)
  123.       {
  124.         $letter=f;
  125.       }
  126.       if ($j<61)
  127.       {
  128.         $letter=e;
  129.       }
  130.       if ($j<49)
  131.       {
  132.         $letter=d;
  133.       }
  134.       if ($j<37)
  135.       {
  136.         $letter=c;
  137.       }
  138.       if ($j<25)
  139.       {
  140.         $letter=b;
  141.       }
  142.       if ($j<13)
  143.       {
  144.         $letter=a;
  145.       }
  146.  
  147.       if ($l>12)
  148.       {
  149.         $l=1;
  150.       }
  151.       $border="2px 0 0 2px";
  152.       if ($l==12)
  153.       {
  154.         $border="2px 2px 0 2px";
  155.       }
  156.       if ($j>84)
  157.       {
  158.         $border="2px 0 2px 2px";
  159.       }
  160.       if ($j==96)
  161.       {
  162.           $border="2px 2px 2px 2px";
  163.       }
  164. ?>
  165.       <div style="width: 30px; height: 30px; line-height: 30px; border-style: solid; border-color: #ffffff; border-width: <?=$border;?>; float: left; background-color: #000000; color: #ffffff; text-align: center; vertical-align: middle; font-weight: 700;" id="<?=$letter, $l;?>" onclick="plan('<?=$letter, $l;?>');">&nbsp;</div>
  166. <?php
  167.     }
  168. ?>
  169.     <input type="button" value="Done" onclick="number();"/>
  170.   </body>
  171. </html>
Dec 30 '13 #1
5 1050
Rabbit
12,516 Expert Mod 8TB
Loop through every box and if it is an "active box" check the north, south, east, and west, boxes for at least one active box.
Dec 30 '13 #2
KeredDrahcir
426 256MB
I thought about that but found that if there could you have two groups of four boxes not connected. Someone did this once and I'm not sure how but this was 30 years ago on the computers of that day!
Dec 31 '13 #3
Rabbit
12,516 Expert Mod 8TB
In that case, you want to find the first "active" box and use a recursive algorithm to traverse all the connected boxes. Use an array to keep track of the traversed boxes. Then compare the traversal array to the original boxes to see if any "active" boxes have not been traversed.

This way, only one group can ever be traversed.
Dec 31 '13 #4
KeredDrahcir
426 256MB
That's the idea. I was just wondering if someone could start me off because I'm not quite what way to go around it.
It's not quite as simple on some plans. Would it help if included a plan that does work that I've used but would be very tricky to validate?
Dec 31 '13 #5
Rabbit
12,516 Expert Mod 8TB
It wouldn't hurt to have an example.

In pseudo-code, the traversal would look like this:
Expand|Select|Wrap|Line Numbers
  1. function isValid() {
  2.    for each box
  3.       if box is active
  4.          traverse box
  5.          exit for
  6.  
  7.    for each box
  8.       if box is active and not traversed
  9.          return fail
  10.  
  11.    return pass
  12. }  
  13.  
  14. function traverse(box) {
  15.    mark box as traversed in traversal array
  16.  
  17.    if north box is active and not traversed
  18.       traverse north box
  19.  
  20.    if east box is active and not traversed
  21.       traverse east box
  22.  
  23.    if south box is active and not traversed
  24.       traverse south box
  25.  
  26.    if west box is active and not traversed
  27.       traverse west box
  28. }
Dec 31 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jay | last post by:
Hi ! I am a beginner in PHP. Does anybody know how to check if an URL is alive or dead in PHP (not exist or server down...or whatever ) ? Any help would be appreciated ! Thanks ! Jay
3
by: Bryan Parkoff | last post by:
Do C/C++ Compiler allow function to contain more than 8 parameters? I checked MS Visual C++ 6.0 that it can only limit 8 parameters, but most C/C++ Compiler can limit maximum 256 parameters. Can...
1
by: vncntj | last post by:
i want to enable one listbox and disable the other depending on if i check winter_gala (checkbox) here is my code. <script type="text/javascript"> function check() { if...
2
by: Dknight | last post by:
Hi, all! I have a problem, how can I check a form data before sending it to server. I have the example below, but first data is sent to server, but after JavaScript checks the data :( I need...
1
by: padew | last post by:
<form method="post" name="formWin" onSubmit="check();"> this work only if I send the form's datas directly with <input type=submit ...> if I use document.formWin.submit(); for to send the...
2
by: ext237 | last post by:
hello. I'm trying to halt a function's execution while waiting for user interaction. For example, I have a function called getUserValue() that pops up a hidden div containing several buttons....
0
by: Dave Siegel | last post by:
Hey, Is it possible to have the webbrowser give me a msgbox of the javascript execution every time javascript has been executed? If yes, How? Because i'm oblivious on how to get it.
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
3
by: maheswaran | last post by:
hi all, I have doubt in class_exist funxtion. I know its use to Tests whether a class is defined or not. But i want to know how its work. Is this function check any specific DB or any definition...
2
by: amit yogi | last post by:
hi , i am making a form of user registratin and using server side validation. but function is not working on click. please suggest... <html> <head> <script language="javascript">
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.