473,586 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I can't figure out how to get script to work for 25 pieces?

tpgames
785 Contributor
The original game worked for 15 tile sliding game. I wanted a 24 tile game. What's wrong?

Expand|Select|Wrap|Line Numbers
  1. var tiles = [[],[],[],[],[]];
  2. var grid = [[],[],[],[],[]];
  3.  
  4. var game = document.createElement("div");
  5. game.className = "game";
  6. game.style.position = "absolute"; // give tiles something to "stick" to
  7. game.style.width = width + "px";
  8. game.style.height = height + "px";
  9.  
  10.  
  11. var theEmptyT, emptyp, emptyq;
  12.  
  13. for (var i=0; i<5; ++i)
  14. for (var j=0; j<5; ++j)
  15. {
  16. var t = document.createElement("div");
  17. var x = i * tileWidth;
  18. var y = j * tileHeight;
  19.  
  20. t.style.position = "absolute";
  21. t.style.width = tileWidth + "px";
  22. t.style.height = tileHeight + "px";
  23.  
  24. t.style.backgroundImage = "url(" + im.src + ")";
  25. t.style.backgroundAttachment = "scroll";
  26. t.style.backgroundPosition = "-" + x + "px -" + y + "px";
  27. t.style.backgroundRepeat = "no-repeat";
  28.  
  29. // setting using shorthand works in ie but not in moz:
  30. // t.style.background = "url(" + im.src + ") scroll -" + x + " -" + y + " no-repeat";
  31.  
  32. t.className = "tile";
  33.  
  34. if (i == 4 && j == 4)
  35. {
  36.     t.style.display = "none"; // hide last block until game is over
  37.     t.isEmpty = true;
  38.     theEmptyT = t;
  39.     t.className = "empty";
  40. }
  41.  
  42. // i,j is original position. used to determine when you've won.
  43. t.i = i;
  44. t.j = j;
  45. // p,q is current position. initially set by repositionAll();
  46.  
  47. grid[i][j] = t;
  48.  
  49. t.onclick = 
  50.     function()
  51.     {
  52.      var p = this.p;
  53.      var q = this.q;
  54.      if (p == emptyp) // horizontally in line with empty square
  55.      {
  56.         if (q < emptyq) // empty square is to my right
  57.          for (j=emptyq; j!=q; --j)
  58.             grid[emptyp][j] = grid[emptyp][j - 1];
  59.         else
  60.          for (j=emptyq; j!=q; ++j)
  61.             grid[emptyp][j] = grid[emptyp][j + 1];
  62.         grid[p][q] = theEmptyT;
  63.      } 
  64.      else if (this.q == emptyq) // vertically in line with empty square
  65.      {
  66.         if (p < emptyp) // empty square is to my right
  67.          for (i=emptyp; i!=p; --i)
  68.             grid[i][emptyq] = grid[i - 1][emptyq];
  69.         else
  70.          for (i=emptyp; i!=p; ++i)
  71.             grid[i][emptyq] = grid[i + 1][emptyq];
  72.         grid[p][q] = theEmptyT;
  73.      }
  74.      repositionAll();
  75.     }
  76.  
  77. game.appendChild(t);
  78. } // huge for loop to create tiles
  79.  
  80.  
  81.  
  82. function repositionAll()
  83. {
  84. var correct = 0, i, j;
  85.  
  86. for (i=0; i<5; ++i)
  87. for (j=0; j<5; ++j)
  88. {
  89.      t = grid[i][j];
  90.      if (t.p != i)
  91.      {
  92.      if (t.p != null && !t.isEmpty)
  93.          gradual(t.style, "left", tileWidth * t.p, tileWidth * i)
  94.      else
  95.          t.style.left = tileWidth * i + "px";
  96.      t.p = i;
  97.      }
  98.      if (t.q != j)
  99.      {
  100.      if (t.q != null && !t.isEmpty)
  101.          gradual(t.style, "top", tileHeight * t.q, tileHeight * j)
  102.      else 
  103.          t.style.top = tileHeight * j + "px";
  104.      t.q = j;
  105.      }
  106.      if (t.isEmpty)
  107.      {
  108.      emptyp = i;
  109.      emptyq = j;
  110.      }
  111.      if (t.i == i && t.j == j)
  112.      ++correct;
  113. }
  114.  
  115. theEmptyT.style.display = (correct == 25) ? "block" : "none";
  116. game.style.borderColor = (correct == 25) ? "lightgreen" : "black";
  117. }
  118.  
  119.  
  120. repositionAll();
  121.  
  122. if (scramble)
  123. {
  124.  
  125. // CAN'T FIGURE OUT WHAT NUMBERS GOES HERE?
  126. for (var s = 0; s < 128; ++s)
  127. {
  128.     var r = Math.random();
  129.     var sp, sq;
  130.     if (r < .5)
  131.     {
  132.      sp = emptyp + (r < .25 ? 1 : -1);
  133.      sq = emptyq;
  134.     }
  135.     else
  136.     {
  137.      sp = emptyp;
  138.      sq = emptyq + (r > .75 ? 1 : -1);
  139.     }
  140.  
  141.     // "reflect" if try to swap with something outside of board Changed 2 to 3, 4 to 5.
  142.     if (sp == -1) sp = 1;
  143.     if (sp == 5) sp = 3;
  144.     if (sq == -1) sq = 1;
  145.     if (sq == 5) sq = 3;
  146.  
  147.     var swaptemp = grid[sp][sq];
  148.     grid[sp][sq] = grid[emptyp][emptyq];
  149.     grid[emptyp][emptyq] = swaptemp;
  150.  
  151.     emptyp = sp;
  152.     emptyq = sq;
  153. }
  154. } // if scramble
  155.  
  156. repositionAll();
  157.  
  158. var par = im.parentNode;
  159. par.insertBefore(game, im);
  160. par.removeChild(im);
  161.  
  162. } // function makeGame
  163.  
  164.  
  165.  
  166.  
  167. function replaceImage()
  168. {
  169. var url = document.f.image.value;
  170. var im = document.createElement("img");
  171.  
  172. var h = document.getElementById("imageHolder");
  173. while (h.firstChild)
  174.     h.removeChild(h.firstChild);
  175. h.appendChild(im);
  176.  
  177. im.onload = function() { makeGame(this); }
  178. im.src = url; 
  179. }
Original code:
http://www.tpgames.net/gaming/2/15squares/1.html

My altered code:

http://www.tpgames.net/gaming/2/15sq...24squares.html


-- Edited by iam_clint: Fixed link
Jan 22 '07 #1
4 1391
tpgames
785 Contributor
Okay, what do I need to change, in order to get any response to this post? LOL

1) It is NOT a school assignment. I graduated years ago.
2) I am trying to learn JS and have tried several different changes to the code and can't figure it out.
3) I do know that the [ ] [ ] does get the 25 boxes I need. Apparently, that seems to be the only thing that is working.
Thanks!
Jan 25 '07 #2
iam_clint
1,208 Recognized Expert Top Contributor
sorry i didn't reply to your post sooner i haven't been around in a day or so.


I played the game alittle bit and i don't fully understand what the problem is....

please elaborate what is happening in your game and what you want it to be doing.
Jan 25 '07 #3
tpgames
785 Contributor
Here is the link to the 15 square puzzle the works properly.
http://www.tpgames.net/gaming/2/15sq...quaresnav.html

I want the 24 piece puzzle to work exactly like the 15 square puzzle. The script doesn't divide the puzzle into 25 pieces, and blank out the 25th piece like it should. Also, the puzzle pieces don't always move how they should, in that the game sees one spot as blank, but I see lots of blanks.

(I came down with a bad cold, so couldn't get back to this one right away.)
Jan 30 '07 #4
tpgames
785 Contributor
What I mean is that one image will be in square 25, but won't move to square 24 or any other square.
A image in squares 1 - 4, 6 - 9, 11 - 14, 16 - 19 move correctly.
Images in squares 5, 10, 15, and 20 - 25 do not move properly always. (counting the squares from left to right, top to bottom).
Thanks! :>)
Jan 31 '07 #5

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

Similar topics

3
1397
by: frank.baris | last post by:
I usually can figure out anything by searching the groups or the net all day, but this problem has been bugging me for awhile now. I have multiple databases, all of which have the same 2 tables, and all the same fields and field types. Each database holds a particular vendors data, lets call them VDB1, VDB2, ... VDBX. Then I have a temp...
2
27884
by: tlotr | last post by:
Can I get my local IP address from javascript under IE? I've some pieces of code that work under netscape, but it doesnt under ie. Thanks in advance, tlotr
21
1769
by: Cigar | last post by:
I am developing a program for a client. She runs a shop where her clients bring in items for sale or short term buyback. Development of the program has been going great but she's mentioned that there is a 'feature' coming up in the next couple of weeks that she'd like me to implement that has me a bit worried. My client has told me a...
7
11173
by: mattrapoport | last post by:
I have a page with a div on it. The div displays a user comment. When the user logs into this page, their current comment is pulled from a db and displayed in the div. The user can edit the comment through a pop-up that contains a textarea. When the user hits OK on the pop-up, the text in the textarea is sent to a function on the main...
4
5932
by: aramsey | last post by:
I have a javascript program that works fine under Firefox and on IE when running XP, but is having a problem with IE running under Windows 2000 Pro. On my personal XP development machine I have the Microsoft Script Editor and I can set a breakpoint, step through code, inspect variables, etc... with no problem. On a machine where I am...
19
1962
RMWChaos
by: RMWChaos | last post by:
Previously, I had used independent JSON lists in my code, where the lists were part of separate scripts. Because this method did not support reuse of a script without modification, I decided to consolidate all my JSON lists into one and modify my scripts so that they were more generic and reusable. So far so good. The problem is that my JSON...
4
1301
by: TriAdmin | last post by:
The return from a curl() cal is:https_Ordernumber=https_card_number=**************https_exp_date=1218https_amount=40.54https_customer_code=https_salestax=https_result=8https_result_message=NOT...
15
3234
by: Lawrence Krubner | last post by:
Does anything about this script look expensive, in terms of resources or execution time? This script dies after processing about 20 or 25 numbers, yet it leaves no errors in the error logs. This is on a server that handles a fairly demanding site. The defaults, in php.ini, have all been cranked fairly high: scripts get 180 seconds to run, and...
4
3089
by: Chris Seymour | last post by:
Hi All, I am working on a python script for my colleague that will walk a directory and search in the different files for a specific string. These pieces I am able to do. What my colleague wants is that when she runs the script the filename is replaced by the current file that is being processed. So instead of seeing a series of files being...
0
7912
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8202
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6614
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.